01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: // @see PackageTest for unit tests
22:
23: import java.io.Serializable;
24: import java.util.Collection;
25: import java.util.LinkedList;
26: import java.util.List;
27:
28: import org.apache.jmeter.engine.util.CompoundVariable;
29: import org.apache.jmeter.samplers.SampleResult;
30: import org.apache.jmeter.samplers.Sampler;
31: import org.apache.jmeter.util.JMeterUtils;
32:
33: /**
34: * Function to evaluate a string which may contain variable or function references.
35: *
36: * Parameter: string to be evaluated
37: *
38: * Returns: the evaluated value
39: *
40: */
41: public class EvalFunction extends AbstractFunction implements
42: Serializable {
43:
44: private static final long serialVersionUID = 1L;
45:
46: private static final List desc = new LinkedList();
47:
48: private static final String KEY = "__eval"; //$NON-NLS-1$
49:
50: // Number of parameters expected - used to reject invalid calls
51: private static final int MIN_PARAMETER_COUNT = 1;
52: private static final int MAX_PARAMETER_COUNT = 1;
53:
54: static {
55: desc.add(JMeterUtils.getResString("eval_name_param")); //$NON-NLS-1$
56: }
57:
58: private Object[] values;
59:
60: public EvalFunction() {
61: }
62:
63: public Object clone() throws CloneNotSupportedException {
64: return super .clone();
65: }
66:
67: public synchronized String execute(SampleResult previousResult,
68: Sampler currentSampler) throws InvalidVariableException {
69: String parameter = ((CompoundVariable) values[0]).execute();
70: CompoundVariable cv = new CompoundVariable(parameter);
71: return cv.execute();
72: }
73:
74: public synchronized void setParameters(Collection parameters)
75: throws InvalidVariableException {
76:
77: values = parameters.toArray();
78:
79: if ((values.length < MIN_PARAMETER_COUNT)
80: || (values.length > MAX_PARAMETER_COUNT)) {
81: throw new InvalidVariableException(
82: "Parameter Count not between "
83: + MIN_PARAMETER_COUNT + " & "
84: + MAX_PARAMETER_COUNT);
85: }
86:
87: }
88:
89: public String getReferenceKey() {
90: return KEY;
91: }
92:
93: public List getArgumentDesc() {
94: return desc;
95: }
96:
97: }
|