View Javadoc

1   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2   // This file is copied from JUnit with some modification.
3   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4   package junit.framework;
5   
6   import java.util.Enumeration;
7   import java.util.Vector;
8   
9   /**
10   * A <code>TestSuite</code> is a <code>Composite</code> of Tests. It runs a collection of test cases. Here is an example
11   * using the dynamic test definition.
12   * 
13   * <pre>
14   * TestSuite suite = new TestSuite();
15   * suite.addTest(new MathTest(&quot;testAdd&quot;));
16   * suite.addTest(new MathTest(&quot;testDivideByZero&quot;));
17   * </pre>
18   * 
19   * Alternatively, a TestSuite can extract the tests to be run automatically. To do so you pass the class of your
20   * TestCase class to the TestSuite constructor.
21   * 
22   * <pre>
23   * TestSuite suite = new TestSuite(MathTest.class);
24   * </pre>
25   * 
26   * This constructor creates a suite with all the methods starting with "test" that take no arguments.
27   * 
28   * @see Test
29   */
30  public class TestSuite implements Test {
31  
32  	private Vector fTests = new Vector(10);
33  
34  	private String fName;
35  
36  	/**
37  	 * Constructs an empty TestSuite.
38  	 */
39  	public TestSuite() {
40  	}
41  
42  	/**
43  	 * Constructs a TestSuite from the given class with the given name.
44  	 * 
45  	 * @see TestSuite#TestSuite(Class)
46  	 */
47  	public TestSuite(Class theClass, String name) {
48  		this(theClass);
49  		setName(name);
50  	}
51  
52  	/**
53  	 * Constructs a TestSuite from the given class. Adds all the methods starting with "test" as test cases to the
54  	 * suite. Parts of this method was written at 2337 meters in the Hüffihütte, Kanton Uri
55  	 */
56  	public TestSuite(final Class theClass) {
57  		if (TestCase.class.isAssignableFrom(theClass)) {
58  			fName = theClass.getName();
59  
60  			TestCase test;
61  			try {
62  				test = (TestCase) theClass.newInstance();
63  				TestMethod[] methods = test.getTestMethods();
64  
65  				if (methods != null) {
66  					for (int i = 0; i < methods.length; i++) {
67  						addTestMethod(methods[i], theClass);
68  					}
69  				}
70  				if (fTests.size() == 0) {
71  					addTest(warning("No tests found in " + theClass.getName()));
72  				}
73  			} catch (InstantiationException e) {
74  				// TODO Auto-generated catch block
75  				e.printStackTrace();
76  			} catch (IllegalAccessException e) {
77  				// TODO Auto-generated catch block
78  				e.printStackTrace();
79  			}
80  		}
81  	}
82  
83  	/**
84  	 * Constructs an empty TestSuite.
85  	 */
86  	public TestSuite(String name) {
87  		setName(name);
88  	}
89  
90  	/**
91  	 * Adds a test to the suite.
92  	 */
93  	public void addTest(Test test) {
94  		fTests.addElement(test);
95  	}
96  
97  	/**
98  	 * Adds the tests from the given class to the suite
99  	 */
100 	public void addTestSuite(Class testClass) {
101 		addTest(new TestSuite(testClass));
102 	}
103 
104 	private void addTestMethod(TestMethod m, Class theClass) {
105 		addTest(createTest(theClass, m));
106 	}
107 
108 	/**
109 	 * ...as the moon sets over the early morning Merlin, Oregon mountains, our intrepid adventurers type... --- Just in
110 	 * case if sombody calls this.
111 	 */
112 	static public Test createTest(Class theClass, TestMethod testMethod) {
113 		Test test = null;
114 		try {
115 			test = (Test) theClass.newInstance();
116 			if (test instanceof TestCase) {
117 				((TestCase) test).setTestMethod(testMethod);
118 			}
119 		} catch (InstantiationException e) {
120 			return (warning("Cannot instantiate test case: " + testMethod + " (" + exceptionToString(e) + ")"));
121 		} catch (IllegalAccessException e) {
122 			return (warning("Cannot access test case: " + testMethod + " (" + exceptionToString(e) + ")"));
123 		}
124 		return (Test) test;
125 	}
126 
127 	/**
128 	 * Converts the stack trace into a string
129 	 */
130 	private static String exceptionToString(Throwable t) {
131 		// StringWriter stringWriter= new StringWriter();
132 		// PrintWriter writer= new PrintWriter(stringWriter);
133 		// t.printStackTrace(writer);
134 		// return stringWriter.toString();
135 		return t.getMessage();
136 
137 	}
138 
139 	/**
140 	 * Counts the number of test cases that will be run by this test.
141 	 */
142 	public int countTestCases() {
143 		int count = 0;
144 		for (Enumeration e = tests(); e.hasMoreElements();) {
145 			Test test = (Test) e.nextElement();
146 			count = count + test.countTestCases();
147 		}
148 		return count;
149 	}
150 
151 	/**
152 	 * Runs the tests and collects their result in a TestResult.
153 	 */
154 	public void run(TestResult result) {
155 		for (Enumeration e = tests(); e.hasMoreElements();) {
156 			if (result.shouldStop())
157 				break;
158 			Test test = (Test) e.nextElement();
159 			runTest(test, result);
160 		}
161 	}
162 
163 	public void runTest(Test test, TestResult result) {
164 		test.run(result);
165 	}
166 
167 	/**
168 	 * Returns the test at the given index
169 	 */
170 	public Test testAt(int index) {
171 		return (Test) fTests.elementAt(index);
172 	}
173 
174 	/**
175 	 * Returns the number of tests in this suite
176 	 */
177 	public int testCount() {
178 		return fTests.size();
179 	}
180 
181 	/**
182 	 * Returns the tests as an enumeration
183 	 */
184 	public Enumeration tests() {
185 		return fTests.elements();
186 	}
187 
188 	/**
189 	 */
190 	public String toString() {
191 		if (getName() != null)
192 			return getName();
193 		return super.toString();
194 	}
195 
196 	/**
197 	 * Sets the name of the suite.
198 	 * 
199 	 * @param name
200 	 *            The name to set
201 	 */
202 	public void setName(String name) {
203 		fName = name;
204 	}
205 
206 	/**
207 	 * Returns the name of the suite. Not all test suites have a name and this method can return null.
208 	 */
209 	public String getName() {
210 		return fName;
211 	}
212 
213 	/**
214 	 * Returns a test which will fail and log a warning message.
215 	 */
216 	private static Test warning(final String message) {
217 		return new TestCase(new TestMethod("warning") {
218 			public void run(TestCase testCase) throws Throwable {
219 			}
220 		}) {
221 			protected void runTest() {
222 				fail(message);
223 			}
224 
225 			public TestMethod[] getTestMethods() {
226 				return null;
227 			}
228 		};
229 	}
230 
231 	// public TestMethod[] getTestMethods() {
232 	// Vector testMethodsVector = new Vector();
233 	// for (int i = 0; i < fTests.size(); i++) {
234 	// Test test = (Test)fTests.elementAt(i);
235 	// for (int j = 0; j < test.getTestMethods().length; j++) {
236 	// testMethodsVector.addElement(test.getTestMethods()[j]);
237 	// }
238 	// }
239 	// TestMethod[] testMethods = new TestMethod[testMethodsVector.size()];
240 	// testMethodsVector.copyInto(testMethods);
241 	// return testMethods;
242 	// }
243 }