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.util.JMeterUtils;
   29   
   30   /**
   31    * Function to get a JMeter property, or a default. Does not offer the option to
   32    * store the value, as it is just as easy to refetch it. This is a
   33    * specialisation of the __property() function to make it simpler to use for
   34    * ThreadGroup GUI etc. The name is also shorter.
   35    *
   36    * Parameters: - property name - default value (optional; defaults to "1")
   37    *
   38    * Usage:
   39    *
   40    * Define the property in jmeter.properties, or on the command-line: java ...
   41    * -Jpropname=value
   42    *
   43    * Retrieve the value in the appropriate GUI by using the string:
   44    * ${__P(propname)} $(__P(propname,default)}
   45    *
   46    * Returns: - the property value, but if not found - the default value, but if
   47    * not present - "1" (suitable for use in ThreadGroup GUI)
   48    *
   49    */
   50   public class Property2 extends AbstractFunction {
   51   
   52       private static final List desc = new LinkedList();
   53   
   54       private static final String KEY = "__P"; //$NON-NLS-1$
   55   
   56       // Number of parameters expected - used to reject invalid calls
   57       private static final int MIN_PARAMETER_COUNT = 1;
   58   
   59       private static final int MAX_PARAMETER_COUNT = 2;
   60       static {
   61           desc.add(JMeterUtils.getResString("property_name_param")); //$NON-NLS-1$
   62           desc.add(JMeterUtils.getResString("property_default_param")); //$NON-NLS-1$
   63       }
   64   
   65       private Object[] values;
   66   
   67       public Property2() {
   68       }
   69   
   70       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   71               throws InvalidVariableException {
   72           String propertyName = ((CompoundVariable) values[0]).execute();
   73   
   74           String propertyDefault = "1"; //$NON-NLS-1$
   75           if (values.length > 1) { // We have a default
   76               propertyDefault = ((CompoundVariable) values[1]).execute();
   77           }
   78   
   79           String propertyValue = JMeterUtils.getPropDefault(propertyName, propertyDefault);
   80   
   81           return propertyValue;
   82   
   83       }
   84   
   85       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
   86           checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
   87           values = parameters.toArray();
   88       }
   89   
   90       public String getReferenceKey() {
   91           return KEY;
   92       }
   93   
   94       public List getArgumentDesc() {
   95           return desc;
   96       }
   97   
   98   }

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