001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.functions;
020:
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.apache.jmeter.engine.util.CompoundVariable;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.samplers.Sampler;
029: import org.apache.jmeter.threads.JMeterVariables;
030: import org.apache.jmeter.util.JMeterUtils;
031:
032: /**
033: * Provides an intSum function that adds two or more integer values.
034: *
035: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
036: */
037: public class IntSum extends AbstractFunction implements Serializable {
038:
039: private static final List desc = new LinkedList();
040:
041: private static final String KEY = "__intSum"; //$NON-NLS-1$
042:
043: static {
044: desc.add(JMeterUtils.getResString("intsum_param_1")); //$NON-NLS-1$
045: desc.add(JMeterUtils.getResString("intsum_param_2")); //$NON-NLS-1$
046: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
047: }
048:
049: private Object[] values;
050:
051: /**
052: * No-arg constructor.
053: */
054: public IntSum() {
055: }
056:
057: /**
058: * Clone this Add object.
059: *
060: * @return A new Add object.
061: * @throws CloneNotSupportedException
062: */
063: public Object clone() throws CloneNotSupportedException {
064: return super .clone();
065: }
066:
067: /**
068: * Execute the function.
069: *
070: * @see Function#execute(SampleResult, Sampler)
071: */
072: public synchronized String execute(SampleResult previousResult,
073: Sampler currentSampler) throws InvalidVariableException {
074:
075: JMeterVariables vars = getVariables();
076:
077: int sum = 0;
078: String varName = ((CompoundVariable) values[values.length - 1])
079: .execute();
080:
081: for (int i = 0; i < values.length - 1; i++) {
082: sum += Integer.parseInt(((CompoundVariable) values[i])
083: .execute());
084: }
085:
086: String totalString = Integer.toString(sum);
087: vars.put(varName, totalString);
088:
089: return totalString;
090:
091: }
092:
093: /**
094: * Set the parameters for the function.
095: *
096: * @see Function#setParameters(Collection)
097: */
098: public synchronized void setParameters(Collection parameters)
099: throws InvalidVariableException {
100: values = parameters.toArray();
101:
102: if (values.length < 3) {
103: throw new InvalidVariableException();
104: }
105:
106: }
107:
108: /**
109: * Get the invocation key for this function.
110: *
111: * @see Function#getReferenceKey()
112: */
113: public String getReferenceKey() {
114: return KEY;
115: }
116:
117: /**
118: * Get the description of this function.
119: *
120: * @see Function#getArgumentDesc()
121: */
122: public List getArgumentDesc() {
123: return desc;
124: }
125: }
|