Service Virtualization

Expand all | Collapse all

Loop in ExecuteScript using testExec and String Pattern not working properly

Jump to Best Answer
  • 1.  Loop in ExecuteScript using testExec and String Pattern not working properly

    Posted 06-07-2018 07:48 AM

    When I try to use String Patterns in ExecuteScript(JSR-223), i find the following behaviour. This is weird.
    Contents of Script
          testExec.setStateValue("hashId1","{{=[H*(32)]}}");
          testExec.setStateValue("hashId2","{{=[H*(32)]}}");
    In the above scenario hashId1 and hashId2 are unique.
    Contents of Script
          for(int i=1;i<3;i++){
                testExec.setStateValue("hashId"+i,"{{=[H*(32)]}}");
          }

    In the above scenario hashId1 and hashId2 are exactly same.
    Why is this behaviour happening? How to solve this.?

    Thanks in Advance.

    If there is a better way to use String Patterns in ExecuteScipt, please suggest.



  • 2.  Re: Loop in ExecuteScript using testExec and String Pattern not working properly

    Broadcom Employee
    Posted 06-07-2018 06:41 PM

    What result are you seeing?  I will test this out too. 



  • 3.  Re: Loop in ExecuteScript using testExec and String Pattern not working properly

    Posted 06-08-2018 02:46 AM

    For case i - without loop. The values are as follows

    -> hashId1 = FC65E318D9B3E7068F0F04BD451F5C4A

    -> hashId2 = E364D2B3F6F5C989B4C8A2A3E1BC780E

     

    But for case ii - using for loop. The values are as follows.

    -> hashId1 = 7D56CA8536345F0482A35F43D5DABCAC

    -> hashId2 = 7D56CA8536345F0482A35F43D5DABCAC

     

    When i run it in loop, whey does hashId1 and hashId2 have the same value?

    i need unique hashIds generated using loop.

     



  • 4.  Re: Loop in ExecuteScript using testExec and String Pattern not working properly
    Best Answer

    Posted 06-08-2018 07:35 AM

    Are you trying to create a guaranteed unique hash?

    So far as I know, none of the OOTB pattern generators guarantee uniqueness from one use to the next. 

    Use Java's UUID.randomUUID() method instead.  



  • 5.  Re: Loop in ExecuteScript using testExec and String Pattern not working properly

    Broadcom Employee
    Posted 06-08-2018 10:06 AM

    This is expected behavior.  Here's what's happening...

     

    Anything in {{double curly braces}} gets evaluated before executing the script - not as the script executes.  In your first case, there are two sets of the {{hash code generation}} so the expression executes twice.  In the second case, you only have one evaluation of the expression no matter how many times you loop.  Essentially, that expression gets expanded beforehand and acts like a hard coded value.

     

    --Mike