1 3 package jodd.util; 4 5 import java.io.Serializable ; 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Field ; 8 9 import junit.framework.TestCase; 10 import jodd.util.testdata.*; 11 import jodd.util.testdata2.D; 12 import jodd.util.testdata2.E; 13 14 public class ReflectUtilTest extends TestCase { 15 16 public void testInvoke() { 17 TFooBean bean = new TFooBean(); 18 19 String result; 20 try { 21 result = (String ) ReflectUtil.invoke(TFooBean.class, bean, "getPublic", null, null); 22 assertEquals("public", result); 23 result = (String ) ReflectUtil.invoke(bean, "getPublic", null, null); 24 assertEquals("public", result); 25 result = (String ) ReflectUtil.invoke(bean, "getPublic", null); 26 assertEquals("public", result); 27 } catch (Exception e) { 28 fail("ReflectUtil.invoke() failed " + e.toString()); 29 } 30 31 try { 32 ReflectUtil.invoke(TFooBean.class, bean, "getDefault", null, null); 33 fail("ReflectUtil.invoke() works irregular!"); 34 } catch (Exception e) {} 35 36 try { 37 ReflectUtil.invoke(TFooBean.class, bean, "getProtected", null, null); 38 fail("ReflectUtil.invoke() works irregular!"); 39 } catch (Exception e) {} 40 41 try { 42 ReflectUtil.invoke(TFooBean.class, bean, "getPrivate", null, null); 43 fail("ReflectUtil.invoke() works irregular!"); 44 } catch (Exception e) {} 45 } 46 47 48 public void testInvokeEx() { 49 TFooBean bean = new TFooBean(); 50 51 String result; 52 try { 53 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getPublic", null, null); 54 assertEquals("public", result); 55 result = (String ) ReflectUtil.invokeDeclared(bean, "getPublic", null, null); 56 assertEquals("public", result); 57 result = (String ) ReflectUtil.invokeDeclared(bean, "getPublic", null); 58 assertEquals("public", result); 59 } catch (Exception e) { 60 fail("ReflectUtil.invoke() failed " + e.toString()); 61 } 62 63 try { 64 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getDefault", null, null); 65 assertEquals("default", result); 66 result = (String ) ReflectUtil.invokeDeclared(bean, "getDefault", null, null); 67 assertEquals("default", result); 68 } catch (Exception e) { 69 fail("ReflectUtil.invoke() failed " + e.toString()); 70 } 71 72 try { 73 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getProtected", null, null); 74 assertEquals("protected", result); 75 result = (String ) ReflectUtil.invokeDeclared(bean, "getProtected", null, null); 76 assertEquals("protected", result); 77 } catch (Exception e) { 78 fail("ReflectUtil.invoke() failed " + e.toString()); 79 } 80 81 try { 82 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getPrivate", null, null); 83 assertEquals("private", result); 84 result = (String ) ReflectUtil.invokeDeclared(bean, "getPrivate", null); 85 assertEquals("private", result); 86 } catch (Exception e) { 87 fail("ReflectUtil.invoke() failed " + e.toString()); 88 } 89 90 } 91 92 public void testInvoke2() { 93 TFooBean bean = new TFooBean(); 94 String result; 95 try { 96 result = (String ) ReflectUtil.invoke(TFooBean.class, bean, "getMore", new Class [] {String .class, Integer .class}, new Object [] {"qwerty", new Integer (173)}); 97 assertEquals("qwerty173", result); 98 result = (String ) ReflectUtil.invoke(TFooBean.class, bean, "getMore", new Object [] {"Qwerty", new Integer (173)}); 99 assertEquals("Qwerty173", result); 100 result = (String ) ReflectUtil.invoke(bean, "getMore", new Class [] {String .class, Integer .class}, new Object [] {"QWerty", new Integer (173)}); 101 assertEquals("QWerty173", result); 102 result = (String ) ReflectUtil.invoke(bean, "getMore", new Object [] {"QWErty", new Integer (173)}); 103 assertEquals("QWErty173", result); 104 105 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getMore", new Class [] {String .class, Integer .class}, new Object [] {"qwerty", new Integer (173)}); 106 assertEquals("qwerty173", result); 107 result = (String ) ReflectUtil.invokeDeclared(TFooBean.class, bean, "getMore", new Object [] {"Qwerty", new Integer (173)}); 108 assertEquals("Qwerty173", result); 109 result = (String ) ReflectUtil.invokeDeclared(bean, "getMore", new Class [] {String .class, Integer .class}, new Object [] {"QWerty", new Integer (173)}); 110 assertEquals("QWerty173", result); 111 result = (String ) ReflectUtil.invokeDeclared(bean, "getMore", new Object [] {"QWErty", new Integer (173)}); 112 assertEquals("QWErty173", result); 113 } catch (Exception e) { 114 fail("ReflectUtil.invoke() failed " + e.toString()); 115 } 116 } 117 118 119 public void testMethod0() { 120 TFooBean bean = new TFooBean(); 121 Method m; 122 m = ReflectUtil.getMethod0(TFooBean.class, "getMore", new Class [] {String .class, Integer .class}); 123 assertNotNull(m); 124 125 m = ReflectUtil.getMethod0(bean, "getMore", new Class [] {String .class, Integer .class}); 126 assertNotNull(m); 127 128 m = ReflectUtil.getMethod0(bean, "getXXX", new Class [] {String .class, Integer .class}); 129 assertNull(m); 130 131 m = ReflectUtil.getMethod0(bean, "getPublic", null); 132 assertNotNull(m); 133 134 m = ReflectUtil.getMethod0(bean, "getDefault", null); 135 assertNull(m); 136 137 m = ReflectUtil.getMethod0(bean, "getProtected", null); 138 assertNull(m); 139 140 m = ReflectUtil.getMethod0(bean, "getPrivate", null); 141 assertNull(m); 142 } 143 144 145 public void testMethod() { 146 TFooBean bean = new TFooBean(); 147 Method m; 148 m = ReflectUtil.findMethod(TFooBean.class, "getMore"); 149 assertNotNull(m); 150 151 m = ReflectUtil.findMethod(bean.getClass(), "getMore"); 152 assertNotNull(m); 153 154 m = ReflectUtil.findMethod(bean.getClass(), "getXXX"); 155 assertNull(m); 156 } 157 158 159 public void testMatchClasses() { 160 TFooBean a = new TFooBean(); 161 TFooBean b = new TFooBean(); 162 TFooBean2 c = new TFooBean2(); 163 164 assertTrue(TFooBean.class.isInstance(a)); 165 assertTrue(ReflectUtil.isSubclass(TFooBean.class, a.getClass())); 166 assertTrue(ReflectUtil.isSubclass(TFooBean.class, b.getClass())); 167 assertTrue(ReflectUtil.isSubclass(a.getClass(), b.getClass())); 168 assertTrue(ReflectUtil.isSubclass(b.getClass(), a.getClass())); 169 170 assertTrue(ReflectUtil.isSubclass(TFooBean2.class, c.getClass())); 171 assertTrue(ReflectUtil.isSubclass(TFooBean2.class, TFooBean.class)); 172 assertFalse(ReflectUtil.isSubclass(TFooBean.class, TFooBean2.class)); 173 assertTrue(ReflectUtil.isSubclass(c.getClass(), TFooBean.class)); 174 assertFalse(ReflectUtil.isSubclass(a.getClass(), TFooBean2.class)); 175 176 assertTrue(ReflectUtil.isSubclass(TFooBean.class, Serializable .class)); 177 assertTrue(Serializable .class.isInstance(c)); 178 assertTrue(c instanceof Serializable ); 180 assertTrue(ReflectUtil.isInstanceOf(c, Serializable .class)); 181 assertTrue(ReflectUtil.isSubclass(TFooBean2.class, Serializable .class)); 182 assertTrue(ReflectUtil.isSubclass(TFooBean2.class, Comparable .class)); 183 assertFalse(ReflectUtil.isSubclass(TFooBean.class, Comparable .class)); 184 185 assertTrue(ReflectUtil.isSubclass(TFooBean.class, TFooIndyEx.class)); 186 assertTrue(ReflectUtil.isSubclass(TFooBean2.class, TFooIndyEx.class)); 187 assertTrue(ReflectUtil.isSubclass(TFooBean.class, TFooIndy.class)); 188 } 189 190 191 public void testAccessibleA() { 192 Method [] ms = ReflectUtil.getAccessibleMethods(A.class, null); 193 assertEquals(4 + 11, ms.length); ms = ReflectUtil.getAccessibleMethods(A.class); 195 assertEquals(4, ms.length); 196 ms = A.class.getMethods(); 197 assertEquals(1 + 9, ms.length); ms = A.class.getDeclaredMethods(); 199 assertEquals(4, ms.length); 200 ms = ReflectUtil.getSupportedMethods(A.class, null); 201 assertEquals(4 + 12, ms.length); ms = ReflectUtil.getSupportedMethods(A.class); 203 assertEquals(4, ms.length); 204 205 206 Field [] fs = ReflectUtil.getAccessibleFields(A.class); 207 assertEquals(4, fs.length); 208 fs = A.class.getFields(); 209 assertEquals(1, fs.length); 210 fs = A.class.getDeclaredFields(); 211 assertEquals(4, fs.length); 212 fs = ReflectUtil.getSupportedFields(A.class); 213 assertEquals(4, fs.length); 214 } 215 216 public void testAccessibleB() { 217 Method [] ms = ReflectUtil.getAccessibleMethods(B.class, null); 218 assertEquals(3 + 11, ms.length); 219 ms = ReflectUtil.getAccessibleMethods(B.class); 220 assertEquals(3, ms.length); 221 ms = B.class.getMethods(); 222 assertEquals(1 + 9, ms.length); 223 ms = B.class.getDeclaredMethods(); 224 assertEquals(0, ms.length); 225 ms = ReflectUtil.getSupportedMethods(B.class, null); 226 assertEquals(4 + 12, ms.length); 227 ms = ReflectUtil.getSupportedMethods(B.class); 228 assertEquals(4, ms.length); 229 230 231 Field [] fs = ReflectUtil.getAccessibleFields(B.class); 232 assertEquals(3, fs.length); 233 fs = B.class.getFields(); 234 assertEquals(1, fs.length); 235 fs = B.class.getDeclaredFields(); 236 assertEquals(0, fs.length); 237 fs = ReflectUtil.getSupportedFields(B.class); 238 assertEquals(4, fs.length); 239 } 240 241 public void testAccessibleC() { 242 Method [] ms = ReflectUtil.getAccessibleMethods(C.class, null); 243 assertEquals(5 + 11, ms.length); 244 ms = ReflectUtil.getAccessibleMethods(C.class); 245 assertEquals(5, ms.length); 246 ms = C.class.getMethods(); 247 assertEquals(2 + 9, ms.length); 248 ms = C.class.getDeclaredMethods(); 249 assertEquals(5, ms.length); 250 ms = ReflectUtil.getSupportedMethods(C.class, null); 251 assertEquals(5 + 12, ms.length); 252 ms = ReflectUtil.getSupportedMethods(C.class); 253 assertEquals(5, ms.length); 254 255 256 Field [] fs = ReflectUtil.getAccessibleFields(C.class); 257 assertEquals(5, fs.length); 258 fs = C.class.getFields(); 259 assertEquals(3, fs.length); 260 fs = C.class.getDeclaredFields(); 261 assertEquals(5, fs.length); 262 fs = ReflectUtil.getSupportedFields(C.class); 263 assertEquals(5, fs.length); 264 } 265 266 public void testAccessibleD() { 267 Method [] ms = ReflectUtil.getAccessibleMethods(D.class, null); 268 assertEquals(3 + 11, ms.length); 269 ms = ReflectUtil.getAccessibleMethods(D.class); 270 assertEquals(3, ms.length); 271 ms = D.class.getMethods(); 272 assertEquals(2 + 9, ms.length); 273 ms = D.class.getDeclaredMethods(); 274 assertEquals(0, ms.length); 275 ms = ReflectUtil.getSupportedMethods(D.class, null); 276 assertEquals(5 + 12, ms.length); 277 ms = ReflectUtil.getSupportedMethods(D.class); 278 assertEquals(5, ms.length); 279 280 Field [] fs = ReflectUtil.getAccessibleFields(D.class); 281 assertEquals(3, fs.length); 282 fs = D.class.getFields(); 283 assertEquals(3, fs.length); 284 fs = D.class.getDeclaredFields(); 285 assertEquals(0, fs.length); 286 fs = ReflectUtil.getSupportedFields(D.class); 287 assertEquals(5, fs.length); 288 } 289 290 public void testAccessibleE() { 291 Method [] ms = ReflectUtil.getAccessibleMethods(E.class, null); 292 assertEquals(5 + 11, ms.length); 293 ms = ReflectUtil.getAccessibleMethods(E.class); 294 assertEquals(5, ms.length); 295 ms = E.class.getMethods(); 296 assertEquals(2 + 9, ms.length); 297 ms = E.class.getDeclaredMethods(); 298 assertEquals(4, ms.length); 299 ms = ReflectUtil.getSupportedMethods(E.class, null); 300 assertEquals(5 + 12, ms.length); 301 ms = ReflectUtil.getSupportedMethods(E.class); 302 assertEquals(5, ms.length); 303 304 Field [] fs = ReflectUtil.getAccessibleFields(E.class); 305 assertEquals(5, fs.length); 306 fs = E.class.getFields(); 307 assertEquals(4, fs.length); 308 fs = E.class.getDeclaredFields(); 309 assertEquals(4, fs.length); 310 fs = ReflectUtil.getSupportedFields(E.class); 311 assertEquals(5, fs.length); 312 } 313 } 314 | Popular Tags |