1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 import junit.framework.TestResult; 34 import junit.framework.AssertionFailedError; 35 36 import java.io.IOException ; 37 import java.lang.reflect.Method ; 38 39 40 47 public class SubTestTestCaseUTest extends TestCase 48 { 49 52 private static final Class THIS_CLASS = SubTestTestCaseUTest.class; 53 55 public SubTestTestCaseUTest( String name ) 56 { 57 super( name ); 58 } 59 60 61 62 63 66 67 public static class MyTestCase1 extends TestCase 68 { 69 public MyTestCase1( String name ) 70 { 71 super( name ); 72 } 73 74 public void testFail() 75 { 76 fail("this should fail."); 77 } 78 79 public static Test suite() 80 { 81 return new TestSuite( MyTestCase1.class ); 82 } 83 } 84 85 86 public static class MyTestCase2 extends SubTestTestCase 87 { 88 public MyTestCase2( String name ) 89 { 90 super( name ); 91 } 92 93 public void testFailTwice() 94 { 95 addSubTest( MyTestCase1.suite() ); 96 fail( "this should fail too." ); 97 } 98 99 public static Test suite() 100 { 101 return new TestSuite( MyTestCase2.class ); 102 } 103 } 104 105 106 public static class MyTestCase3 extends SubTestTestCase 107 { 108 boolean enteredOnce = false; 109 long createdTime = System.currentTimeMillis(); 110 111 public MyTestCase3( String name ) 112 { 113 super( name ); 114 } 115 116 public void testReentryTwice() 117 { 118 try 119 { 120 System.out.println("Entering Created Time="+createdTime); 121 if (!this.enteredOnce) 122 { 123 this.enteredOnce = true; 124 System.out.println("Running again Created Time="+createdTime); 125 run( new TestResult() ); 126 System.out.println("Back from Created Time="+createdTime); 127 } 128 addSubTest( MyTestCase1.suite() ); 129 System.out.println("Leaving Created Time="+createdTime); 130 } 131 catch (RuntimeException re) 132 { 133 re.printStackTrace(); 134 throw re; 135 } 136 catch (Error e) 137 { 138 e.printStackTrace(); 139 throw e; 140 } 141 } 142 143 public static Test suite() 144 { 145 return new TestSuite( MyTestCase3.class ); 146 } 147 } 148 149 150 public void testSameResult1() 151 { 152 try 154 { 155 Test t = MyTestCase2.suite(); 156 TestResult tr = new TestResult(); 157 158 t.run( tr ); 159 160 assertEquals( 161 "Should have 2 failures now.", 162 2, 163 tr.failureCount() ); 164 assertEquals( 165 "Should have no errors now.", 166 0, 167 tr.errorCount() ); 168 assertEquals( 169 "Should have 2 tests now.", 170 2, 171 tr.runCount() ); 172 } 173 catch (RuntimeException re) 174 { 175 re.printStackTrace(); 176 throw re; 177 } 178 catch (Error e) 179 { 180 e.printStackTrace(); 181 throw e; 182 } 183 } 184 185 186 public void testReentry1() 187 { 188 try 189 { 190 192 Test t = MyTestCase3.suite(); 193 TestResult tr = new TestResult(); 194 195 t.run( tr ); 204 205 assertEquals( 206 "Should have 2 failures.", 207 2, 208 tr.failureCount() ); 209 assertEquals( 210 "Should have no errors now.", 211 0, 212 tr.errorCount() ); 213 assertEquals( 214 "Should have 3 tests now.", 215 3, 216 tr.runCount() ); 217 } 218 catch (RuntimeException re) 219 { 220 re.printStackTrace(); 221 throw re; 222 } 223 catch (Error e) 224 { 225 e.printStackTrace(); 226 throw e; 227 } 228 } 229 230 231 232 235 236 237 240 241 public static Test suite() 242 { 243 try 244 { 245 TestSuite suite = new TestSuite( THIS_CLASS ); 246 247 return suite; 248 } 249 catch (RuntimeException re) 250 { 251 re.printStackTrace(); 252 throw re; 253 } 254 catch (Error e) 255 { 256 e.printStackTrace(); 257 throw e; 258 } 259 } 260 261 public static void main( String [] args ) 262 { 263 String [] name = { THIS_CLASS.getName() }; 264 265 268 junit.textui.TestRunner.main( name ); 269 } 270 271 272 276 protected void setUp() throws Exception 277 { 278 super.setUp(); 279 280 } 282 283 284 288 protected void tearDown() throws Exception 289 { 290 292 293 super.tearDown(); 294 } 295 } 296 297 | Popular Tags |