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   import java.util.Iterator;
   23   
   24   import org.apache.jmeter.samplers.Sampler;
   25   import org.apache.jmeter.testelement.TestElement;
   26   import org.apache.jmeter.testelement.property.StringProperty;
   27   
   28   // For unit tests @see TestSwitchController
   29   
   30   /**
   31    * <p>
   32    * Implements a controller which selects at most one of its children
   33    * based on the condition value, which may be a number or a string.
   34    * </p>
   35    * <p>
   36    * For numeric input, the controller processes the appropriate child,
   37    * where the numbering starts from 0.
   38    * If the number is out of range, then the first (0th) child is selected.
   39    * If the condition is the empty string, then it is assumed to be 0.
   40    * </p>
   41    * <p>
   42    * For non-empty non-numeric input, the child is selected by name.
   43    * This may be the name of the controller or a sampler.
   44    * If the string does not match any of the names, then the controller
   45    * with the name "default" (any case) is processed.
   46    * If there is no default entry, then unlike the numeric case,
   47    * no child is selected.
   48    * </p>
   49    */
   50   public class SwitchController extends GenericController implements Serializable {
   51       // Package access for use by Test code
   52       final static String SWITCH_VALUE = "SwitchController.value"; //$NON-NLS-1$
   53   
   54       public SwitchController() {
   55           super();
   56       }
   57   
   58       public Sampler next() {
   59           if (isFirst()) { // Set the selection once per iteration
   60               current = getSelectionAsInt();
   61           }
   62           return super.next();
   63       }
   64   
   65       /*
   66        * (non-Javadoc)
   67        *
   68        * incrementCurrent is called when the current child (whether sampler or controller)
   69        * has been processed.
   70        *
   71        * Setting it to int.max marks the controller as having processed all its
   72        * children. Thus the controller processes one child per iteration.
   73        *
   74        * @see org.apache.jmeter.control.GenericController#incrementCurrent()
   75        */
   76       protected void incrementCurrent() {
   77           current=Integer.MAX_VALUE;
   78       }
   79   
   80       public void setSelection(String inputValue) {
   81           setProperty(new StringProperty(SWITCH_VALUE, inputValue));
   82       }
   83   
   84       /*
   85        * Returns the selection value as a int,
   86        * with the value set to zero if it is out of range.
   87        */
   88       private int getSelectionAsInt() {
   89           int ret;
   90           getProperty(SWITCH_VALUE).recoverRunningVersion(null);
   91           String sel = getSelection();
   92           try {
   93               ret = Integer.parseInt(sel);
   94               if (ret < 0 || ret >= getSubControllers().size()) {
   95                   ret = 0;
   96               }
   97           } catch (NumberFormatException e) {
   98               if (sel.length()==0) {
   99                   ret = 0;
  100               } else {
  101                   ret = scanControllerNames(sel);
  102               }
  103           }
  104           return ret;
  105       }
  106   
  107       private int scanControllerNames(String sel){
  108           Iterator iter = getSubControllers().iterator();
  109           int i = 0;
  110           int default_pos = Integer.MAX_VALUE;
  111           while(iter.hasNext()) {
  112               String name;
  113               TestElement el = (TestElement)iter.next();
  114               name=el.getName();
  115               if (name.equals(sel)) {
  116                   return i;
  117               }
  118                if (name.equalsIgnoreCase("default")) {  //$NON-NLS-1$
  119                    default_pos = i;
  120                }
  121               i++;
  122           }
  123           return default_pos;
  124       }
  125   
  126       public String getSelection() {
  127           return getPropertyAsString(SWITCH_VALUE);
  128       }
  129   }

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