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   
   23   import org.apache.jmeter.samplers.SampleResult;
   24   import org.apache.jmeter.samplers.Sampler;
   25   import org.apache.jmeter.threads.JMeterContext;
   26   import org.apache.jmeter.threads.JMeterContextService;
   27   import org.apache.jmeter.threads.JMeterVariables;
   28   
   29   /**
   30    * Provides common methods for all functions
   31    */
   32   public abstract class AbstractFunction implements Function {
   33   
   34       /**
   35        * <p><b>
   36        * N.B. setParameters() and execute() are called from different threads, 
   37        * so both must be synchronized unless there are no parameters to save
   38        * </b></p>
   39        * @see Function#execute(SampleResult, Sampler)
   40        */
   41       abstract public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException;
   42   
   43       public String execute() throws InvalidVariableException {
   44           JMeterContext context = JMeterContextService.getContext();
   45           SampleResult previousResult = context.getPreviousResult();
   46           Sampler currentSampler = context.getCurrentSampler();
   47           return execute(previousResult, currentSampler);
   48       }
   49   
   50       /**
   51        * 
   52        * <p><b>
   53        * N.B. setParameters() and execute() are called from different threads, 
   54        * so both must be synchronized unless there are no parameters to save
   55        * </b></p>
   56        * 
   57        * @see Function#setParameters(Collection)
   58        * <br/>
   59        * Note: This is always called even if no parameters are provided 
   60        * (versions of JMeter after 2.3.1)
   61        */
   62       abstract public void setParameters(Collection parameters) throws InvalidVariableException;
   63   
   64       /**
   65        * @see Function#getReferenceKey()
   66        */
   67       abstract public String getReferenceKey();
   68   
   69       protected JMeterVariables getVariables() {
   70           return JMeterContextService.getContext().getVariables();
   71       }
   72       
   73       /**
   74        * Utility method to check parameter counts.
   75        * 
   76        * @param parameters collection of parameters
   77        * @param min minimum number of parameters allowed
   78        * @param max maximum number of parameters allowed
   79        * 
   80        * @throws InvalidVariableException if the number of parameters is incorrect
   81        */
   82       protected void checkParameterCount(Collection parameters, int min, int max) 
   83           throws InvalidVariableException
   84       {
   85           int num = parameters.size();
   86           if ((num > max) || (num < min)) {
   87               throw new InvalidVariableException(
   88                       getReferenceKey() +
   89                       " called with wrong number of parameters. Actual: "+num+
   90                       (
   91                           min==max ? 
   92                           ". Expected: "+min+"." 
   93                           : ". Expected: >= "+min+" and <= "+max
   94                       )
   95                       );
   96           }
   97       }
   98   
   99       /**
  100        * Utility method to check parameter counts.
  101        * 
  102        * @param parameters collection of parameters
  103        * @param count number of parameters expected
  104        * 
  105        * @throws InvalidVariableException if the number of parameters is incorrect
  106        */
  107       protected void checkParameterCount(Collection parameters, int count) 
  108           throws InvalidVariableException
  109       {
  110           int num = parameters.size();
  111           if (num != count) {
  112               throw new InvalidVariableException(
  113                       getReferenceKey() +
  114                       " called with wrong number of parameters. Actual: "+num+". Expected: "+count+"." 
  115                      );
  116           }
  117       }
  118   
  119       /**
  120        * Utility method to check parameter counts.
  121        * 
  122        * @param parameters collection of parameters
  123        * @param minimum number of parameters expected
  124        * 
  125        * @throws InvalidVariableException if the number of parameters is incorrect
  126        */
  127       protected void checkMinParameterCount(Collection parameters, int minimum) 
  128           throws InvalidVariableException
  129       {
  130           int num = parameters.size();
  131           if (num < minimum) {
  132               throw new InvalidVariableException(
  133                       getReferenceKey() +
  134                       " called with wrong number of parameters. Actual: "+num+". Expected at least: "+minimum+"." 
  135                      );
  136           }
  137       }
  138   }

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