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: 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: * @see Function#execute(SampleResult, Sampler)
36: */
37: abstract public String execute(SampleResult previousResult,
38: Sampler currentSampler) throws InvalidVariableException;
39:
40: public String execute() throws InvalidVariableException {
41: JMeterContext context = JMeterContextService.getContext();
42: SampleResult previousResult = context.getPreviousResult();
43: Sampler currentSampler = context.getCurrentSampler();
44: return execute(previousResult, currentSampler);
45: }
46:
47: /**
48: * @see Function#setParameters(Collection)
49: * Note: This may not be called (e.g. if no parameters are provided)
50: *
51: */
52: abstract public void setParameters(Collection parameters)
53: throws InvalidVariableException;
54:
55: /**
56: * @see Function#getReferenceKey()
57: */
58: abstract public String getReferenceKey();
59:
60: protected JMeterVariables getVariables() {
61: return JMeterContextService.getContext().getVariables();
62: }
63:
64: /*
65: * Utility method to check parameter counts
66: */
67: protected void checkParameterCount(Collection parameters, int min,
68: int max) throws InvalidVariableException {
69: int num = parameters.size();
70: if ((num > max) || (num < min)) {
71: throw new InvalidVariableException(
72: "Wrong number of parameters. Actual: " + num
73: + ". Expected: >= " + min + " and <= "
74: + max);
75: }
76: }
77: }
|