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 longSum function that adds two or more long values.
   33    * @see IntSum
   34    */
   35   public class LongSum extends AbstractFunction {
   36   
   37       private static final List desc = new LinkedList();
   38   
   39       private static final String KEY = "__longSum"; //$NON-NLS-1$
   40   
   41       static {
   42           desc.add(JMeterUtils.getResString("longsum_param_1")); //$NON-NLS-1$
   43           desc.add(JMeterUtils.getResString("longsum_param_2")); //$NON-NLS-1$
   44           desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
   45       }
   46   
   47       private Object[] values;
   48   
   49       /**
   50        * No-arg constructor.
   51        */
   52       public LongSum() {
   53       }
   54   
   55       /**
   56        * Execute the function.
   57        *
   58        * @see Function#execute(SampleResult, Sampler)
   59        */
   60       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   61               throws InvalidVariableException {
   62   
   63           JMeterVariables vars = getVariables();
   64   
   65           long sum = 0;
   66           String varName = ((CompoundVariable) values[values.length - 1]).execute().trim();
   67   
   68           for (int i = 0; i < values.length - 1; i++) {
   69               sum += Long.parseLong(((CompoundVariable) values[i]).execute());
   70           }
   71   
   72           try {
   73               sum += Long.parseLong(varName);
   74               varName = null; // there is no variable name
   75           } catch (NumberFormatException ignored) {
   76           }
   77   
   78           String totalString = Long.toString(sum);
   79           if (vars != null && varName != null && varName.length() > 0){// vars will be null on TestPlan
   80               vars.put(varName, totalString);
   81           }
   82   
   83           return totalString;
   84   
   85       }
   86   
   87       /**
   88        * Set the parameters for the function.
   89        *
   90        * @see Function#setParameters(Collection)
   91        */
   92       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
   93           checkMinParameterCount(parameters, 2);
   94           values = parameters.toArray();
   95       }
   96   
   97       /**
   98        * Get the invocation key for this function.
   99        *
  100        * @see Function#getReferenceKey()
  101        */
  102       public String getReferenceKey() {
  103           return KEY;
  104       }
  105   
  106       /**
  107        * Get the description of this function.
  108        *
  109        * @see Function#getArgumentDesc()
  110        */
  111       public List getArgumentDesc() {
  112           return desc;
  113       }
  114   }

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