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.config;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.jmeter.testelement.property.CollectionProperty;
028: import org.apache.jmeter.testelement.property.PropertyIterator;
029: import org.apache.jmeter.testelement.property.TestElementProperty;
030:
031: // Mark Walsh, 2002-08-03 add method:
032: // addArgument(String name, Object value, Object metadata)
033: // Modify methods:
034: // toString(), addEmptyArgument(), addArgument(String name, Object value)
035:
036: /**
037: * A set of Argument objects.
038: *
039: */
040: public class Arguments extends ConfigTestElement implements
041: Serializable {
042: /** The name of the property used to store the arguments. */
043: public static final String ARGUMENTS = "Arguments.arguments"; //$NON-NLS-1$
044:
045: /**
046: * Create a new Arguments object with no arguments.
047: */
048: public Arguments() {
049: setProperty(new CollectionProperty(ARGUMENTS, new ArrayList()));
050: }
051:
052: /**
053: * Get the arguments.
054: *
055: * @return the arguments
056: */
057: public CollectionProperty getArguments() {
058: return (CollectionProperty) getProperty(ARGUMENTS);
059: }
060:
061: /**
062: * Clear the arguments.
063: */
064: public void clear() {
065: super .clear();
066: setProperty(new CollectionProperty(ARGUMENTS, new ArrayList()));
067: }
068:
069: /**
070: * Set the list of arguments. Any existing arguments will be lost.
071: *
072: * @param arguments
073: * the new arguments
074: */
075: public void setArguments(List arguments) {
076: setProperty(new CollectionProperty(ARGUMENTS, arguments));
077: }
078:
079: /**
080: * Get the arguments as a Map. Each argument name is used as the key, and
081: * its value as the value.
082: *
083: * @return a new Map with String keys and values containing the arguments
084: */
085: public Map getArgumentsAsMap() {
086: PropertyIterator iter = getArguments().iterator();
087: Map argMap = new HashMap();
088: while (iter.hasNext()) {
089: Argument arg = (Argument) iter.next().getObjectValue();
090: // Because CollectionProperty.mergeIn will not prevent adding two
091: // properties of the same name, we need to select the first value so
092: // that this element's values prevail over defaults provided by
093: // configuration
094: // elements:
095: if (!argMap.containsKey(arg.getName()))
096: argMap.put(arg.getName(), arg.getValue());
097: }
098: return argMap;
099: }
100:
101: /**
102: * Add a new argument with the given name and value.
103: *
104: * @param name
105: * the name of the argument
106: * @param value
107: * the value of the argument
108: */
109: public void addArgument(String name, String value) {
110: addArgument(new Argument(name, value, null));
111: }
112:
113: /**
114: * Add a new argument.
115: *
116: * @param arg
117: * the new argument
118: */
119: public void addArgument(Argument arg) {
120: TestElementProperty newArg = new TestElementProperty(arg
121: .getName(), arg);
122: if (isRunningVersion()) {
123: this .setTemporary(newArg);
124: }
125: getArguments().addItem(newArg);
126: }
127:
128: /**
129: * Add a new argument with the given name, value, and metadata.
130: *
131: * @param name
132: * the name of the argument
133: * @param value
134: * the value of the argument
135: * @param metadata
136: * the metadata for the argument
137: */
138: public void addArgument(String name, String value, String metadata) {
139: addArgument(new Argument(name, value, metadata));
140: }
141:
142: /**
143: * Get a PropertyIterator of the arguments.
144: *
145: * @return an iteration of the arguments
146: */
147: public PropertyIterator iterator() {
148: return getArguments().iterator();
149: }
150:
151: /**
152: * Create a string representation of the arguments.
153: *
154: * @return the string representation of the arguments
155: */
156: public String toString() {
157: StringBuffer str = new StringBuffer();
158: PropertyIterator iter = getArguments().iterator();
159: while (iter.hasNext()) {
160: Argument arg = (Argument) iter.next().getObjectValue();
161: final String metaData = arg.getMetaData();
162: str.append(arg.getName());
163: if (metaData == null) {
164: str.append("="); //$NON-NLS-1$
165: } else {
166: str.append(metaData);
167: }
168: str.append(arg.getValue());
169: if (iter.hasNext()) {
170: str.append("&"); //$NON-NLS-1$
171: }
172: }
173: return str.toString();
174: }
175:
176: /**
177: * Remove the specified argument from the list.
178: *
179: * @param row
180: * the index of the argument to remove
181: */
182: public void removeArgument(int row) {
183: if (row < getArguments().size()) {
184: getArguments().remove(row);
185: }
186: }
187:
188: /**
189: * Remove the specified argument from the list.
190: *
191: * @param arg
192: * the argument to remove
193: */
194: public void removeArgument(Argument arg) {
195: PropertyIterator iter = getArguments().iterator();
196: while (iter.hasNext()) {
197: Argument item = (Argument) iter.next().getObjectValue();
198: if (arg.equals(item)) {
199: iter.remove();
200: }
201: }
202: }
203:
204: /**
205: * Remove the argument with the specified name.
206: *
207: * @param argName
208: * the name of the argument to remove
209: */
210: public void removeArgument(String argName) {
211: PropertyIterator iter = getArguments().iterator();
212: while (iter.hasNext()) {
213: Argument arg = (Argument) iter.next().getObjectValue();
214: if (arg.getName().equals(argName)) {
215: iter.remove();
216: }
217: }
218: }
219:
220: /**
221: * Remove all arguments from the list.
222: */
223: public void removeAllArguments() {
224: getArguments().clear();
225: }
226:
227: /**
228: * Add a new empty argument to the list. The new argument will have the
229: * empty string as its name and value, and null metadata.
230: */
231: public void addEmptyArgument() {
232: addArgument(new Argument("", "", null));
233: }
234:
235: /**
236: * Get the number of arguments in the list.
237: *
238: * @return the number of arguments
239: */
240: public int getArgumentCount() {
241: return getArguments().size();
242: }
243:
244: /**
245: * Get a single argument.
246: *
247: * @param row
248: * the index of the argument to return.
249: * @return the argument at the specified index, or null if no argument
250: * exists at that index.
251: */
252: public Argument getArgument(int row) {
253: Argument argument = null;
254:
255: if (row < getArguments().size()) {
256: argument = (Argument) getArguments().get(row)
257: .getObjectValue();
258: }
259:
260: return argument;
261: }
262: }
|