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 a Random function which returns a random long integer between a min
034: * (first argument) and a max (second argument).
035: *
036: * @author <a href="mailto:sjkwadzo@praize.com">Jonathan Kwadzo</a>
037: */
038: public class Random extends AbstractFunction implements Serializable {
039:
040: private static final long serialVersionUID = 1L;
041:
042: private static final List desc = new LinkedList();
043:
044: private static final String KEY = "__Random"; //$NON-NLS-1$
045:
046: static {
047: desc.add(JMeterUtils.getResString("minimum_param")); //$NON-NLS-1$
048: desc.add(JMeterUtils.getResString("maximum_param")); //$NON-NLS-1$
049: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
050: }
051:
052: private transient CompoundVariable varName, minimum, maximum;
053:
054: /**
055: * No-arg constructor.
056: */
057: public Random() {
058: }
059:
060: public Object clone() throws CloneNotSupportedException {
061: return super .clone();
062: }
063:
064: /**
065: * Execute the function.
066: *
067: * @see Function#execute(SampleResult, Sampler)
068: */
069: public synchronized String execute(SampleResult previousResult,
070: Sampler currentSampler) throws InvalidVariableException {
071:
072: JMeterVariables vars = getVariables();
073:
074: long min = Long.parseLong(minimum.execute().trim());
075: long max = Long.parseLong(maximum.execute().trim());
076:
077: long rand = min + (long) (Math.random() * (max - min + 1));
078:
079: String randString = Long.toString(rand);
080: vars.put(varName.execute(), randString);
081:
082: return randString;
083:
084: }
085:
086: /**
087: * Set the parameters for the function.
088: *
089: * @see Function#setParameters(Collection)
090: */
091: public synchronized void setParameters(Collection parameters)
092: throws InvalidVariableException {
093: Object[] values = parameters.toArray();
094:
095: if (values.length < 3) {
096: throw new InvalidVariableException();
097: }
098: varName = (CompoundVariable) values[2];
099: minimum = (CompoundVariable) values[0];
100: maximum = (CompoundVariable) values[1];
101:
102: }
103:
104: /**
105: * Get the invocation key for this function.
106: *
107: * @see Function#getReferenceKey()
108: */
109: public String getReferenceKey() {
110: return KEY;
111: }
112:
113: /**
114: * Get the description of this function.
115: *
116: * @see Function#getArgumentDesc()
117: */
118: public List getArgumentDesc() {
119: return desc;
120: }
121:
122: }
|