Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [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.functions;
   20   
   21   import java.util.Collection;
   22   import java.util.LinkedList;
   23   import java.util.List;
   24   
   25   import org.apache.jmeter.engine.util.CompoundVariable;
   26   import org.apache.jmeter.samplers.SampleResult;
   27   import org.apache.jmeter.samplers.Sampler;
   28   import org.apache.jmeter.threads.JMeterVariables;
   29   import org.apache.jmeter.util.JMeterUtils;
   30   
   31   /**
   32    * Provides a Random function which returns a random long integer between a min
   33    * (first argument) and a max (second argument).
   34    *
   35    */
   36   public class Random extends AbstractFunction {
   37   
   38       private static final List desc = new LinkedList();
   39   
   40       private static final String KEY = "__Random"; //$NON-NLS-1$
   41   
   42       static {
   43           desc.add(JMeterUtils.getResString("minimum_param")); //$NON-NLS-1$
   44           desc.add(JMeterUtils.getResString("maximum_param")); //$NON-NLS-1$
   45           desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
   46       }
   47   
   48       private CompoundVariable varName, minimum, maximum;
   49   
   50       /**
   51        * No-arg constructor.
   52        */
   53       public Random() {
   54       }
   55   
   56       /**
   57        * Execute the function.
   58        *
   59        * @see Function#execute(SampleResult, Sampler)
   60        */
   61       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   62               throws InvalidVariableException {
   63   
   64   
   65           long min = Long.parseLong(minimum.execute().trim());
   66           long max = Long.parseLong(maximum.execute().trim());
   67   
   68           long rand = min + (long) (Math.random() * (max - min + 1));
   69   
   70           String randString = Long.toString(rand);
   71   
   72           if (varName != null) {
   73               JMeterVariables vars = getVariables();
   74               final String varTrim = varName.execute().trim();
   75               if (vars != null && varTrim.length() > 0){// vars will be null on TestPlan
   76                   vars.put(varTrim, randString);
   77               }
   78           }
   79   
   80           return randString;
   81   
   82       }
   83   
   84       /**
   85        * Set the parameters for the function.
   86        *
   87        * @see Function#setParameters(Collection)
   88        */
   89       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
   90           checkParameterCount(parameters, 2, 3);
   91           Object[] values = parameters.toArray();
   92   
   93           minimum = (CompoundVariable) values[0];
   94           maximum = (CompoundVariable) values[1];
   95           if (values.length>2){
   96               varName = (CompoundVariable) values[2];
   97           } else {
   98               varName = null;
   99           }
  100   
  101       }
  102   
  103       /**
  104        * Get the invocation key for this function.
  105        *
  106        * @see Function#getReferenceKey()
  107        */
  108       public String getReferenceKey() {
  109           return KEY;
  110       }
  111   
  112       /**
  113        * Get the description of this function.
  114        *
  115        * @see Function#getArgumentDesc()
  116        */
  117       public List getArgumentDesc() {
  118           return desc;
  119       }
  120   
  121   }

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