001: //$Header$
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019:
020: package org.apache.jmeter.testelement;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024: import java.util.Collection;
025: import java.util.LinkedList;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.jmeter.config.Arguments;
030: import org.apache.jmeter.config.ConfigElement;
031: import org.apache.jmeter.engine.event.LoopIterationEvent;
032: import org.apache.jmeter.services.FileServer;
033: import org.apache.jmeter.testelement.property.CollectionProperty;
034: import org.apache.jmeter.testelement.property.StringProperty;
035: import org.apache.jmeter.testelement.property.TestElementProperty;
036: import org.apache.jmeter.threads.ThreadGroup;
037: import org.apache.jmeter.util.JMeterUtils;
038: import org.apache.jorphan.logging.LoggingManager;
039: import org.apache.log.Logger;
040:
041: /**
042: * @author Peter Lin created 8/11
043: * @version $Revision: 493793 $ Last updated: $Date: 2007-01-07 18:19:27 +0000 (Sun, 07 Jan 2007) $
044: */
045: public class ReportPlan extends AbstractTestElement implements
046: Serializable, TestListener {
047: private static Logger log = LoggingManager.getLoggerForClass();
048:
049: public final static String REPORT_PAGE = "ReportPlan.report_page";
050:
051: public final static String USER_DEFINED_VARIABLES = "ReportPlan.user_defined_variables";
052:
053: public final static String COMMENTS = "ReportPlan.comments";
054:
055: public final static String BASEDIR = "ReportPlan.basedir";
056:
057: private transient List reportPages = new LinkedList();
058:
059: private transient List configs = new LinkedList();
060:
061: private static List itemsCanAdd = new LinkedList();
062:
063: private static ReportPlan plan;
064:
065: // There's only 1 test plan, so can cache the mode here
066: private static boolean functionalMode = false;
067:
068: static {
069: itemsCanAdd.add(JMeterUtils.getResString("report_page"));
070: }
071:
072: public ReportPlan() {
073: this (JMeterUtils.getResString("report_plan"));
074: }
075:
076: public ReportPlan(String name) {
077: setName(name);
078: setProperty(new CollectionProperty(REPORT_PAGE, reportPages));
079: }
080:
081: public void setUserDefinedVariables(Arguments vars) {
082: setProperty(new TestElementProperty(USER_DEFINED_VARIABLES,
083: vars));
084: }
085:
086: public String getBasedir() {
087: return getPropertyAsString(BASEDIR);
088: }
089:
090: public void setBasedir(String b) {
091: setProperty(BASEDIR, b);
092: }
093:
094: public Map getUserDefinedVariables() {
095: Arguments args = getVariables();
096: return args.getArgumentsAsMap();
097: }
098:
099: private Arguments getVariables() {
100: Arguments args = (Arguments) getProperty(USER_DEFINED_VARIABLES)
101: .getObjectValue();
102: if (args == null) {
103: args = new Arguments();
104: setUserDefinedVariables(args);
105: }
106: return args;
107: }
108:
109: /**
110: * Gets the static copy of the functional mode
111: *
112: * @return mode
113: */
114: public static boolean getFunctionalMode() {
115: return functionalMode;
116: }
117:
118: public void addParameter(String name, String value) {
119: getVariables().addArgument(name, value);
120: }
121:
122: public static ReportPlan createReportPlan(String name) {
123: if (plan == null) {
124: if (name == null) {
125: plan = new ReportPlan();
126: } else {
127: plan = new ReportPlan(name);
128: }
129: plan.setProperty(new StringProperty(TestElement.GUI_CLASS,
130: "org.apache.jmeter.control.gui.ReportGui"));
131: }
132: return plan;
133: }
134:
135: public void addTestElement(TestElement tg) {
136: super .addTestElement(tg);
137: if (tg instanceof ThreadGroup && !isRunningVersion()) {
138: addReportPage((ThreadGroup) tg);
139: }
140: }
141:
142: public void addJMeterComponent(TestElement child) {
143: if (child instanceof ThreadGroup) {
144: addReportPage((ThreadGroup) child);
145: }
146: }
147:
148: /**
149: * Gets the ThreadGroups attribute of the TestPlan object.
150: *
151: * @return the ThreadGroups value
152: */
153: public Collection getReportPages() {
154: return reportPages;
155: }
156:
157: /**
158: * Adds a feature to the ConfigElement attribute of the TestPlan object.
159: *
160: * @param c
161: * the feature to be added to the ConfigElement attribute
162: */
163: public void addConfigElement(ConfigElement c) {
164: configs.add(c);
165: }
166:
167: /**
168: * Adds a feature to the ThreadGroup attribute of the TestPlan object.
169: *
170: * @param group
171: * the feature to be added to the ThreadGroup attribute
172: */
173: public void addReportPage(ThreadGroup group) {
174: reportPages.add(group);
175: }
176:
177: /*
178: * (non-Javadoc)
179: *
180: * @see org.apache.jmeter.testelement.TestListener#testEnded()
181: */
182: public void testEnded() {
183: try {
184: FileServer.getFileServer().closeFiles();
185: } catch (IOException e) {
186: log.error("Problem closing files at end of test", e);
187: }
188: }
189:
190: /*
191: * (non-Javadoc)
192: *
193: * @see org.apache.jmeter.testelement.TestListener#testEnded(java.lang.String)
194: */
195: public void testEnded(String host) {
196: testEnded();
197:
198: }
199:
200: /*
201: * (non-Javadoc)
202: *
203: * @see org.apache.jmeter.testelement.TestListener#testIterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
204: */
205: public void testIterationStart(LoopIterationEvent event) {
206: }
207:
208: /*
209: * (non-Javadoc)
210: *
211: * @see org.apache.jmeter.testelement.TestListener#testStarted()
212: */
213: public void testStarted() {
214: if (getBasedir() != null && getBasedir().length() > 0) {
215: try {
216: FileServer.getFileServer().setBasedir(
217: FileServer.getFileServer().getBaseDir()
218: + getBasedir());
219: } catch (IOException e) {
220: log.error("Failed to set file server base dir with "
221: + getBasedir(), e);
222: }
223: }
224: }
225:
226: /*
227: * (non-Javadoc)
228: *
229: * @see org.apache.jmeter.testelement.TestListener#testStarted(java.lang.String)
230: */
231: public void testStarted(String host) {
232: testStarted();
233: }
234: }
|