Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jmeter.control;
   20   
   21   import java.io.Serializable;
   22   
   23   import org.apache.jmeter.samplers.Sampler;
   24   import org.apache.jmeter.testelement.property.JMeterProperty;
   25   import org.apache.jmeter.testelement.property.StringProperty;
   26   import org.apache.jmeter.threads.JMeterContextService;
   27   import org.apache.jmeter.threads.JMeterThread;
   28   import org.apache.jmeter.threads.JMeterVariables;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.log.Logger;
   31   
   32   // @see TestWhileController for unit tests
   33   
   34   public class WhileController extends GenericController implements Serializable {
   35       private static final Logger log = LoggingManager.getLoggerForClass();
   36   
   37       private static final long serialVersionUID = 232L;
   38   
   39       private static final String CONDITION = "WhileController.condition"; // $NON-NLS-1$
   40   
   41       public WhileController() {
   42       }
   43   
   44       /*
   45        * Evaluate the condition, which can be:
   46        * blank or LAST = was the last sampler OK?
   47        * otherwise, evaluate the condition to see if it is not "false"
   48        * If blank, only evaluate at the end of the loop
   49        *
   50        * Must only be called at start and end of loop
   51        *
   52        * @param loopEnd - are we at loop end?
   53        * @return true means OK to continue
   54        */
   55       private boolean endOfLoop(boolean loopEnd) {
   56           String cnd = getCondition().trim();
   57           log.debug("Condition string:" + cnd+".");
   58           boolean res;
   59           // If blank, only check previous sample when at end of loop
   60           if ((loopEnd && cnd.length() == 0) || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$
   61               JMeterVariables threadVars = JMeterContextService.getContext().getVariables();
   62               res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// $NON-NLS-1$
   63           } else {
   64               // cnd may be null if next() called us
   65               res = "false".equalsIgnoreCase(cnd);// $NON-NLS-1$
   66           }
   67           log.debug("Condition value: " + res);
   68           return res;
   69       }
   70   
   71       /*
   72        * (non-Javadoc) Only called at End of Loop
   73        *
   74        * @see org.apache.jmeter.control.GenericController#nextIsNull()
   75        */
   76       protected Sampler nextIsNull() throws NextIsNullException {
   77           reInitialize();
   78           if (endOfLoop(true)){
   79               return null;
   80           }
   81           return next();
   82       }
   83   
   84       /*
   85        * This skips controller entirely if the condition is false on first entry.
   86        */
   87       public Sampler next(){
   88           if (isFirst()){
   89               if (endOfLoop(false)){
   90                   return null;
   91               }
   92           }
   93           return super.next();
   94       }
   95   
   96       /**
   97        * @param string
   98        *            the condition to save
   99        */
  100       public void setCondition(String string) {
  101           log.debug("setCondition(" + string + ")");
  102           setProperty(new StringProperty(CONDITION, string));
  103       }
  104   
  105       /**
  106        * @return the condition
  107        */
  108       public String getCondition() {
  109           String cnd;
  110           JMeterProperty prop=getProperty(CONDITION);
  111           prop.recoverRunningVersion(this);
  112           cnd = prop.getStringValue();
  113           return cnd;
  114       }
  115   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]