1 7 package com.inversoft.util.test; 8 9 10 import java.lang.reflect.Method ; 11 import java.security.Permission ; 12 import java.util.AbstractList ; 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 16 import junit.framework.TestCase; 17 18 import com.inversoft.util.BaseException; 19 import com.inversoft.util.ReflectionException; 20 import com.inversoft.util.ReflectionTools; 21 22 23 29 public class ReflectionToolsTest extends TestCase { 30 31 public ReflectionToolsTest(String name) { 32 super(name); 33 } 34 35 36 public void testFindClass() { 37 try { 38 assertSame(ReflectionTools.class, 39 ReflectionTools.findClass("com.inversoft.util.ReflectionTools")); 40 assertSame(ReflectionTools.class, 41 ReflectionTools.findClass("ReflectionTools", "com.inversoft.util")); 42 assertSame(ReflectionTools.class, 43 ReflectionTools.findClass("com.inversoft.util.ReflectionTools", null)); 44 } catch (ReflectionException re) { 45 fail(re.toString()); 46 } 47 48 try { 49 ReflectionTools.findClass("bad.class"); 50 fail("Should have failed"); 51 } catch (ReflectionException e) { 52 } 54 55 try { 56 ReflectionTools.findClass(null); 57 fail("Should have failed"); 58 } catch (Throwable t) { 59 assertSame(AssertionError .class, t.getClass()); 61 } 62 } 63 64 public void testGetMethod() { 65 try { 66 Method m = ReflectionTools.getMethod(Bean.class, "getName", new Class []{}); 67 assertEquals("getName", m.getName()); 68 assertEquals(0, m.getParameterTypes().length); 69 } catch (ReflectionException re) { 70 fail(re.toString()); 71 } 72 73 try { 74 ReflectionTools.getMethod(Bean.class, "bad", new Class []{}); 75 fail("Should have failed"); 76 } catch (ReflectionException re) { 77 assertEquals(NoSuchMethodException .class, re.getCause().getClass()); 78 } 79 80 SecurityManager old = System.getSecurityManager(); 81 try { 82 SecurityManager sm = new SecurityManager () { 83 public void checkMemberAccess(Class clazz, int which) { 84 throw new SecurityException (); 85 } 86 87 public void checkPermission(Permission p) { 88 } 90 }; 91 92 System.setSecurityManager(sm); 93 ReflectionTools.getMethod(Bean.class, "getName", new Class []{}); 94 fail("Should have failed"); 95 } catch (ReflectionException re) { 96 assertEquals(SecurityException .class, re.getCause().getClass()); 98 } finally { 99 System.setSecurityManager(old); 100 } 101 102 old = System.getSecurityManager(); 103 try { 104 SecurityManager sm = new SecurityManager () { 105 public void checkMemberAccess(Class clazz, int which) { 106 throw new SecurityException (); 107 } 108 109 public void checkPermission(Permission p) { 110 } 112 }; 113 114 System.setSecurityManager(sm); 115 ReflectionTools.getMethod(Bean.class, "setName", new Class []{String .class}); 116 fail("Should have failed"); 117 } catch (ReflectionException re) { 118 assertEquals(SecurityException .class, re.getCause().getClass()); 120 } finally { 121 System.setSecurityManager(old); 122 } 123 } 124 125 public void testGetMethods() { 126 try { 127 Method [] m = ReflectionTools.getMethods(Bean.class); 128 assertEquals(14, m.length); 129 assertEquals(Bean.class, m[0].getDeclaringClass()); 130 } catch (ReflectionException re) { 131 fail(re.toString()); 132 } 133 134 SecurityManager old = System.getSecurityManager(); 135 try { 136 SecurityManager sm = new SecurityManager () { 137 public void checkMemberAccess(Class clazz, int which) { 138 throw new SecurityException (); 139 } 140 141 public void checkPermission(Permission p) { 142 } 144 }; 145 146 System.setSecurityManager(sm); 147 ReflectionTools.getMethods(Bean.class); 148 fail("Should have failed"); 149 } catch (ReflectionException re) { 150 assertEquals(SecurityException .class, re.getCause().getClass()); 152 } finally { 153 System.setSecurityManager(old); 154 } 155 } 156 157 public void testInvokeBadClass() { 158 159 try { 160 Method getter = ReflectionTools.getMethod(Bean.class, "getName", new Class []{}); 161 ReflectionTools.invokeMethod(getter, new FileToolsTest("foo"), new Object [0]); 162 fail("Should have failed"); 163 } catch (ReflectionException re) { 164 System.err.println(re.toString()); 165 } 166 } 167 168 public void testInvokeBadParamters() { 169 try { 170 Method getter = ReflectionTools.getMethod(Bean.class, "getName", new Class []{}); 171 ReflectionTools.invokeMethod(getter, new Bean(), new Object []{"foo"}); 172 fail("Should have failed"); 173 } catch (ReflectionException re) { 174 System.err.println(re.toString()); 175 } 176 } 177 178 public void testInvokeException() { 179 try { 180 Method getter = ReflectionTools.getMethod(Bean.class, 181 "throwCheckedMethod", new Class []{}); 182 ReflectionTools.invokeMethod(getter, new Bean(), new Object [0]); 183 fail("Should have failed"); 184 } catch (ReflectionException re) { 185 assertEquals(BaseException.class, re.getTarget().getClass()); 186 } 187 188 try { 189 Method getter = ReflectionTools.getMethod(Bean.class, 190 "throwUncheckedMethod", new Class []{}); 191 ReflectionTools.invokeMethod(getter, new Bean(), new Object [0]); 192 fail("Should have failed"); 193 } catch (ReflectionException re) { 194 fail("Shouldn't have thrown this"); 195 } catch (NullPointerException npe) { 196 } 198 199 try { 200 Method getter = ReflectionTools.getMethod(Bean.class, 201 "assertMethod", new Class []{}); 202 ReflectionTools.invokeMethod(getter, new Bean(), new Object [0]); 203 fail("Should have failed"); 204 } catch (ReflectionException re) { 205 fail("Shouldn't have thrown this"); 206 } catch (Error e) { 207 assertTrue(e instanceof AssertionError ); 208 } 209 210 try { 211 Method hidden = Bean.class.getDeclaredMethod("hidden", new Class [0]); 212 ReflectionTools.invokeMethod(hidden, new Bean(), new Object [0]); 213 fail("Should have failed"); 214 } catch (ReflectionException re) { 215 assertEquals(IllegalAccessException .class, re.getCause().getClass()); 216 } catch (Exception e) { 217 fail(e.toString()); 218 } 219 } 220 221 public void testFindMethod() { 222 223 Method method = ReflectionTools.findMethod(ReflectionToolsTest.class, 224 "overLoadedMethod", new Class []{ArrayList .class}, 0); 225 assertNotNull("Should not be null", method); 226 assertEquals("Should be abstract list version", method.getParameterTypes()[0], 227 AbstractList .class); 228 229 Method method1 = ReflectionTools.findMethod(ReflectionToolsTest.class, 230 "overLoadedMethod", new Class [] {HashMap .class}, 0); 231 assertNotNull("Should not be null", method1); 232 assertEquals("Should be Object version", method1.getParameterTypes()[0], 233 Object .class); 234 235 Method method2 = ReflectionTools.findMethod(ReflectionToolsTest.class, 236 "overLoadedMethod2", new Class [] {HashMap .class}, 0); 237 assertNull("Should be null", method2); 238 239 Class [] params = new Class [] {HashMap .class}; 240 ReflectionTools.findMethod(ReflectionToolsTest.class, 241 "overLoadedMethod", params, 0); 242 assertEquals("Should be Object version", method1.getParameterTypes()[0], 243 Object .class); 244 assertEquals("Should not have changed array", params.length, 1); 245 assertEquals("Should not have changed array values", params[0], HashMap .class); 246 } 247 248 public void testInstantiate() { 249 try { 250 Object obj = ReflectionTools.instantiate(Bean.class); 251 assertNotNull(obj); 252 assertTrue(obj instanceof Bean); 253 } catch (ReflectionException re) { 254 fail(re.toString()); 255 } 256 257 try { 258 ReflectionTools.instantiate(PrivateBean.class); 259 fail("Should have failed"); 260 } catch (ReflectionException re) { 261 assertEquals(IllegalAccessException .class, re.getCause().getClass()); 262 } 263 264 try { 265 ReflectionTools.instantiate(ExceptionBean.class); 266 fail("Should have failed"); 267 } catch (ReflectionException re) { 268 assertEquals(InstantiationException .class, re.getCause().getClass()); 269 } 270 271 try { 272 Object obj = ReflectionTools.instantiate("com.inversoft.util.test.Bean"); 273 assertNotNull(obj); 274 assertTrue(obj instanceof Bean); 275 } catch (ReflectionException re) { 276 fail(re.toString()); 277 } 278 279 try { 280 ReflectionTools.instantiate("bad.class"); 281 fail("Should have failed"); 282 } catch (ReflectionException re) { 283 assertEquals(ClassNotFoundException .class, re.getCause().getClass()); 284 } 285 286 SecurityManager old = System.getSecurityManager(); 287 try { 288 SecurityManager sm = new SecurityManager () { 289 public void checkMemberAccess(Class clazz, int which) { 290 throw new SecurityException (); 291 } 292 293 public void checkPermission(Permission p) { 294 } 296 }; 297 298 System.setSecurityManager(sm); 299 ReflectionTools.instantiate(Bean.class); 300 fail("Should have failed"); 301 } catch (ReflectionException re) { 302 assertEquals(SecurityException .class, re.getCause().getClass()); 304 } finally { 305 System.setSecurityManager(old); 306 } 307 } 308 309 public void testConvertToWrapper() { 310 assertEquals(Boolean .class, ReflectionTools.convertToWrapper(Boolean.TYPE)); 311 assertEquals(Byte .class, ReflectionTools.convertToWrapper(Byte.TYPE)); 312 assertEquals(Character .class, ReflectionTools.convertToWrapper(Character.TYPE)); 313 assertEquals(Short .class, ReflectionTools.convertToWrapper(Short.TYPE)); 314 assertEquals(Integer .class, ReflectionTools.convertToWrapper(Integer.TYPE)); 315 assertEquals(Long .class, ReflectionTools.convertToWrapper(Long.TYPE)); 316 assertEquals(Float .class, ReflectionTools.convertToWrapper(Float.TYPE)); 317 assertEquals(Double .class, ReflectionTools.convertToWrapper(Double.TYPE)); 318 assertNull(ReflectionTools.convertToWrapper(String .class)); 319 } 320 321 public void overLoadedMethod(AbstractList param) { 322 } 324 325 public void overLoadedMethod(Object param) { 326 } 328 329 public void overLoadedMethod2(ArrayList param) { 330 } 332 } | Popular Tags |