1 26 27 package net.sourceforge.groboutils.junit.v1.iftc; 28 29 30 import junit.framework.TestCase; 31 32 import java.io.StringWriter ; 33 import java.io.PrintWriter ; 34 35 import java.util.Stack ; 36 37 38 89 public abstract class InterfaceTestCase extends TestCase 90 { 91 static final String DONT_USE_CLASSNAME = 93 InterfaceTestCase.class.getName() + ".no-classname"; 94 95 private ImplFactory factory; 96 private Class interfaceClass; 97 98 private Stack instantiatedObjects = new Stack (); 101 102 private boolean isICxFactory = false; 105 106 private Boolean useClassInName = null; 108 109 110 117 public InterfaceTestCase( String name, Class interfaceClass, ImplFactory f ) 118 { 119 super( name ); 120 if (interfaceClass == null || f == null) 121 { 122 throw new IllegalArgumentException ("no null arguments"); 123 } 124 125 assertTrue( 127 "Interface under test argument ("+interfaceClass.getName()+ 128 ") is the same as the current test's class ("+ 129 getClass().getName()+ 130 "). The correct usage is to pass in the Class object which "+ 131 "corresponds to the superclass or interface all instance methods "+ 132 "tested must extend.", 133 !getClass().equals( interfaceClass ) ); 134 135 this.interfaceClass = interfaceClass; 138 this.factory = f; 139 140 if (f instanceof ICxFactory) 142 { 143 this.isICxFactory = true; 144 } 145 } 146 147 148 155 public void setUseClassInName( boolean use ) 156 { 157 this.useClassInName = new Boolean ( use ); 158 } 159 160 161 162 163 176 public Object createImplObject() 177 { 178 assertNotNull( 180 "The factory instance was never set.", 181 this.factory ); 182 183 Object o; 184 try 185 { 186 o = this.factory.createImplObject(); 187 } 188 catch (Exception ex) 189 { 190 fail( "Factory "+this.factory.toString()+ 192 " threw exception "+ex+" during creation: "+ 193 exceptionToString( ex ) ); 194 o = null; 197 } 198 assertNotNull( 199 "The implementation factory "+this.factory+" created a null.", 200 o ); 201 202 if (this.isICxFactory) 206 { 207 this.instantiatedObjects.push( o ); 208 } 209 210 assertTrue( 211 "The implementation factory did not create a valid class: created "+ 212 o.getClass().getName()+", but should have been of type "+ 213 getInterfaceClass().getName()+".", 214 getInterfaceClass().isInstance( o ) ); 215 return o; 216 } 217 218 219 224 public Class getInterfaceClass() 225 { 226 return this.interfaceClass; 227 } 228 229 230 237 public String getName() 238 { 239 return getNamePrefix() + super.getName() + 240 "[" + this.factory.toString() +"]"; 241 } 242 243 244 250 public String name() 251 { 252 return getName(); 253 } 254 255 256 263 protected void tearDown() throws Exception 264 { 265 if (this.isICxFactory) 266 { 267 int errorCount = 0; 268 StringBuffer sb = new StringBuffer ( 269 "Encountered factory tearDown exceptions: " ); 270 ICxFactory cf = (ICxFactory)this.factory; 271 while (!this.instantiatedObjects.isEmpty()) 272 { 273 try 274 { 275 cf.tearDown( this.instantiatedObjects.pop() ); 276 } 277 catch (ThreadDeath td) 278 { 279 throw td; 281 } 282 catch (Throwable t) 283 { 284 if (errorCount > 0) 287 { 288 sb.append( "; " ); 289 } 290 sb.append( t.toString() ); 291 292 ++errorCount; 293 294 t.printStackTrace(); 296 } 297 } 298 assertTrue( sb.toString(), errorCount <= 0 ); 301 } 302 303 super.tearDown(); 305 } 306 307 308 315 private String exceptionToString( Throwable t ) 316 { 317 if (t == null) 318 { 319 return "<null exception>"; 320 } 321 StringWriter sw = new StringWriter (); 322 PrintWriter pw = new PrintWriter ( sw ); 323 t.printStackTrace( pw ); 324 pw.flush(); 325 return sw.toString(); 326 } 327 328 329 335 private String getNamePrefix() 336 { 337 boolean usePrefix; 338 if (this.useClassInName != null) 339 { 340 usePrefix = this.useClassInName.booleanValue(); 341 } 342 else 343 { 344 usePrefix = !Boolean.getBoolean( DONT_USE_CLASSNAME ); 345 } 346 String ret = ""; 347 if (usePrefix) 348 { 349 ret = this.getClass().getName(); 351 int pos = ret.lastIndexOf( '.' ); 352 if (pos >= 0) 353 { 354 ret = ret.substring( pos+1 ); 355 } 356 ret = ret + '.'; 357 } 358 return ret; 359 } 360 } 361 362 | Popular Tags |