1 26 27 package net.sourceforge.groboutils.junit.v1.iftc; 28 29 import org.easymock.EasyMock; 30 import org.easymock.MockControl; 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 import junit.framework.AssertionFailedError; 35 36 37 44 public class InterfaceTestCaseUTest extends TestCase 45 { 46 47 50 private static final Class THIS_CLASS = InterfaceTestCaseUTest.class; 51 private static final org.apache.log4j.Logger LOG = 52 org.apache.log4j.Logger.getLogger( InterfaceTestCaseUTest.class ); 53 54 public InterfaceTestCaseUTest( String name ) 55 { 56 super( name ); 57 } 58 59 60 61 62 65 public static class MyInterfaceTestCase extends InterfaceTestCase 66 { 67 public MyInterfaceTestCase( String name, Class interfaceClass, 68 ImplFactory f ) 69 { 70 super( name, interfaceClass, f ); 71 } 72 } 73 74 public void testConstructor1() 75 { 76 LOG.debug( "Entering testConstructor1" ); 77 ImplFactory f = new ImplFactory() { 78 public Object createImplObject() { 79 return new Runnable () { public void run() {} }; 80 } 81 }; 82 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 83 Runnable .class, f ); 84 assertEquals( 85 "Did not store the interface class correctly.", 86 Runnable .class, 87 itc.getInterfaceClass() ); 88 LOG.debug( "Leaving testConstructor1" ); 89 } 90 91 92 public void testConstructor2() 93 { 94 LOG.debug( "Entering testConstructor2" ); 95 try 96 { 97 new MyInterfaceTestCase( "test", null, null ); 98 fail("Did not throw IllegalArgumentException."); 99 } 100 catch (IllegalArgumentException e) 101 { 102 } 104 LOG.debug( "Leaving testConstructor2" ); 105 } 106 107 108 public void testConstructor3() 109 { 110 LOG.debug( "Entering testConstructor3" ); 111 try 112 { 113 new MyInterfaceTestCase( "test", Runnable .class, null ); 114 fail("Did not throw IllegalArgumentException."); 115 } 116 catch (IllegalArgumentException e) 117 { 118 } 120 LOG.debug( "Leaving testConstructor3" ); 121 } 122 123 124 public void testConstructor4() 125 { 126 LOG.debug( "Entering testConstructor4" ); 127 ImplFactory f = new ImplFactory() { 128 public Object createImplObject() { 129 return new Runnable () { public void run() {} }; 130 } 131 }; 132 try 133 { 134 new MyInterfaceTestCase( "test", null, f ); 135 fail("Did not throw IllegalArgumentException."); 136 } 137 catch (IllegalArgumentException e) 138 { 139 } 141 LOG.debug( "Leaving testConstructor4" ); 142 } 143 144 145 public void testCreate1() 146 { 147 LOG.debug( "Entering testCreate1" ); 148 ImplFactory f = new ImplFactory() { 149 public Object createImplObject() { 150 return "a string"; 151 } 152 }; 153 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 154 Runnable .class, f ); 155 try 156 { 157 itc.createImplObject(); 158 fail("Did not fail."); 159 } 160 catch (AssertionFailedError e) 161 { 162 } 164 LOG.debug( "Leaving testCreate1" ); 165 } 166 167 168 public void testCreate2() 169 { 170 LOG.debug( "Entering testCreate2" ); 171 ImplFactory f = new ImplFactory() { 172 public Object createImplObject() { 173 return null; 174 } 175 }; 176 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 177 Runnable .class, f ); 178 try 179 { 180 itc.createImplObject(); 181 fail("Did not fail."); 182 } 183 catch (AssertionFailedError e) 184 { 185 } 187 LOG.debug( "Leaving testCreate2" ); 188 } 189 190 191 public void testCreate3() 192 { 193 LOG.debug( "Entering testCreate3" ); 194 ImplFactory f = new ImplFactory() { 195 public Object createImplObject() { 196 throw new IllegalArgumentException ("IAE"); 197 } 198 }; 199 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 200 Runnable .class, f ); 201 try 202 { 203 itc.createImplObject(); 204 fail("Did not throw exception."); 205 } 206 catch (AssertionFailedError e) 207 { 208 assertTrue( 210 "Does not contain the correct exception text.", 211 e.getMessage().indexOf( " threw exception "+ 212 "java.lang.IllegalArgumentException: IAE during "+ 213 "creation:" ) > 0 ); 214 215 } 216 } 217 218 219 final String teststring = "a string"; 220 public void testCreate4() 221 { 222 LOG.debug( "Entering testCreate4" ); 223 ImplFactory f = new ImplFactory() { 224 public Object createImplObject() { 225 return teststring; 226 } 227 }; 228 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 229 String .class, f ); 230 Object o = itc.createImplObject(); 231 assertSame( 232 "Did not return the exact object we thought we returned.", 233 teststring, 234 o ); 235 } 236 237 238 final static String factoryname = "TestFactoryName"; 239 public void testToString1() 240 { 241 LOG.debug( "Entering testToString1" ); 242 ImplFactory f = new ImplFactory() { 243 public Object createImplObject() { 244 return ""; 245 } 246 public String toString() { 247 return factoryname; 248 } 249 }; 250 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 251 String .class, f ); 252 itc.setUseClassInName( false ); 253 assertEquals( 254 "Did not return the correct text.", 255 "test["+factoryname+"]("+itc.getClass().getName()+")", 256 itc.toString() ); 257 } 258 259 260 public void testGetName1() 261 { 262 LOG.debug( "Entering testGetName1" ); 263 ImplFactory f = new ImplFactory() { 264 public Object createImplObject() { 265 return ""; 266 } 267 public String toString() { 268 return factoryname; 269 } 270 }; 271 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 272 String .class, f ); 273 itc.setUseClassInName( false ); 274 assertEquals( 275 "Did not return the correct text.", 276 "test["+factoryname+"]", 277 itc.getName() ); 278 } 279 280 281 public void testGetName2() 282 { 283 LOG.debug( "Entering testGetName1" ); 284 ImplFactory f = new ImplFactory() { 285 public Object createImplObject() { 286 return ""; 287 } 288 public String toString() { 289 return factoryname; 290 } 291 }; 292 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 293 String .class, f ); 294 itc.setUseClassInName( true ); 295 assertEquals( 296 "Did not return the correct text.", 297 "InterfaceTestCaseUTest$MyInterfaceTestCase.test["+factoryname+"]", 298 itc.getName() ); 299 } 300 301 302 public void testName1() 303 { 304 LOG.debug( "Entering testName1" ); 305 ImplFactory f = new ImplFactory() { 306 public Object createImplObject() { 307 return ""; 308 } 309 public String toString() { 310 return factoryname; 311 } 312 }; 313 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 314 String .class, f ); 315 itc.setUseClassInName( false ); 316 assertEquals( 317 "Did not return the correct text.", 318 itc.getName(), 319 itc.name() ); 320 } 321 322 324 private static class MyCxFactory implements ICxFactory 325 { 326 public int createCount = 0; 327 public int tearDownCount = 0; 328 329 public Object createImplObject() 330 { 331 ++this.createCount; 332 return new Integer ( this.createCount ); 333 } 334 335 public void tearDown( Object o ) 336 { 337 int i = ((Integer )o).intValue(); 338 assertEquals( 339 "Did not tear down in the right order.", 340 createCount - tearDownCount, 341 i ); 342 ++tearDownCount; 343 } 344 } 345 346 public void testTearDown1() throws Exception 347 { 348 LOG.debug( "Entering testTearDown1()" ); 349 MyCxFactory f = new MyCxFactory(); 350 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 351 Integer .class, f ); 352 int instantCount = 100; 353 for (int i = 0; i < instantCount; ++i) 354 { 355 itc.createImplObject(); 356 } 357 itc.tearDown(); 358 assertEquals( 359 "Did not tear down all expected instantiated objects.", 360 instantCount, 361 f.tearDownCount ); 362 } 363 364 366 private static class MyCxFactory2 implements ICxFactory 367 { 368 public Object createImplObject() 369 { 370 return new Integer ( 0 ); 371 } 372 373 public void tearDown( Object o ) throws Exception 374 { 375 throw new IllegalStateException (); 376 } 377 } 378 379 public void testTearDown2() throws Exception 380 { 381 LOG.debug( "Entering testTearDown2()" ); 382 MyCxFactory2 f = new MyCxFactory2(); 383 InterfaceTestCase itc = new MyInterfaceTestCase( "test", 384 Integer .class, f ); 385 int instantCount = 100; 386 for (int i = 0; i < instantCount; ++i) 387 { 388 itc.createImplObject(); 389 } 390 try 391 { 392 itc.tearDown(); 393 fail( "tearDown did not propigate any exceptions to the top." ); 394 } 395 catch (AssertionFailedError ex) 396 { 397 String s = ex.toString(); 398 int count = -1; 399 int pos = 0; 400 while (pos >= 0) 401 { 402 ++count; 403 pos = s.indexOf( IllegalStateException .class.getName(), 404 pos+1 ); 405 } 406 assertEquals( 407 "Did not catch or report all exceptions.", 408 instantCount, 409 count ); 410 } 411 } 412 413 414 415 418 419 public static Test suite() 420 { 421 TestSuite suite = new TestSuite( THIS_CLASS ); 422 423 return suite; 424 } 425 426 public static void main( String [] args ) 427 { 428 String [] name = { THIS_CLASS.getName() }; 429 430 433 junit.textui.TestRunner.main( name ); 434 } 435 436 437 441 protected void setUp() throws Exception 442 { 443 super.setUp(); 444 445 } 447 448 449 453 protected void tearDown() throws Exception 454 { 455 457 458 super.tearDown(); 459 } 460 } 461 462 | Popular Tags |