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.tools.ant.taskdefs.optional.junit;
020:
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023: import java.util.Properties;
024: import java.util.Vector;
025: import org.apache.tools.ant.Project;
026:
027: /**
028: * <p> Run a single JUnit test.
029: *
030: * <p> The JUnit test is actually run by {@link JUnitTestRunner}.
031: * So read the doc comments for that class :)
032: *
033: * @since Ant 1.2
034: *
035: * @see JUnitTask
036: * @see JUnitTestRunner
037: */
038: public class JUnitTest extends BaseTest implements Cloneable {
039:
040: /** the name of the test case */
041: private String name = null;
042:
043: /** the name of the result file */
044: private String outfile = null;
045:
046: // @todo this is duplicating TestResult information. Only the time is not
047: // part of the result. So we'd better derive a new class from TestResult
048: // and deal with it. (SB)
049: private long runs, failures, errors;
050: private long runTime;
051:
052: // Snapshot of the system properties
053: private Properties props = null;
054:
055: /** No arg constructor. */
056: public JUnitTest() {
057: }
058:
059: /**
060: * Constructor with name.
061: * @param name the name of the test.
062: */
063: public JUnitTest(String name) {
064: this .name = name;
065: }
066:
067: /**
068: * Constructor with options.
069: * @param name the name of the test.
070: * @param haltOnError if true halt the tests if there is an error.
071: * @param haltOnFailure if true halt the tests if there is a failure.
072: * @param filtertrace if true filter stack traces.
073: */
074: public JUnitTest(String name, boolean haltOnError,
075: boolean haltOnFailure, boolean filtertrace) {
076: this .name = name;
077: this .haltOnError = haltOnError;
078: this .haltOnFail = haltOnFailure;
079: this .filtertrace = filtertrace;
080: }
081:
082: /**
083: * Set the name of the test class.
084: * @param value the name to use.
085: */
086: public void setName(String value) {
087: name = value;
088: }
089:
090: /**
091: * Set the name of the output file.
092: * @param value the name of the output file to use.
093: */
094: public void setOutfile(String value) {
095: outfile = value;
096: }
097:
098: /**
099: * Get the name of the test class.
100: * @return the name of the test.
101: */
102: public String getName() {
103: return name;
104: }
105:
106: /**
107: * Get the name of the output file
108: *
109: * @return the name of the output file.
110: */
111: public String getOutfile() {
112: return outfile;
113: }
114:
115: /**
116: * Set the number of runs, failures and errors.
117: * @param runs the number of runs.
118: * @param failures the number of failures.
119: * @param errors the number of errors.
120: */
121: public void setCounts(long runs, long failures, long errors) {
122: this .runs = runs;
123: this .failures = failures;
124: this .errors = errors;
125: }
126:
127: /**
128: * Set the runtime.
129: * @param runTime the time in milliseconds.
130: */
131: public void setRunTime(long runTime) {
132: this .runTime = runTime;
133: }
134:
135: /**
136: * Get the number of runs.
137: * @return the number of runs.
138: */
139: public long runCount() {
140: return runs;
141: }
142:
143: /**
144: * Get the number of failures.
145: * @return the number of failures.
146: */
147: public long failureCount() {
148: return failures;
149: }
150:
151: /**
152: * Get the number of errors.
153: * @return the number of errors.
154: */
155: public long errorCount() {
156: return errors;
157: }
158:
159: /**
160: * Get the run time.
161: * @return the run time in milliseconds.
162: */
163: public long getRunTime() {
164: return runTime;
165: }
166:
167: /**
168: * Get the properties used in the test.
169: * @return the properties.
170: */
171: public Properties getProperties() {
172: return props;
173: }
174:
175: /**
176: * Set the properties to be used in the test.
177: * @param p the properties.
178: * This is a copy of the projects ant properties.
179: */
180: public void setProperties(Hashtable p) {
181: props = new Properties();
182: for (Enumeration e = p.keys(); e.hasMoreElements();) {
183: Object key = e.nextElement();
184: props.put(key, p.get(key));
185: }
186: }
187:
188: /**
189: * Check if this test should run based on the if and unless
190: * attributes.
191: * @param p the project to use to check if the if and unless
192: * properties exist in.
193: * @return true if this test or testsuite should be run.
194: */
195: public boolean shouldRun(Project p) {
196: if (ifProperty != null && p.getProperty(ifProperty) == null) {
197: return false;
198: } else if (unlessProperty != null
199: && p.getProperty(unlessProperty) != null) {
200: return false;
201: }
202:
203: return true;
204: }
205:
206: /**
207: * Get the formatters set for this test.
208: * @return the formatters as an array.
209: */
210: public FormatterElement[] getFormatters() {
211: FormatterElement[] fes = new FormatterElement[formatters.size()];
212: formatters.copyInto(fes);
213: return fes;
214: }
215:
216: /**
217: * Convenient method to add formatters to a vector
218: */
219: void addFormattersTo(Vector v) {
220: final int count = formatters.size();
221: for (int i = 0; i < count; i++) {
222: v.addElement(formatters.elementAt(i));
223: }
224: }
225:
226: /**
227: * @since Ant 1.5
228: * @return a clone of this test.
229: */
230: public Object clone() {
231: try {
232: JUnitTest t = (JUnitTest) super .clone();
233: t.props = props == null ? null : (Properties) props.clone();
234: t.formatters = (Vector) formatters.clone();
235: return t;
236: } catch (CloneNotSupportedException e) {
237: // plain impossible
238: return this;
239: }
240: }
241: }
|