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.testelement;
020:
021: import java.io.IOException;
022: import java.io.Serializable;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.jmeter.NewDriver;
028: import org.apache.jmeter.config.Arguments;
029: import org.apache.jmeter.engine.event.LoopIterationEvent;
030: import org.apache.jmeter.services.FileServer;
031: import org.apache.jmeter.testelement.property.BooleanProperty;
032: import org.apache.jmeter.testelement.property.JMeterProperty;
033: import org.apache.jmeter.testelement.property.TestElementProperty;
034: import org.apache.jmeter.threads.ThreadGroup;
035: import org.apache.jorphan.logging.LoggingManager;
036: import org.apache.jorphan.util.JOrphanUtils;
037: import org.apache.log.Logger;
038:
039: public class TestPlan extends AbstractTestElement implements
040: Serializable, TestListener {
041: private static final Logger log = LoggingManager
042: .getLoggerForClass();
043:
044: // does not appear to be needed
045: // private final static String THREAD_GROUPS = "TestPlan.thread_groups"; //$NON-NLS-1$
046:
047: private final static String FUNCTIONAL_MODE = "TestPlan.functional_mode"; //$NON-NLS-1$
048:
049: private final static String USER_DEFINED_VARIABLES = "TestPlan.user_defined_variables"; //$NON-NLS-1$
050:
051: private final static String SERIALIZE_THREADGROUPS = "TestPlan.serialize_threadgroups"; //$NON-NLS-1$
052:
053: private final static String CLASSPATHS = "TestPlan.user_define_classpath"; //$NON-NLS-1$
054: private static final String CLASSPATH_SEPARATOR = ","; //$NON-NLS-1$
055:
056: private final static String BASEDIR = "basedir";
057:
058: private transient List threadGroups = new LinkedList();
059:
060: // Does not appear to be needed
061: // private transient List configs = new LinkedList();
062:
063: // // Does not appear to be needed
064: // private static List itemsCanAdd = new LinkedList();
065:
066: // Does not appear to be needed
067: // private static TestPlan plan;
068:
069: // There's only 1 test plan, so can cache the mode here
070: private static boolean functionalMode = false;
071:
072: static {
073: // WARNING! This String value must be identical to the String value
074: // returned in org.apache.jmeter.threads.ThreadGroup.getClassLabel()
075: // method. If it's not you will not be able to add a Thread Group
076: // element to a Test Plan.
077:
078: // Does not appear to be needed
079: // itemsCanAdd.add(JMeterUtils.getResString("threadgroup")); //$NON-NLS-1$
080: }
081:
082: public TestPlan() {
083: // this("Test Plan");
084: // setFunctionalMode(false);
085: // setSerialized(false);
086: }
087:
088: public TestPlan(String name) {
089: setName(name);
090: // setFunctionalMode(false);
091: // setSerialized(false);
092:
093: // Does not appear to be needed
094: // setProperty(new CollectionProperty(THREAD_GROUPS, threadGroups));
095: }
096:
097: // create transient item
098: private Object readResolve() throws java.io.ObjectStreamException {
099: threadGroups = new LinkedList();
100: return this ;
101: }
102:
103: public void prepareForPreCompile() {
104: getVariables().setRunningVersion(true);
105: }
106:
107: /**
108: * Fetches the functional mode property
109: *
110: * @return functional mode
111: */
112: public boolean isFunctionalMode() {
113: return getPropertyAsBoolean(FUNCTIONAL_MODE);
114: }
115:
116: public void setUserDefinedVariables(Arguments vars) {
117: setProperty(new TestElementProperty(USER_DEFINED_VARIABLES,
118: vars));
119: }
120:
121: public JMeterProperty getUserDefinedVariablesAsProperty() {
122: return getProperty(USER_DEFINED_VARIABLES);
123: }
124:
125: public String getBasedir() {
126: return getPropertyAsString(BASEDIR);
127: }
128:
129: // Does not appear to be used yet
130: public void setBasedir(String b) {
131: setProperty(BASEDIR, b);
132: }
133:
134: public Arguments getArguments() {
135: return getVariables();
136: }
137:
138: public Map getUserDefinedVariables() {
139: Arguments args = getVariables();
140: return args.getArgumentsAsMap();
141: }
142:
143: private Arguments getVariables() {
144: Arguments args = (Arguments) getProperty(USER_DEFINED_VARIABLES)
145: .getObjectValue();
146: if (args == null) {
147: args = new Arguments();
148: setUserDefinedVariables(args);
149: }
150: return args;
151: }
152:
153: public void setFunctionalMode(boolean funcMode) {
154: setProperty(new BooleanProperty(FUNCTIONAL_MODE, funcMode));
155: functionalMode = funcMode;
156: }
157:
158: /**
159: * Gets the static copy of the functional mode
160: *
161: * @return mode
162: */
163: public static boolean getFunctionalMode() {
164: return functionalMode;
165: }
166:
167: public void setSerialized(boolean serializeTGs) {
168: setProperty(new BooleanProperty(SERIALIZE_THREADGROUPS,
169: serializeTGs));
170: }
171:
172: /**
173: * Set the classpath for the test plan
174: * @param text
175: */
176: public void setTestPlanClasspath(String text) {
177: setProperty(CLASSPATHS, text);
178: }
179:
180: public void setTestPlanClasspathArray(String[] text) {
181: StringBuffer cat = new StringBuffer();
182: for (int idx = 0; idx < text.length; idx++) {
183: if (idx > 0) {
184: cat.append(CLASSPATH_SEPARATOR);
185: }
186: cat.append(text[idx]);
187: }
188: this .setTestPlanClasspath(cat.toString());
189: }
190:
191: public String[] getTestPlanClasspathArray() {
192: return JOrphanUtils.split(this .getTestPlanClasspath(),
193: CLASSPATH_SEPARATOR);
194: }
195:
196: /**
197: * Returns the classpath
198: * @return classpath
199: */
200: public String getTestPlanClasspath() {
201: return getPropertyAsString(CLASSPATHS);
202: }
203:
204: /**
205: * Fetch the serialize threadgroups property
206: *
207: * @return serialized setting
208: */
209: public boolean isSerialized() {
210: return getPropertyAsBoolean(SERIALIZE_THREADGROUPS);
211: }
212:
213: public void addParameter(String name, String value) {
214: getVariables().addArgument(name, value);
215: }
216:
217: // Does not appear to be needed
218: // public static TestPlan createTestPlan(String name) {
219: // if (plan == null) {
220: // if (name == null) {
221: // plan = new TestPlan();
222: // } else {
223: // plan = new TestPlan(name);
224: // }
225: // plan.setProperty(new StringProperty(TestElement.GUI_CLASS,
226: // "org.apache.jmeter.control.gui.TestPlanGui")); //$NON-NLS-1$
227: // }
228: // return plan;
229: // }
230:
231: public void addTestElement(TestElement tg) {
232: super .addTestElement(tg);
233: if (tg instanceof ThreadGroup && !isRunningVersion()) {
234: addThreadGroup((ThreadGroup) tg);
235: }
236: }
237:
238: // // Does not appear to be needed
239: // public void addJMeterComponent(TestElement child) {
240: // if (child instanceof ThreadGroup) {
241: // addThreadGroup((ThreadGroup) child);
242: // }
243: // }
244:
245: // /**
246: // * Gets the ThreadGroups attribute of the TestPlan object.
247: // *
248: // * @return the ThreadGroups value
249: // */
250: // // Does not appear to be needed
251: // public Collection getThreadGroups() {
252: // return threadGroups;
253: // }
254:
255: // /**
256: // * Adds a feature to the ConfigElement attribute of the TestPlan object.
257: // *
258: // * @param c
259: // * the feature to be added to the ConfigElement attribute
260: // */
261: // // Does not appear to be needed
262: // public void addConfigElement(ConfigElement c) {
263: // configs.add(c);
264: // }
265:
266: /**
267: * Adds a feature to the ThreadGroup attribute of the TestPlan object.
268: *
269: * @param group
270: * the feature to be added to the ThreadGroup attribute
271: */
272: public void addThreadGroup(ThreadGroup group) {
273: threadGroups.add(group);
274: }
275:
276: /*
277: * (non-Javadoc)
278: *
279: * @see org.apache.jmeter.testelement.TestListener#testEnded()
280: */
281: public void testEnded() {
282: try {
283: FileServer.getFileServer().closeFiles();
284: } catch (IOException e) {
285: log.error("Problem closing files at end of test", e);
286: }
287: }
288:
289: /*
290: * (non-Javadoc)
291: *
292: * @see org.apache.jmeter.testelement.TestListener#testEnded(java.lang.String)
293: */
294: public void testEnded(String host) {
295: testEnded();
296:
297: }
298:
299: /*
300: * (non-Javadoc)
301: *
302: * @see org.apache.jmeter.testelement.TestListener#testIterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
303: */
304: public void testIterationStart(LoopIterationEvent event) {
305: }
306:
307: /*
308: * (non-Javadoc)
309: *
310: * @see org.apache.jmeter.testelement.TestListener#testStarted()
311: */
312: public void testStarted() {
313: if (getBasedir() != null && getBasedir().length() > 0) {
314: try {
315: FileServer.getFileServer().setBasedir(
316: FileServer.getFileServer().getBaseDir()
317: + getBasedir());
318: } catch (IOException e) {
319: log.error("Failed to set file server base dir with "
320: + getBasedir(), e);
321: }
322: }
323: // we set the classpath
324: String[] paths = this .getTestPlanClasspathArray();
325: for (int idx = 0; idx < paths.length; idx++) {
326: NewDriver.addURL(paths[idx]);
327: log.info("add " + paths[idx] + " to classpath");
328: }
329: }
330:
331: /*
332: * (non-Javadoc)
333: *
334: * @see org.apache.jmeter.testelement.TestListener#testStarted(java.lang.String)
335: */
336: public void testStarted(String host) {
337: testStarted();
338: }
339:
340: }
|