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.BooleanProperty;
   25   import org.apache.jmeter.testelement.property.IntegerProperty;
   26   import org.apache.jmeter.testelement.property.JMeterProperty;
   27   import org.apache.jmeter.testelement.property.StringProperty;
   28   
   29   /**
   30    * Class that implements the Loop Controller.
   31    */
   32   public class LoopController extends GenericController implements Serializable {
   33   
   34       private final static String LOOPS = "LoopController.loops"; // $NON-NLS-1$
   35   
   36       private static final long serialVersionUID = 232L;
   37   
   38       /*
   39        * In spite of the name, this is actually used to determine if the loop controller is repeatable.
   40        *
   41        * The value is only used in nextIsNull() when the loop end condition has been detected:
   42        * If forever==true, then it calls resetLoopCount(), otherwise it calls setDone(true).
   43        *
   44        * Loop Controllers always set forever=true, so that they will be executed next time
   45        * the parent invokes them.
   46        *
   47        * Thread Group sets the value false, so nextIsNull() sets done, and the Thread Group will not be repeated.
   48        * However, it's not clear that a Thread Group could ever be repeated.
   49        */
   50       private final static String CONTINUE_FOREVER = "LoopController.continue_forever"; // $NON-NLS-1$
   51   
   52       private transient int loopCount = 0;
   53   
   54       public LoopController() {
   55           setContinueForever_private(true);
   56       }
   57   
   58       public void setLoops(int loops) {
   59           setProperty(new IntegerProperty(LOOPS, loops));
   60       }
   61   
   62       public void setLoops(String loopValue) {
   63           setProperty(new StringProperty(LOOPS, loopValue));
   64       }
   65   
   66       public int getLoops() {
   67           try {
   68               JMeterProperty prop = getProperty(LOOPS);
   69               return Integer.parseInt(prop.getStringValue());
   70           } catch (NumberFormatException e) {
   71               return 0;
   72           }
   73       }
   74   
   75       public String getLoopString() {
   76           return getPropertyAsString(LOOPS);
   77       }
   78   
   79       /**
   80        * Determines whether the loop will return any samples if it is rerun.
   81        *
   82        * @param forever
   83        *            true if the loop must be reset after ending a run
   84        */
   85       public void setContinueForever(boolean forever) {
   86           setContinueForever_private(forever);
   87       }
   88   
   89       private void setContinueForever_private(boolean forever) {
   90           setProperty(new BooleanProperty(CONTINUE_FOREVER, forever));
   91       }
   92   
   93       private boolean getContinueForever() {
   94           return getPropertyAsBoolean(CONTINUE_FOREVER);
   95       }
   96   
   97       /*
   98        * (non-Javadoc)
   99        *
  100        * @see org.apache.jmeter.control.Controller#next()
  101        */
  102       public Sampler next() {
  103           if(endOfLoop()) {
  104               if (!getContinueForever()) {
  105                   setDone(true);
  106               }
  107               return null;
  108           }
  109           return super.next();
  110       }
  111   
  112       private boolean endOfLoop() {
  113           final int loops = getLoops();
  114           return (loops > -1) && (loopCount >= loops);
  115       }
  116   
  117       /*
  118        * (non-Javadoc)
  119        *
  120        * @see org.apache.jmeter.control.GenericController#nextIsNull()
  121        */
  122       protected Sampler nextIsNull() throws NextIsNullException {
  123           reInitialize();
  124           if (endOfLoop()) {
  125               if (!getContinueForever()) {
  126                   setDone(true);
  127               } else {
  128                   resetLoopCount();
  129               }
  130               return null;
  131           }
  132           return next();
  133       }
  134   
  135       protected void incrementLoopCount() {
  136           loopCount++;
  137       }
  138   
  139       protected void resetLoopCount() {
  140           loopCount = 0;
  141       }
  142   
  143       /*
  144        * (non-Javadoc)
  145        *
  146        * @see org.apache.jmeter.control.GenericController#getIterCount()
  147        */
  148       protected int getIterCount() {
  149           return loopCount + 1;
  150       }
  151   
  152       /*
  153        * (non-Javadoc)
  154        *
  155        * @see org.apache.jmeter.control.GenericController#reInitialize()
  156        */
  157       protected void reInitialize() {
  158           setFirst(true);
  159           resetCurrent();
  160           incrementLoopCount();
  161           recoverRunningVersion();
  162       }
  163   }

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