sebb        2005/03/10 16:00:24

  Modified:    src/core/org/apache/jmeter/control Tag: rel-2_0
                        WhileController.java
  Log:
  Add test cases
  TODO fix Stack Overflow when controller never runs
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.6   +170 -20   
jakarta-jmeter/src/core/org/apache/jmeter/control/WhileController.java
  
  Index: WhileController.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/WhileController.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- WhileController.java      8 Mar 2005 22:49:12 -0000       1.1.2.5
  +++ WhileController.java      11 Mar 2005 00:00:23 -0000      1.1.2.6
  @@ -20,7 +20,10 @@
   
   import java.io.Serializable;
   
  +import org.apache.jmeter.junit.JMeterTestCase;
  +import org.apache.jmeter.junit.stubs.TestSampler;
   import org.apache.jmeter.samplers.Sampler;
  +import org.apache.jmeter.testelement.TestElement;
   import org.apache.jmeter.testelement.property.StringProperty;
   import org.apache.jmeter.threads.JMeterContextService;
   import org.apache.jmeter.threads.JMeterThread;
  @@ -35,6 +38,9 @@
   {
       private static Logger log = LoggingManager.getLoggerForClass();
        private final static String CONDITION = "WhileController.condition"; // 
$NON-NLS-1$
  +     
  +     private static boolean testMode=false; // To make testing easier
  +     private static boolean testModeResult=false; // dummy sample result
   
       public WhileController()
       {
  @@ -52,23 +58,32 @@
       /*
        * Evaluate the condition, which can be:
        * blank or LAST = was the last sampler OK?
  -     * otherwise, evaluate the condition
  -     * @param inLoop - called by nextIsNull (within loop)
  +     * otherwise, evaluate the condition to see if it is not "false"
  +     * If blank, only evaluate at the end of the loop
  +     * 
  +     * Must only be called at start and end of loop
  +     * 
  +     * @param loopEnd - are we at loop end?
  +     * @return true means OK to continue 
        */
  -    private boolean conditionTrue(boolean inLoop)
  +    private boolean conditionTrue(boolean loopEnd)
       {
   //           clear cached condition
                getProperty(CONDITION).recoverRunningVersion(null);
        String cnd = getCondition();
        log.debug("Condition string:"+cnd);
        boolean res;
  -     // If blank, only check previous sample when in loop
  -     if ((inLoop && cnd.length() == 0) 
  +     // If blank, only check previous sample when at end of loop
  +     if ((loopEnd && cnd.length() == 0) 
                        || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$
  -             JMeterVariables threadVars = 
  -                     JMeterContextService.getContext().getVariables();
  -             // Use !false rather than true, so that null is treated as true 
  -                 res = 
!"false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// 
$NON-NLS-1$
  +                     if (testMode) {
  +                             res=testModeResult;
  +                     } else {
  +                 JMeterVariables threadVars = 
  +                         JMeterContextService.getContext().getVariables();
  +                 // Use !false rather than true, so that null is treated as 
true 
  +                     res = 
!"false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// 
$NON-NLS-1$
  +         }
        } else {
                // cnd may be blank if next() called us
                res = !"false".equalsIgnoreCase(cnd);// $NON-NLS-1$
  @@ -78,6 +93,7 @@
       }
   
        /* (non-Javadoc)
  +      * Only called at End of Loop
        * @see org.apache.jmeter.control.GenericController#nextIsNull()
        */
       protected Sampler nextIsNull() throws NextIsNullException
  @@ -85,7 +101,7 @@
           reInitialize();
           if (conditionTrue(true))
           {
  -            return next();
  +            return super.next();
           }
           else
           {
  @@ -94,16 +110,13 @@
           }
       }
   
  -    /*
  -     * This skips controller entirely if the condition is false
  -     * 
  -     * TODO consider checking for previous sampler failure here -
  -     * would need to distinguish this from previous failure *inside* loop 
  -     * 
  -     */
       public Sampler next()
       {
  -        if(conditionTrue(false))// $NON-NLS-1$
  +             if (current!=0){ // in the middle of the loop
  +                     return super.next();
  +             }
  +             // Must be start of loop
  +        if(conditionTrue(false)) // Still OK
           {
               return super.next(); // OK to continue
           }
  @@ -131,4 +144,141 @@
                log.debug("getCondition() => "+cnd);
                return cnd;
        }
  +    public static class Test extends JMeterTestCase
  +    {
  +         static{
  +                 //LoggingManager.setPriority("DEBUG","jmeter");
  +                 //LoggingManager.setTarget(new 
java.io.PrintWriter(System.out));
  +         }
  +
  +        public Test(String name)
  +        {
  +            super(name);
  +        }
  +
  +             // Get next sample and its name
  +             private String nextName(GenericController c){
  +                     Sampler s = c.next();
  +                     if (s==null){
  +                             return null;
  +                     } else {
  +                         return s.getPropertyAsString(TestElement.NAME);
  +                     }
  +             }
  +             
  +             // While (blank), previous sample OK - should loop until false
  +             public void testBlankPrevOK() throws Exception
  +             {
  +                     log.info("testBlankPrevOK");
  +                     runtestPrevOK("");
  +             }
  +             
  +             // While (LAST), previous sample OK - should loop until false
  +             public void testLastPrevOK() throws Exception
  +             {
  +                     log.info("testLASTPrevOK");
  +                     runtestPrevOK("LAST");
  +             }
  +             
  +             private static final String OTHER = "X"; // Dummy for testing 
functions
  +             // While (LAST), previous sample OK - should loop until false
  +             public void testOtherPrevOK() throws Exception
  +             {
  +                     log.info("testOtherPrevOK");
  +                     runtestPrevOK(OTHER);
  +             }
  +             
  +        public void runtestPrevOK(String type) throws Exception
  +        {
  +                     testMode=true;
  +                     testModeResult=true;
  +            GenericController controller = new GenericController();
  +            WhileController while_cont = new WhileController();
  +                     while_cont.setCondition(type);
  +            while_cont.addTestElement(new TestSampler("one"));
  +            while_cont.addTestElement(new TestSampler("two"));
  +            while_cont.addTestElement(new TestSampler("three"));
  +            controller.addTestElement(while_cont);                   
  +            controller.addTestElement(new TestSampler("four"));
  +            controller.initialize();
  +                     assertEquals("one",nextName(controller));
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     assertEquals("one",nextName(controller));
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     assertEquals("one",nextName(controller));
  +                     testModeResult=false;// one and two fail
  +                     if (type.equals(OTHER)) 
while_cont.setCondition("false");
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     testModeResult=true;// but three OK, so does not exit
  +                     if (type.equals(OTHER)) while_cont.setCondition(OTHER);
  +                     assertEquals("one",nextName(controller));
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     testModeResult=false;// three fails, so exits while
  +                     if (type.equals(OTHER)) 
while_cont.setCondition("false");
  +                     assertEquals("four",nextName(controller));
  +                     assertNull(nextName(controller));
  +                     testModeResult=true;
  +                     if (type.equals(OTHER)) while_cont.setCondition(OTHER);
  +                     assertEquals("one",nextName(controller));
  +         }
  +             
  +             // While (blank), previous sample failed - should run once
  +        public void testBlankPrevFailed() throws Exception
  +        {
  +                     log.info("testBlankPrevFailed");
  +                     testMode=true;
  +                     testModeResult=false;
  +            GenericController controller = new GenericController();
  +            WhileController while_cont = new WhileController();
  +                     while_cont.setCondition("");
  +            while_cont.addTestElement(new TestSampler("one"));
  +            while_cont.addTestElement(new TestSampler("two"));
  +            controller.addTestElement(while_cont);                   
  +            controller.addTestElement(new TestSampler("three"));
  +            controller.initialize();
  +                     assertEquals("one",nextName(controller));
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     assertNull(nextName(controller));
  +                     assertEquals("one",nextName(controller));
  +                     assertEquals("two",nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     assertNull(nextName(controller));
  +        }
  +
  +             // While LAST,  previous sample failed - should not run
  +        public void testLASTPrevFailed() throws Exception
  +        {
  +                     log.info("testLastPrevFailed");
  +                     runTestPrevFailed("LAST");
  +        }
  +             // While False,  previous sample failed - should not run
  +        public void testfalsePrevFailed() throws Exception
  +        {
  +                     log.info("testFalsePrevFailed");
  +                     runTestPrevFailed("False");
  +        }
  +         public void runTestPrevFailed(String s) throws Exception
  +         {
  +                     testMode=true;
  +                     testModeResult=false;
  +            GenericController controller = new GenericController();
  +            WhileController while_cont = new WhileController();
  +                     while_cont.setCondition(s);
  +            while_cont.addTestElement(new TestSampler("one"));
  +            while_cont.addTestElement(new TestSampler("two"));
  +            controller.addTestElement(while_cont);                   
  +            controller.addTestElement(new TestSampler("three"));
  +            controller.initialize();
  +                     assertEquals("three",nextName(controller));
  +                     assertNull(nextName(controller));
  +                     assertEquals("three",nextName(controller));
  +                     assertNull(nextName(controller));
  +        }
  +
  +   }
   }
  \ No newline at end of file
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to