1 7 8 package de.tivano.junit; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.util.Enumeration ; 13 import java.util.HashSet ; 14 import java.util.Properties ; 15 import java.util.Set ; 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 27 public class ParametrizedTestSuiteTest extends TestCase { 28 29 public ParametrizedTestSuiteTest(java.lang.String testName) { 30 super(testName); 31 } 32 33 public static void main(java.lang.String [] 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 testProperties; 44 45 46 47 public void setUp() throws IOException { 48 InputStream data = getClass().getResourceAsStream( 49 "/de/tivano/junit/ParametrizationTestDummy.properties"); 50 testProperties = new Properties (); 51 testProperties.load(data); 52 } 53 54 55 public void testWarning() { 56 ParametrizedTestSuite suite = new ParametrizedTestSuite(new Properties ()); 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 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 85 public void testParametrizedTestSuite_Properties_Empty() { 86 ParametrizedTestSuite suite = new ParametrizedTestSuite(new Properties ()); 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 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 103 public void testParametrizedTestSuite_Properties_Null() { 104 ParametrizedTestSuite suite = new ParametrizedTestSuite((Properties )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 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 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 130 public void testParametrizedTestSuite_String_Properties_Empty() { 131 ParametrizedTestSuite suite = new ParametrizedTestSuite("some_name", new Properties ()); 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 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 149 public void testParametrizedTestSuite_String_Properties_Null() { 150 ParametrizedTestSuite suite = new ParametrizedTestSuite("some_name", (Properties )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 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 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 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 195 public void testParametrizedTestSuite_Class() throws Throwable { 196 ParametrizedTestSuite suite = 197 new ParametrizedTestSuite(ParametrizationTestDummy.class); 198 assertTestSuiteOK(suite); 199 } 200 201 202 public void testAddTest() throws Throwable { 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 216 public void testAmbiguousSetterMethod() { 217 Properties prop = new Properties (); 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 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 241 public void testMissingProperty() { 242 Properties prop = new Properties (); 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 263 public void testAddTestSuite() throws Throwable { 264 ParametrizedTestSuite suite = 265 new ParametrizedTestSuite(testProperties); 266 suite.addTestSuite(ParametrizationTestDummy.class); 267 assertTestSuiteOK(suite); 268 } 269 270 271 275 private void assertWrappedTestOK(Test wrapper, String key) throws Throwable { 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 ((ParametrizationTestDummy)wrappedTest).setExpectedData(key, testProperties); 288 TestResult result = new TestResult(); 289 wrapper.run(result); 290 if (result.errorCount() != 0) { 295 throw ((TestFailure)result.errors().nextElement()).thrownException(); 296 } 297 if ("testFailAlways".equals(((TestCase)wrappedTest).getName())) { 298 } else { 299 if (result.failureCount() != 0) { 303 throw ((TestFailure)result.failures().nextElement()).thrownException(); 304 } 305 } 306 } 307 308 315 private void assertTestSuiteOK(TestSuite suite) throws Throwable { 316 assertEquals("Number of tests in the suite:", 2, suite.testCount()); 317 assertEquals("Total number of test cases:", 8, suite.countTestCases()); 318 final String TEST_NAMES[] = { "Test1", "Test123" }; 319 for (int i=0; i<suite.testCount(); i++) { 320 String 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 testCaseNames = new HashSet (); 332 for (int j=0; j<innerSuite.testCount(); j++) { 333 assertWrappedTestOK(innerSuite.testAt(j), testName); 334 String 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 |