KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > tivano > junit > ParametrizedTestSuiteTest


1 /*
2  * ParametrizedTestSuiteTest.java
3  * JUnit based test
4  *
5  * Created on 3. März 2002, 18:47
6  */

7
8 package de.tivano.junit;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import junit.framework.Test;
18 import junit.framework.TestCase;
19 import junit.framework.TestFailure;
20 import junit.framework.TestResult;
21 import junit.framework.TestSuite;
22
23 /**
24  *
25  * @author Richard Kunze
26  */

27 public class ParametrizedTestSuiteTest extends TestCase {
28     
29     public ParametrizedTestSuiteTest(java.lang.String JavaDoc testName) {
30         super(testName);
31     }
32     
33     public static void main(java.lang.String JavaDoc[] args) {
34         junit.textui.TestRunner.run(suite());
35     }
36     
37     public static Test suite() {
38         TestSuite suite = new TestSuite(ParametrizedTestSuiteTest.class);
39         
40         return suite;
41     }
42
43     private Properties JavaDoc testProperties;
44     
45     
46     /** Set up the fixture */
47     public void setUp() throws IOException JavaDoc {
48         InputStream JavaDoc data = getClass().getResourceAsStream(
49            "/de/tivano/junit/ParametrizationTestDummy.properties");
50         testProperties = new Properties JavaDoc();
51         testProperties.load(data);
52     }
53     
54     /** Test of warning method, of class de.tivano.junit.ParametrizedTestSuite. */
55     public void testWarning() {
56         ParametrizedTestSuite suite = new ParametrizedTestSuite(new Properties JavaDoc());
57         
58         Test warning = suite.warning("This is a warning");
59         
60         assertTrue("Expected the warning to be an instance of ParametrizedTestSuite.Warning",
61                    warning instanceof ParametrizedTestSuite.Warning);
62         assertEquals("Test name:", "warning", ((TestCase)warning).getName());
63         
64         TestResult result = ((TestCase)warning).run();
65         assertEquals("failure count:", 1, result.failureCount());
66         assertEquals("error count:", 0, result.errorCount());
67         TestFailure failure = (TestFailure)result.failures().nextElement();
68         assertEquals("failure message:", "This is a warning",
69                      failure.thrownException().getMessage());
70     }
71     
72     /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Properties)} constructor
73      * with a non-empty properties object.
74      */

75     public void testParametrizedTestSuite_Properties() {
76          ParametrizedTestSuite suite = new ParametrizedTestSuite(testProperties);
77          assertEquals("Test suite name", null, suite.getName());
78          assertEquals("Number of test cases", 0, suite.testCount());
79    }
80     
81     /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Properties)} constructor
82      * with an empty properties object.
83      * The test expects that a warning is added to the test suite.
84      */

85     public void testParametrizedTestSuite_Properties_Empty() {
86         ParametrizedTestSuite suite = new ParametrizedTestSuite(new Properties JavaDoc());
87         assertEquals("Number of test cases", 1, suite.testCount());
88         assertEquals("Expected the suite to contain only warnings",
89                      ParametrizedTestSuite.Warning.class.getName(),
90                      suite.testAt(0).getClass().getName());
91         TestResult result = new TestResult();
92         suite.testAt(0).run(result);
93         Enumeration JavaDoc failures = result.failures();
94         assertEquals("warning message:",
95                      "Test data definition for test suite 'null' is empty!",
96                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
97     }
98     
99      /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Properties)} constructor
100      * with an null properties object.
101      * The test expects that a warning is added to the test suite.
102      */

103     public void testParametrizedTestSuite_Properties_Null() {
104         ParametrizedTestSuite suite = new ParametrizedTestSuite((Properties JavaDoc)null);
105         assertEquals("Number of test cases", 1, suite.testCount());
106         assertEquals("Expected the suite to contain only warnings",
107                      ParametrizedTestSuite.Warning.class.getName(),
108                      suite.testAt(0).getClass().getName());
109         TestResult result = new TestResult();
110         suite.testAt(0).run(result);
111         Enumeration JavaDoc failures = result.failures();
112         assertEquals("warning message:",
113                      "No test data specified for test suite 'null'!",
114                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
115     }
116     
117     /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(String, Properties)} constructor
118      * with a non-empty properties object.
119      */

