1 26 27 package net.sourceforge.groboutils.util.classes.v1; 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 35 36 43 public class ClassLoadHelperUTest extends TestCase 44 { 45 48 private static final Class THIS_CLASS = ClassLoadHelperUTest.class; 49 50 public ClassLoadHelperUTest( String name ) 51 { 52 super( name ); 53 } 54 55 56 59 63 protected void setUp() throws Exception 64 { 65 super.setUp(); 66 67 } 69 70 71 74 75 public void testConstructor1() 76 { 77 new ClassLoadHelper(); 78 } 79 80 81 public void testConstructor2() 82 { 83 try 84 { 85 new ClassLoadHelper( (Class )null ); 86 fail("Did not throw NullPointerException"); 87 } 88 catch (NullPointerException npe) 89 { 90 } 92 } 93 94 95 public void testConstructor2a() 96 { 97 new ClassLoadHelper( (ClassLoader )null ); 98 } 99 100 101 public void testConstructor3() 102 { 103 new ClassLoadHelper( createClassLoader() ); 104 } 105 106 107 public void testGetClass1() 108 { 109 ClassLoadHelper clh = new ClassLoadHelper(); 110 Class c = clh.getClass( null ); 111 assertNull( 112 "Null string should return null.", 113 c ); 114 } 115 116 117 private static final String BAD_CLASS_NAME_1 = ""; 118 private static final String BAD_CLASS_NAME_2 = "java.lang.2-Not A Class"; 120 121 public void testGetClass2() 122 { 123 ClassLoadHelper clh = new ClassLoadHelper(); 124 125 Class c = clh.getClass( BAD_CLASS_NAME_1 ); 126 assertNull( 127 "Bad class name should return null.", 128 c ); 129 } 130 131 public void testGetClass3() 132 { 133 ClassLoadHelper clh = new ClassLoadHelper(); 134 135 Class c = clh.getClass( BAD_CLASS_NAME_2 ); 136 assertNull( 137 "Bad class name should return null.", 138 c ); 139 } 140 141 142 public void testGetClass4() 143 { 144 ClassLoadHelper clh = new ClassLoadHelper(); 145 146 Class c = clh.getClass( String .class.getName() ); 147 assertEquals( 148 "Should have returned the String class with the same classloader "+ 149 "as the one that loaded ourself.", 150 String .class, 151 c ); 152 } 153 154 155 public interface MyInterface {} 156 public class MyInnerClass {} 157 public static class MyGoodClass {} 158 159 public void testCreateObjectS1() 160 { 161 ClassLoadHelper clh = new ClassLoadHelper(); 162 163 Object o = clh.createObject( (String )null ); 164 assertNull( 165 "Null string should return null.", 166 o ); 167 } 168 169 public void testCreateObjectS2() 170 { 171 ClassLoadHelper clh = new ClassLoadHelper(); 172 173 Object o = clh.createObject( BAD_CLASS_NAME_1 ); 175 assertNull( 176 "Bad class name should return null.", 177 o ); 178 } 179 180 public void testCreateObjectS3() 181 { 182 ClassLoadHelper clh = new ClassLoadHelper(); 183 184 Object o = clh.createObject( BAD_CLASS_NAME_2 ); 186 assertNull( 187 "Bad class name should return null.", 188 o ); 189 } 190 191 public void testCreateObjectS4() 192 { 193 ClassLoadHelper clh = new ClassLoadHelper(); 194 195 Object o = clh.createObject( MyInterface.class.getName() ); 197 assertNull( 198 "Interface should return null.", 199 o ); 200 } 201 202 public void testCreateObjectS5() 203 { 204 ClassLoadHelper clh = new ClassLoadHelper(); 205 206 Object o = clh.createObject( MyInnerClass.class.getName() ); 208 assertNull( 209 "Inner class should return null.", 210 o ); 211 } 212 213 public void testCreateObjectS6() 214 { 215 ClassLoadHelper clh = new ClassLoadHelper(); 216 217 Object o = clh.createObject( this.getClass().getName() ); 219 assertNull( 220 "No-default constructor class should return null.", 221 o ); 222 } 223 224 public void testCreateObjectS7() 225 { 226 ClassLoadHelper clh = new ClassLoadHelper(); 227 228 Object o = clh.createObject( MyGoodClass.class.getName() ); 229 assertNotNull( 230 "Good class should not return null.", 231 o ); 232 assertEquals( 233 "Good class should be of the correct class.", 234 MyGoodClass.class, 235 o.getClass() ); 236 } 237 238 239 public void testCreateObjectSZ1() 240 { 241 ClassLoadHelper clh = new ClassLoadHelper(); 242 243 Object o = clh.createObject( (String )null, false ); 245 assertNull( 246 "Null string should return null.", 247 o ); 248 } 249 250 public void testCreateObjectSZ2() 251 { 252 ClassLoadHelper clh = new ClassLoadHelper(); 253 254 try 256 { 257 Object o = clh.createObject( BAD_CLASS_NAME_1, false ); 258 fail( "Did not throw an IllegalStateException: retrieved "+o ); 259 } 260 catch (IllegalStateException ise) 261 { 262 } 264 } 265 266 public void testCreateObjectSZ3() 267 { 268 ClassLoadHelper clh = new ClassLoadHelper(); 269 270 try 272 { 273 Object o = clh.createObject( BAD_CLASS_NAME_2, false ); 274 fail( "Did not throw an IllegalStateException: retrieved "+o ); 275 } 276 catch (IllegalStateException ise) 277 { 278 } 280 } 281 282 public void testCreateObjectSZ4() 283 { 284 ClassLoadHelper clh = new ClassLoadHelper(); 285 286 try 288 { 289 Object o = clh.createObject( MyInterface.class.getName(), false ); 290 fail( "Did not throw an IllegalStateException: retrieved "+o ); 291 } 292 catch (IllegalStateException ise) 293 { 294 } 296 } 297 298 public void testCreateObjectSZ5() 299 { 300 ClassLoadHelper clh = new ClassLoadHelper(); 301 302 try 304 { 305 Object o = clh.createObject( MyInnerClass.class.getName(), false ); 306 fail( "Did not throw an IllegalStateException: retrieved "+o ); 307 } 308 catch (IllegalStateException ise) 309 { 310 } 312 } 313 314 public void testCreateObjectSZ6() 315 { 316 ClassLoadHelper clh = new ClassLoadHelper(); 317 318 try 320 { 321 Object o = clh.createObject( this.getClass().getName(), false ); 322 fail( "Did not throw an IllegalStateException." ); 323 } 324 catch (IllegalStateException ise) 325 { 326 } 328 } 329 330 public void testCreateObjectSZ7() 331 { 332 ClassLoadHelper clh = new ClassLoadHelper(); 333 334 Object o = clh.createObject( MyGoodClass.class.getName(), false ); 335 assertNotNull( 336 "Good class should not return null.", 337 o ); 338 assertEquals( 339 "Good class should be of the correct class.", 340 MyGoodClass.class, 341 o.getClass() ); 342 } 343 344 345 346 347 350 351 protected ClassLoader createClassLoader() 352 { 353 return new ClassLoader () { 354 public Class loadClass( String s, boolean f ) 355 { 356 return null; 357 } 358 }; 359 } 360 361 362 365 366 public static Test suite() 367 { 368 TestSuite suite = new TestSuite( THIS_CLASS ); 369 370 381 382 return suite; 383 } 384 385 public static void main( String [] args ) 386 { 387 String [] name = { THIS_CLASS.getName() }; 388 389 392 junit.textui.TestRunner.main( name ); 393 } 394 395 396 400 protected void tearDown() throws Exception 401 { 402 404 405 super.tearDown(); 406 } 407 } 408 409 | Popular Tags |