1 26 27 package net.sourceforge.groboutils.util.classes.v1; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 34 43 public class ClassUtilUTest extends TestCase 44 { 45 private static final Class THIS_CLASS = ClassUtilUTest.class; 46 47 public ClassUtilUTest( String name ) 48 { 49 super( name ); 50 } 51 52 public static Test suite() 53 { 54 TestSuite suite = new TestSuite( THIS_CLASS ); 55 56 return suite; 57 } 58 59 public static void main( String [] args ) 60 { 61 String [] name = { THIS_CLASS.getName() }; 62 63 66 junit.textui.TestRunner.main( name ); 67 } 68 69 protected void setUp() throws Exception 70 { 71 super.setUp(); 72 73 } 75 76 77 protected void tearDown() throws Exception 78 { 79 81 super.tearDown(); 82 } 83 84 85 86 public void testInstantiate1() 87 { 88 assertNotNull( "Singleton method returned null.", 89 ClassUtil.getInstance() ); 90 } 91 92 93 private static final String BELIEF_CLASS = "BeliefOfTheDay"; 94 95 96 public static class InnerClass 97 { 98 public InnerClass() 99 { 100 } 102 } 103 104 105 public void testGetClass1() 106 { 107 ClassUtil util = startTest(); 108 109 Class c = util.getClass( THIS_CLASS.getName() ); 110 assertNotNull( "getClass( "+THIS_CLASS.getName()+" ) returned null.", 111 c ); 112 assertEquals( "Did not load the class from the default classloader.", 113 THIS_CLASS, c ); 114 } 115 116 117 public void testGetClass2() 118 { 119 ClassUtil util = startTest(); 120 121 Class c = util.getClass( BELIEF_CLASS ); 122 assertEquals( 123 "getClass( Belief ) was found in the default classloader.", 124 null, c ); 125 } 126 127 128 public void testGetClass3() 129 { 130 ClassUtil util = startTest(); 131 132 Class c = util.getClass( THIS_CLASS.getName(), null ); 133 assertNotNull( "getClass( "+THIS_CLASS.getName()+" ) returned null.", 134 c ); 135 assertEquals( "Did not load the class from the default classloader.", 136 THIS_CLASS, c ); 137 } 138 139 140 public void testGetClass4() 141 { 142 ClassUtil util = startTest(); 143 144 Class c = util.getClass( BELIEF_CLASS, null ); 145 assertEquals( 146 "getClass( Belief ) was found in the default classloader.", 147 null, c ); 148 } 149 150 151 public void testFlush1() 152 { 153 ClassUtil util = startTest(); 154 155 } 158 159 160 public void testCreateObject1() 161 { 162 ClassUtil util = startTest(); 163 164 Object o = util.createObject( InnerClass.class.getName() ); 165 assertNotNull( "getClass( "+InnerClass.class.getName()+ 166 " ) returned null.", 167 o ); 168 assertEquals( "Did not load the class from the default classloader.", 169 InnerClass.class, o.getClass() ); 170 } 171 172 173 public void testCreateObject2() 174 { 175 ClassUtil util = startTest(); 176 177 Object o = util.createObject( THIS_CLASS.getName() ); 180 assertEquals( "Incorrectly loaded the class which doesn't have a "+ 181 "default constructor.", 182 null, o ); 183 } 184 185 186 public void testCreateObject3() 187 { 188 ClassUtil util = startTest(); 189 190 Object o = util.createObject( ClassUtil.class.getName() ); 193 if (o != null) 194 { 195 System.out.println("Warning - your JVM allowed the creation of "+ 196 "an object which has a 'protected' level default constructor."); 197 } 198 204 } 205 206 207 public void testCreateObject4() 208 { 209 ClassUtil util = startTest(); 210 211 Object o = util.createObject( BELIEF_CLASS ); 212 assertEquals( 213 "createObject( "+BELIEF_CLASS+" ) incorrectly found a class.", 214 null, o ); 215 } 216 217 218 public void testCreateObject5() 219 { 220 ClassUtil util = startTest(); 221 222 Object o = util.createObject( InnerClass.class.getName(), null ); 223 assertNotNull( "getClass( "+InnerClass.class.getName()+ 224 " ) returned null.", 225 o ); 226 assertEquals( "Did not load the class from the default classloader.", 227 InnerClass.class, o.getClass() ); 228 } 229 230 231 public void testCreateObject6() 232 { 233 ClassUtil util = startTest(); 234 235 Object o = util.createObject( THIS_CLASS.getName(), null ); 238 assertEquals( "Incorrectly loaded the class which doesn't have a "+ 239 "default constructor.", 240 null, o ); 241 } 242 243 244 245 public void testCreateObject7() 246 { 247 ClassUtil util = startTest(); 248 249 assertEquals( "Did not return a null instance with a null class.", 250 null, util.createObject( (Class )null ) ); 251 } 252 253 254 public void testCreateObject8() 255 { 256 ClassUtil util = startTest(); 257 258 Object o = util.createObject( InnerClass.class ); 259 assertNotNull( "Returned null from creating a proper class.", o ); 260 assertEquals( "Did not create a proper class.", 261 InnerClass.class, o.getClass() ); 262 } 263 264 265 public void tsetIsJdk2Compatible1() 266 { 267 ClassUtil util = startTest(); 268 269 util.isJdk2Compatible(); 271 } 272 273 275 276 protected ClassUtil startTest() 279 { 280 ClassUtil util = ClassUtil.getInstance(); 281 util.flush(); 282 return util; 283 } 284 } 285 | Popular Tags |