120     public void testParametrizedTestSuite_String_Properties() {
121          ParametrizedTestSuite suite = new ParametrizedTestSuite("some_name", testProperties);
122          assertEquals("Test suite name", "some_name", suite.getName());
123          assertEquals("Number of test cases", 0, suite.testCount());
124    }
125     
126     /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Properties)} constructor
127      * with an empty properties object.
128      * The test expects that a warning is added to the test suite.
129      */

130     public void testParametrizedTestSuite_String_Properties_Empty() {
131         ParametrizedTestSuite suite = new ParametrizedTestSuite("some_name", new Properties JavaDoc());
132         assertEquals("Test suite name", "some_name", suite.getName());
133         assertEquals("Number of test cases", 1, suite.testCount());
134         assertEquals("Expected the suite to contain only warnings",
135                      ParametrizedTestSuite.Warning.class.getName(),
136                      suite.testAt(0).getClass().getName());
137         TestResult result = new TestResult();
138         Enumeration JavaDoc failures = result.failures();
139         suite.testAt(0).run(result);
140         assertEquals("warning message:",
141                      "Test data definition for test suite 'some_name' is empty!",
142                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
143     }
144     
145      /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Properties)} constructor
146      * with an null properties object.
147      * The test expects that a warning is added to the test suite.
148      */

149     public void testParametrizedTestSuite_String_Properties_Null() {
150         ParametrizedTestSuite suite = new ParametrizedTestSuite("some_name", (Properties JavaDoc)null);
151         assertEquals("Test suite name", "some_name", suite.getName());
152         assertEquals("Number of test cases", 1, suite.testCount());
153         assertEquals("Expected the suite to contain only warnings",
154                      ParametrizedTestSuite.Warning.class.getName(),
155                      suite.testAt(0).getClass().getName());
156         TestResult result = new TestResult();
157         Enumeration JavaDoc failures = result.failures();
158         suite.testAt(0).run(result);
159         assertEquals("warning message:",
160                      "No test data specified for test suite 'some_name'!",
161                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
162     }
163     
164    /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(String)} constructor
165      * with a name that has no matching properties data.
166      * The test expects that a warning is added to the test suite.
167      */

