1 21 22 package org.opensubsystems.core.util; 23 24 import java.util.Properties ; 25 26 import junit.framework.TestCase; 27 28 37 public class ClassFactoryTest extends TestCase 38 { 39 41 44 public static class ClassFactoryTestSampleBase 45 { 46 49 protected String m_attribA; 50 51 54 public ClassFactoryTestSampleBase() 55 { 56 m_attribA = "1"; 57 } 58 } 59 60 63 public static class ClassFactoryTestSampleDerived extends ClassFactoryTestSampleBase 64 { 65 68 protected String m_attribB; 69 70 73 public ClassFactoryTestSampleDerived() 74 { 75 m_attribB = "2"; 76 } 77 } 78 79 81 85 public ClassFactoryTest( 86 String arg0 87 ) 88 { 89 super(arg0); 90 } 91 92 97 protected void setUp( 98 ) throws Exception 99 { 100 super.setUp(); 101 } 102 103 108 protected void tearDown() throws Exception 109 { 110 super.tearDown(); 111 } 112 113 118 public void testCreateInstanceWithClass( 119 ) throws Exception 120 { 121 Config originalInstance; 122 123 originalInstance = Config.getInstance(); 124 try 125 { 126 Object test; 127 ClassFactory factory = new ClassFactory(); 128 129 test = factory.createInstance(ClassFactoryTestSampleBase.class); 132 assertNotNull("Cannot instantiate class", test); 133 assertTrue("Must be instance of base class", 134 test instanceof ClassFactoryTestSampleBase); 135 assertFalse("Cannot be instance of derived class", 136 test instanceof ClassFactoryTestSampleDerived); 137 138 test = factory.createInstance(ClassFactoryTestSampleDerived.class); 141 assertNotNull("Cannot instantiate class", test); 142 assertTrue("Must be instance of base class", 143 test instanceof ClassFactoryTestSampleBase); 144 assertTrue("Must be instance of derived class", 145 test instanceof ClassFactoryTestSampleDerived); 146 147 Properties predefinedProperties = new Properties (); 151 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 152 ClassFactoryTestSampleDerived.class.getName()); 153 Config.setInstance(new Config(predefinedProperties)); 155 156 test = factory.createInstance(ClassFactoryTestSampleBase.class); 158 assertNotNull("Cannot instantiate class", test); 159 assertTrue("Must be instance of base class", 160 test instanceof ClassFactoryTestSampleBase); 161 assertTrue("Must be instance of derived class", 162 test instanceof ClassFactoryTestSampleDerived); 163 } 164 finally 165 { 166 Config.setInstance(originalInstance); 168 } 169 } 170 171 176 public void testCreateInstanceWithClassAndDefault( 177 ) throws Exception 178 { 179 Config originalInstance; 180 181 originalInstance = Config.getInstance(); 182 try 183 { 184 Object test; 185 ClassFactory factory = new ClassFactory(); 186 187 test = factory.createInstance(ClassFactoryTestSampleBase.class); 190 assertNotNull("Cannot instantiate class", test); 191 assertTrue("Must be instance of base class", 192 test instanceof ClassFactoryTestSampleBase); 193 assertFalse("Cannot be instance of derived class", 194 test instanceof ClassFactoryTestSampleDerived); 195 196 test = factory.createInstance(ClassFactoryTestSampleDerived.class); 199 assertNotNull("Cannot instantiate class", test); 200 assertTrue("Must be instance of base class", 201 test instanceof ClassFactoryTestSampleBase); 202 assertTrue("Must be instance of derived class", 203 test instanceof ClassFactoryTestSampleDerived); 204 205 Properties predefinedProperties = new Properties (); 209 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 210 ClassFactoryTestSampleDerived.class.getName()); 211 Config.setInstance(new Config(predefinedProperties)); 213 214 test = factory.createInstance(ClassFactoryTestSampleBase.class); 216 assertNotNull("Cannot instantiate class", test); 217 assertTrue("Must be instance of base class", 218 test instanceof ClassFactoryTestSampleBase); 219 assertTrue("Must be instance of derived class", 220 test instanceof ClassFactoryTestSampleDerived); 221 222 predefinedProperties = new Properties (); 226 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 227 "random.text"); 228 Config.setInstance(new Config(predefinedProperties)); 230 231 test = factory.createInstance(ClassFactoryTestSampleBase.class, 232 String .class); 233 assertNotNull("Cannot instantiate class", test); 234 assertTrue("Must be instance of String class", test instanceof String ); 235 } 236 finally 237 { 238 Config.setInstance(originalInstance); 240 } 241 } 242 243 248 public void testCreateInstanceWithString( 249 ) throws Exception 250 { 251 Config originalInstance; 252 253 originalInstance = Config.getInstance(); 254 try 255 { 256 Object test; 257 ClassFactory factory = new ClassFactory(); 258 259 test = factory.createInstance(ClassFactoryTestSampleBase.class.getName()); 262 assertNotNull("Cannot instantiate class", test); 263 assertTrue("Must be instance of base class", 264 test instanceof ClassFactoryTestSampleBase); 265 assertFalse("Cannot be instance of derived class", 266 test instanceof ClassFactoryTestSampleDerived); 267 268 test = factory.createInstance(ClassFactoryTestSampleDerived.class.getName()); 271 assertNotNull("Cannot instantiate class", test); 272 assertTrue("Must be instance of base class", 273 test instanceof ClassFactoryTestSampleBase); 274 assertTrue("Must be instance of derived class", 275 test instanceof ClassFactoryTestSampleDerived); 276 277 Properties predefinedProperties = new Properties (); 281 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 282 ClassFactoryTestSampleDerived.class.getName()); 283 predefinedProperties.put("random.text", 287 ClassFactoryTestSampleBase.class.getName()); 288 Config.setInstance(new Config(predefinedProperties)); 290 291 test = factory.createInstance(ClassFactoryTestSampleBase.class.getName()); 293 assertNotNull("Cannot instantiate class", test); 294 assertTrue("Must be instance of base class", 295 test instanceof ClassFactoryTestSampleBase); 296 assertTrue("Must be instance of derived class", 297 test instanceof ClassFactoryTestSampleDerived); 298 299 test = factory.createInstance("random.text"); 300 assertNotNull("Cannot instantiate class", test); 301 assertTrue("Must be instance of base class", 302 test instanceof ClassFactoryTestSampleBase); 303 assertFalse("Cannot be instance of derived class", 304 test instanceof ClassFactoryTestSampleDerived); 305 } 306 finally 307 { 308 Config.setInstance(originalInstance); 310 } 311 } 312 313 318 public void testCreateInstanceWithStringAndDefault( 319 ) throws Exception 320 { 321 Config originalInstance; 322 323 originalInstance = Config.getInstance(); 324 try 325 { 326 Object test; 327 ClassFactory factory = new ClassFactory(); 328 329 test = factory.createInstance(ClassFactoryTestSampleBase.class.getName()); 332 assertNotNull("Cannot instantiate class", test); 333 assertTrue("Must be instance of base class", 334 test instanceof ClassFactoryTestSampleBase); 335 assertFalse("Cannot be instance of derived class", 336 test instanceof ClassFactoryTestSampleDerived); 337 338 test = factory.createInstance(ClassFactoryTestSampleDerived.class.getName()); 341 assertNotNull("Cannot instantiate class", test); 342 assertTrue("Must be instance of base class", 343 test instanceof ClassFactoryTestSampleBase); 344 assertTrue("Must be instance of derived class", 345 test instanceof ClassFactoryTestSampleDerived); 346 347 Properties predefinedProperties = new Properties (); 351 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 352 ClassFactoryTestSampleDerived.class.getName()); 353 predefinedProperties.put("random.text", 357 ClassFactoryTestSampleBase.class.getName()); 358 Config.setInstance(new Config(predefinedProperties)); 360 361 test = factory.createInstance(ClassFactoryTestSampleBase.class.getName()); 363 assertNotNull("Cannot instantiate class", test); 364 assertTrue("Must be instance of base class", 365 test instanceof ClassFactoryTestSampleBase); 366 assertTrue("Must be instance of derived class", 367 test instanceof ClassFactoryTestSampleDerived); 368 369 test = factory.createInstance("random.text"); 370 assertNotNull("Cannot instantiate class", test); 371 assertTrue("Must be instance of base class", 372 test instanceof ClassFactoryTestSampleBase); 373 assertFalse("Cannot be instance of derived class", 374 test instanceof ClassFactoryTestSampleDerived); 375 376 predefinedProperties = new Properties (); 380 predefinedProperties.put(ClassFactoryTestSampleBase.class.getName(), 381 "random.text"); 382 Config.setInstance(new Config(predefinedProperties)); 384 385 test = factory.createInstance(ClassFactoryTestSampleBase.class.getName(), 386 String .class); 387 assertNotNull("Cannot instantiate class", test); 388 assertTrue("Must be instance of String class", test instanceof String ); 389 } 390 finally 391 { 392 Config.setInstance(originalInstance); 394 } 395 } 396 } 397 | Popular Tags |