168     public void testParametrizedTestSuite_String_NoData() {
169         ParametrizedTestSuite suite =
170             new ParametrizedTestSuite("there_are_no_properties_for_this_name");
171         
172         assertEquals("Number of test cases", 2, suite.testCount());
173         assertEquals("Expected the suite to contain only warnings",
174                      ParametrizedTestSuite.Warning.class.getName(),
175                      suite.testAt(0).getClass().getName());
176         assertEquals("Expected the suite to contain only warnings",
177                      ParametrizedTestSuite.Warning.class.getName(),
178                      suite.testAt(1).getClass().getName());
179         
180         TestResult result = new TestResult();
181         suite.testAt(0).run(result);
182         suite.testAt(1).run(result);
183         Enumeration JavaDoc failures = result.failures();
184         assertEquals("warning message #1",
185                      "Cannot load test data: Resource 'there_are_no_properties_for_this_name.properties' not found!",
186                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
187         assertEquals("warning message #2",
188                      "Test data definition for test suite 'there_are_no_properties_for_this_name' is empty!",
189                      ((TestFailure)failures.nextElement()).thrownException().getMessage());
190     }
191     
192     /** Test the {@link ParametrizedTestSuite#ParametrizedTestSuite(Class)} constructor with a class
193      * that has a matching test data properties file.
194      */

195     public void testParametrizedTestSuite_Class() throws Throwable JavaDoc {
196         ParametrizedTestSuite suite =
197             new ParametrizedTestSuite(ParametrizationTestDummy.class);
198         assertTestSuiteOK(suite);
199     }
200     
201     /** Test of addTest method, of class de.tivano.junit.ParametrizedTestSuite. */
202     public void testAddTest() throws Throwable JavaDoc {
203         ParametrizedTestSuite suite =
204             new ParametrizedTestSuite("Test1", testProperties);
205         suite.addTest(new ParametrizationTestDummy("testFailAlways"));
206         suite.addTest(new ParametrizationTestDummy("testStandardData"));
207         suite.addTest(new ParametrizationTestDummy("testSetterMethodAndPublicField"));
208         suite.addTest(new ParametrizationTestDummy("testLeaveMeAlone"));
209         assertEquals("Number of tests:", 4, suite.testCount());
210         assertEquals("Total number of test cases:", 4, suite.countTestCases());
211         for (int i=0; i<suite.testCount(); i++)
212             assertWrappedTestOK(suite.testAt(i), "Test1");
213     }
214     
215     /** Test the correct behaviour for ambiguous setter methods. */
216     public void testAmbiguousSetterMethod() {
217         Properties JavaDoc prop = new Properties JavaDoc();
218         prop.setProperty("Test1.foo", "bar");
219         ParametrizedTestSuite suite =
220             new ParametrizedTestSuite("Test1", prop);
221         Test test = new Test() {
222             public void run(TestResult result) {}
223             public int countTestCases() { return 1; }
224             public void setFoo(String JavaDoc value) {}
225             public void setFoo(int value) {}
226             
227         };
228         
229         suite.addTest(test);
230         Test wrapper = suite.testAt(0);
231         TestResult result = new TestResult();
232         wrapper.run(result);
233         assertEquals("Number of failure in the test result:", 1, result.failureCount());
234         TestFailure failure = (TestFailure)result.failures().nextElement();
235         assertEquals("Error message:",
236                      "Cannot set value for attribute 'foo': More than one setter method with compatible parameter types found.",
237                      failure.thrownException().getMessage());
238     }
239     
240     /** Test the correct behaviour for missing fields or setter methods. */
241     public void testMissingProperty() {
242         Properties JavaDoc prop = new Properties JavaDoc();
243         prop.setProperty("Test1.foo", "bar");
244         ParametrizedTestSuite suite =
245             new ParametrizedTestSuite("Test1", prop);
246         Test test = new Test() {
247             public void run(TestResult result) {}
248             public int countTestCases() { return 1; }
249         };
250         
251         suite.addTest(test);
252         Test wrapper = suite.testAt(0);
253         TestResult result = new TestResult();
254         wrapper.run(result);
255         assertEquals("Number of failure in the test result:", 1, result.failureCount());
256         TestFailure failure = (TestFailure)result.failures().nextElement();
257         assertEquals("Error message:",
258                      "Cannot set value for attribute 'foo': No setter method or public member field found with that name.",
259                      failure.thrownException().getMessage());
260     }
261     
262     /** Test of addTestSuite method, of class de.tivano.junit.ParametrizedTestSuite. */
263     public void testAddTestSuite() throws Throwable JavaDoc {
264         ParametrizedTestSuite suite =
265             new ParametrizedTestSuite(testProperties);
266         suite.addTestSuite(ParametrizationTestDummy.class);
267         assertTestSuiteOK(suite);
268     }
269     
270     
271     /** Checks if <code>test</code> is a parmetization decorator, and it is parametrizing
272      * its data according to {@link #testProperties} and <code>key</code>
273      * The wrapped test is assumed to be an instance of {@link ParametrizationTestDummy}
274      */

275     private void assertWrappedTestOK(Test wrapper, String JavaDoc key) throws Throwable JavaDoc {
276         assertTrue("Test wrapper is not an instance of ParametrizedTestSuite.ParametrizedTestWrapper",
277                    wrapper instanceof ParametrizedTestSuite.ParametrizedTestWrapper);
278         Test wrappedTest = ((ParametrizedTestSuite.ParametrizedTestWrapper)wrapper).getTest();
279         assertTrue("Actual test is not an instance of ParametrizationTestDummy",
280                     wrappedTest instanceof ParametrizationTestDummy);
281         // Test the parametrization. Because parametrizetion occurs just before
282
// running the wrapped test, we need to actually run the wrapper here.
283
// The ParametrizationTestDummy does the actual work of validating the
284
// parametrization.
285
// Before starting the test, however, we need to tell the wrapped test
286
// what parametrization data it has to expect...
287
((ParametrizationTestDummy)wrappedTest).setExpectedData(key, testProperties);
288         TestResult result = new TestResult();
289         wrapper.run(result);
290         // If the result had any errors, report this immediately. Ignore
291
// all but the first error on the assumption that they will eventually
292
// be reported on another test run once the cause for the first error has been
293
// fixed...
294
if (result.errorCount() != 0) {
295             throw ((TestFailure)result.errors().nextElement()).thrownException();
296         }
297         if ("testFailAlways".equals(((TestCase)wrappedTest).getName())) {
298         } else {
299             // If there was a failure, report it. Ignore all but the first failure,
300
// they will be reported on subsequent tests once the cause for the
301
// first one has been fixed...
302
if (result.failureCount() != 0) {
303                 throw ((TestFailure)result.failures().nextElement()).thrownException();
304             }
305         }
306     }
307     
308     /** Checks if <code>suite</code> has the correct structure, and that the contained
309      * tests are parametrized in the right way.
310      * The structure is expected to match the definition given in the
311      * {@link ParametrizedTestSuite} documentation, with the tested class being
312      * {@link ParametrizationTestDummy} and the associated test data properties
313      * being {@link #testData}.
314      */

315     private void assertTestSuiteOK(TestSuite suite) throws Throwable JavaDoc {
316         assertEquals("Number of tests in the suite:", 2, suite.testCount());
317         assertEquals("Total number of test cases:", 8, suite.countTestCases());
318         final String JavaDoc TEST_NAMES[] = { "Test1", "Test123" };
319         for (int i=0; i<suite.testCount(); i++) {
320             String JavaDoc testName = TEST_NAMES[i];
321             assertEquals("Inner test suite class:",
322                           ParametrizedTestSuite.class.getName(),
323                           suite.testAt(i).getClass().getName());
324             TestSuite innerSuite = (TestSuite)suite.testAt(i);
325             assertEquals("Inner test suite name:", testName,
326                           innerSuite.getName());
327             assertEquals("Number of tests in inner suite " + innerSuite.getName() + ":",
328                          4, innerSuite.testCount());
329             assertEquals("Total number of tests cases inner suite " + innerSuite.getName() + ":",
330                          4, innerSuite.countTestCases());
331             Set JavaDoc testCaseNames = new HashSet JavaDoc();
332             for (int j=0; j<innerSuite.testCount(); j++) {
333                 assertWrappedTestOK(innerSuite.testAt(j), testName);
334                 String JavaDoc currentTest =
335                     ((TestCase)((ParametrizedTestSuite.ParametrizedTestWrapper)innerSuite.testAt(j)).getTest()).getName();
336                 testCaseNames.add(currentTest);
337             }
338             assertTrue("Missing the ParametrizationTestDummy.testFailAlways() test",
339                        testCaseNames.contains("testFailAlways"));
340             assertTrue("Missing the ParametrizationTestDummy.testStandardData() test",
341                        testCaseNames.contains("testStandardData"));
342             assertTrue("Missing the ParametrizationTestDummy.testSetterMethodAndPublicField() test",
343                        testCaseNames.contains("testSetterMethodAndPublicField"));
344             assertTrue("Missing the ParametrizationTestDummy.testLeaveMeAlone() test",
345                        testCaseNames.contains("testLeaveMeAlone"));
346         }
347     }
348 }
349
Popular Tags