1 7 package com.inversoft.beans.test; 8 9 10 import java.lang.reflect.Method ; 11 import java.util.List ; 12 13 import junit.framework.TestCase; 14 15 import com.inversoft.beans.BeanException; 16 import com.inversoft.beans.JavaBeanTools; 17 import com.inversoft.util.ReflectionException; 18 import com.inversoft.util.ReflectionTools; 19 20 21 26 public class JavaBeanToolsTest extends TestCase { 27 28 public JavaBeanToolsTest(String name) { 29 super(name); 30 } 31 32 33 public void testInheritance() { 34 35 try { 36 Method method = JavaBeanTools.findReadMethod("fromSuper", Bean1.class); 37 assertTrue("method should never be null", method != null); 38 } catch (BeanException be) { 39 fail(be.toString()); 40 } 41 } 42 43 46 public void testGetPropertyName() { 47 try { 48 Method method = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class [0]); 49 assertEquals("property1", JavaBeanTools.getPropertyName(method)); 50 method = ReflectionTools.getMethod(Bean1.class, "setString1", new Class []{String .class}); 51 assertEquals("string1", JavaBeanTools.getPropertyName(method)); 52 method = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class [0]); 53 assertEquals("boolean1", JavaBeanTools.getPropertyName(method)); 54 55 method = ReflectionTools.getMethod(Bean1.class, "get", new Class [0]); 56 assertNull(JavaBeanTools.getPropertyName(method)); 57 method = ReflectionTools.getMethod(Bean1.class, "set", new Class []{String .class}); 58 assertNull(JavaBeanTools.getPropertyName(method)); 59 method = ReflectionTools.getMethod(Bean1.class, "is", new Class [0]); 60 assertNull(JavaBeanTools.getPropertyName(method)); 61 } catch (ReflectionException e) { 62 fail(e.toString()); 63 } 64 65 try { 66 Method method = ReflectionTools.getMethod(Bean1.class, "notAProp", new Class [0]); 67 assertNull(JavaBeanTools.getPropertyName(method)); 68 } catch (ReflectionException e) { 69 fail(e.toString()); 70 } 71 } 72 73 76 public void testCapitalize() { 77 assertEquals("UpperCase", JavaBeanTools.capitalize("upperCase")); 78 assertEquals("URL", JavaBeanTools.capitalize("URL")); 79 assertEquals(" a", JavaBeanTools.capitalize(" a")); 80 assertEquals("Done", JavaBeanTools.capitalize("Done")); 81 } 82 83 86 public void testMaking() { 87 assertEquals("getFoo", JavaBeanTools.makeGetter("foo")); 88 assertEquals("setFoo", JavaBeanTools.makeSetter("foo")); 89 assertEquals("isFoo", JavaBeanTools.makeIs("foo")); 90 assertEquals("handleFoo", JavaBeanTools.makeHandle("foo")); 91 } 92 93 96 public void testChecking() { 97 try { 98 Method method = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class [0]); 99 assertTrue(JavaBeanTools.isValidGetter(method)); 100 method = ReflectionTools.getMethod(Bean1.class, "setString1", new Class []{String .class}); 101 assertTrue(JavaBeanTools.isValidSetter(method)); 102 method = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class [0]); 103 assertTrue(JavaBeanTools.isValidGetter(method)); 104 105 method = ReflectionTools.getMethod(Bean1.class, "get", new Class [0]); 106 assertFalse(JavaBeanTools.isValidGetter(method)); 107 assertTrue(JavaBeanTools.isValidGetter(method, false)); 108 method = ReflectionTools.getMethod(Bean1.class, "set", new Class []{String .class}); 109 assertFalse(JavaBeanTools.isValidSetter(method)); 110 assertTrue(JavaBeanTools.isValidSetter(method, false)); 111 method = ReflectionTools.getMethod(Bean1.class, "is", new Class [0]); 112 assertFalse(JavaBeanTools.isValidGetter(method)); 113 assertTrue(JavaBeanTools.isValidGetter(method, false)); 114 115 method = ReflectionTools.getMethod(Bean1.class, "get", new Class [0]); 116 assertFalse(JavaBeanTools.isValidGetter(method)); 117 assertFalse(JavaBeanTools.isValidGetter(method, true)); 118 method = ReflectionTools.getMethod(Bean1.class, "set", new Class []{String .class}); 119 assertFalse(JavaBeanTools.isValidSetter(method)); 120 assertFalse(JavaBeanTools.isValidSetter(method, true)); 121 method = ReflectionTools.getMethod(Bean1.class, "is", new Class [0]); 122 assertFalse(JavaBeanTools.isValidGetter(method)); 123 assertFalse(JavaBeanTools.isValidGetter(method, true)); 124 } catch (ReflectionException e) { 125 fail(e.toString()); 126 } 127 128 try { 130 Method method = ReflectionTools.getMethod(Bean1.class, "getStringIndexed", new Class []{Integer.TYPE}); 131 assertTrue(JavaBeanTools.isValidIndexedGetter(method)); 132 method = ReflectionTools.getMethod(Bean1.class, "setStringIndexed", new Class []{Integer.TYPE, String .class}); 133 assertTrue(JavaBeanTools.isValidIndexedSetter(method)); 134 method = ReflectionTools.getMethod(Bean1.class, "isBooleanIndexed", new Class []{Integer.TYPE}); 135 assertTrue(JavaBeanTools.isValidIndexedGetter(method)); 136 137 method = ReflectionTools.getMethod(Bean1.class, "get", new Class []{Integer.TYPE}); 138 assertFalse(JavaBeanTools.isValidIndexedGetter(method)); 139 assertTrue(JavaBeanTools.isValidIndexedGetter(method, false)); 140 method = ReflectionTools.getMethod(Bean1.class, "set", new Class []{Integer.TYPE, String .class}); 141 assertFalse(JavaBeanTools.isValidIndexedSetter(method)); 142 assertTrue(JavaBeanTools.isValidIndexedSetter(method, false)); 143 method = ReflectionTools.getMethod(Bean1.class, "is", new Class []{Integer.TYPE}); 144 assertFalse(JavaBeanTools.isValidIndexedGetter(method)); 145 assertTrue(JavaBeanTools.isValidIndexedGetter(method, false)); 146 147 method = ReflectionTools.getMethod(Bean1.class, "get", new Class []{Integer.TYPE}); 148 assertFalse(JavaBeanTools.isValidIndexedGetter(method)); 149 assertFalse(JavaBeanTools.isValidIndexedGetter(method, true)); 150 method = ReflectionTools.getMethod(Bean1.class, "set", new Class []{Integer.TYPE, String .class}); 151 assertFalse(JavaBeanTools.isValidIndexedSetter(method)); 152 assertFalse(JavaBeanTools.isValidIndexedSetter(method, true)); 153 method = ReflectionTools.getMethod(Bean1.class, "is", new Class []{Integer.TYPE}); 154 assertFalse(JavaBeanTools.isValidIndexedGetter(method)); 155 assertFalse(JavaBeanTools.isValidIndexedGetter(method, true)); 156 } catch (ReflectionException e) { 157 fail(e.toString()); 158 } 159 } 160 161 164 public void testFind() { 165 try { 166 Method m1 = ReflectionTools.getMethod(Bean1.class, "getProperty1", new Class [0]); 167 Method m2 = JavaBeanTools.findReadMethod("property1", Bean1.class); 168 assertNotNull(m2); 169 assertEquals(m1, m2); 170 m1 = ReflectionTools.getMethod(Bean1.class, "setString1", new Class []{String .class}); 171 m2 = JavaBeanTools.findWriteMethod("string1", Bean1.class, String .class); 172 assertNotNull(m2); 173 assertEquals(m1, m2); 174 m1 = ReflectionTools.getMethod(Bean1.class, "isBoolean1", new Class [0]); 175 m2 = JavaBeanTools.findReadMethod("boolean1", Bean1.class); 176 assertNotNull(m2); 177 assertEquals(m1, m2); 178 } catch (ReflectionException e) { 179 fail(e.toString()); 180 } 181 182 try { 184 Method m1 = ReflectionTools.getMethod(Bean1.class, "getStringIndexed", new Class []{Integer.TYPE}); 185 Method m2 = JavaBeanTools.findIndexedReadMethod("stringIndexed", Bean1.class); 186 assertNotNull(m2); 187 assertEquals(m1, m2); 188 m1 = ReflectionTools.getMethod(Bean1.class, "setStringIndexed", new Class []{Integer.TYPE, String .class}); 189 m2 = JavaBeanTools.findIndexedWriteMethod("stringIndexed", Bean1.class, String .class); 190 assertNotNull(m2); 191 assertEquals(m1, m2); 192 m1 = ReflectionTools.getMethod(Bean1.class, "isBooleanIndexed", new Class []{Integer.TYPE}); 193 m2 = JavaBeanTools.findIndexedReadMethod("booleanIndexed", Bean1.class); 194 assertNotNull(m2); 195 assertEquals(m1, m2); 196 } catch (ReflectionException e) { 197 fail(e.toString()); 198 } 199 200 try { 202 JavaBeanTools.findReadMethod("badProp", Bean1.class); 203 fail("Should have failed"); 204 } catch (BeanException be) { 205 } 207 try { 208 JavaBeanTools.findWriteMethod("badProp", Bean1.class, String .class); 209 fail("Should have failed"); 210 } catch (BeanException be) { 211 } 213 try { 214 JavaBeanTools.findReadMethod("badPropBool", Bean1.class); 215 fail("Should have failed"); 216 } catch (BeanException be) { 217 } 219 try { 220 JavaBeanTools.findIndexedReadMethod("badProp", Bean1.class); 221 fail("Should have failed"); 222 } catch (BeanException be) { 223 } 225 try { 226 JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, String .class); 227 fail("Should have failed"); 228 } catch (BeanException be) { 229 } 231 try { 232 JavaBeanTools.findIndexedReadMethod("badPropBool", Bean1.class); 233 fail("Should have failed"); 234 } catch (BeanException be) { 235 } 237 238 try { 240 JavaBeanTools.findWriteMethod("badProp", Bean1.class, Void.TYPE); 241 fail("Should have failed"); 242 } catch (BeanException be) { 243 } 245 try { 246 JavaBeanTools.findWriteMethod("badProp", Bean1.class, null); 247 fail("Should have failed"); 248 } catch (BeanException be) { 249 } 251 try { 252 JavaBeanTools.findWriteMethod(" ", Bean1.class, String .class); 253 fail("Should have failed"); 254 } catch (BeanException be) { 255 } 257 try { 258 JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, Void.TYPE); 259 fail("Should have failed"); 260 } catch (BeanException be) { 261 } 263 try { 264 JavaBeanTools.findIndexedWriteMethod("badProp", Bean1.class, null); 265 fail("Should have failed"); 266 } catch (BeanException be) { 267 } 269 try { 270 JavaBeanTools.findIndexedWriteMethod(" ", Bean1.class, String .class); 271 fail("Should have failed"); 272 } catch (BeanException be) { 273 } 275 } 276 277 280 public void testSplit() { 281 JavaBeanTools.NameInfo ni = JavaBeanTools.splitNameBack("foo.bar.john"); 282 assertTrue(ni.indexOfDot != -1); 283 assertTrue(ni.nested); 284 assertEquals("foo.bar", ni.nestedPropertyName); 285 assertEquals("john", ni.localPropertyName); 286 ni = JavaBeanTools.splitNameBack("foo"); 287 assertTrue(ni.indexOfDot == -1); 288 assertFalse(ni.nested); 289 assertEquals(null, ni.nestedPropertyName); 290 assertEquals("foo", ni.localPropertyName); 291 292 ni = JavaBeanTools.splitNameFront("foo.bar.john"); 293 assertTrue(ni.indexOfDot != -1); 294 assertTrue(ni.nested); 295 assertEquals("bar.john", ni.nestedPropertyName); 296 assertEquals("foo", ni.localPropertyName); 297 ni = JavaBeanTools.splitNameFront("foo"); 298 assertTrue(ni.indexOfDot == -1); 299 assertFalse(ni.nested); 300 assertEquals(null, ni.nestedPropertyName); 301 assertEquals("foo", ni.localPropertyName); 302 303 String [] names = JavaBeanTools.splitNameComplete("foo.bar.john"); 304 assertEquals(3, names.length); 305 assertEquals("foo", names[0]); 306 assertEquals("bar", names[1]); 307 assertEquals("john", names[2]); 308 309 names = JavaBeanTools.splitNameComplete("bar"); 310 assertEquals(1, names.length); 311 assertEquals("bar", names[0]); 312 313 names = JavaBeanTools.splitNameComplete(""); 314 assertEquals(0, names.length); 315 } 316 317 320 public void testPropertyInfo() { 321 new JavaBeanTools.PropertyInfo(); 322 try { 323 JavaBeanTools.PropertyInfo pi = 324 JavaBeanTools.retrievePropertyInfo("prop[0][\"foo\"]"); 325 assertEquals("prop", pi.propertyName); 326 assertEquals(new Integer (0), pi.indices.get(0)); 327 assertEquals("foo", pi.indices.get(1)); 328 } catch (BeanException e) { 329 fail(e.toString()); 330 } 331 332 try { 333 JavaBeanTools.retrievePropertyInfo("prop[0"); 334 fail("Should have failed"); 335 } catch (BeanException e) { 336 } 338 339 try { 340 JavaBeanTools.retrievePropertyInfo("prop[\"0]"); 341 fail("Should have failed"); 342 } catch (BeanException e) { 343 } 345 346 try { 347 JavaBeanTools.retrievePropertyInfo("prop['0]"); 348 fail("Should have failed"); 349 } catch (BeanException e) { 350 } 352 } 353 354 357 public void testPropertyInfoList() { 358 try { 359 List list = JavaBeanTools.retrieveAllPropertyInfo("prop[0][\"foo\"].prop2[9]"); 360 assertEquals(2, list.size()); 361 362 JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0); 363 JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1); 364 assertEquals("prop", pi1.propertyName); 365 assertEquals(new Integer (0), pi1.indices.get(0)); 366 assertEquals("foo", pi1.indices.get(1)); 367 368 assertEquals("prop2", pi2.propertyName); 369 assertEquals(new Integer (9), pi2.indices.get(0)); 370 } catch (BeanException e) { 371 fail(e.toString()); 372 } 373 } 374 375 378 public void testPropertyInfoListSinlgeQuote() { 379 try { 380 List list = JavaBeanTools.retrieveAllPropertyInfo("prop[0]['foo'].prop2[9]"); 381 assertEquals(2, list.size()); 382 383 JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0); 384 JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1); 385 assertEquals("prop", pi1.propertyName); 386 assertEquals(new Integer (0), pi1.indices.get(0)); 387 assertEquals("foo", pi1.indices.get(1)); 388 389 assertEquals("prop2", pi2.propertyName); 390 assertEquals(new Integer (9), pi2.indices.get(0)); 391 } catch (BeanException e) { 392 fail(e.toString()); 393 } 394 } 395 396 399 public void testPropertyInfoListNoQuote() { 400 try { 401 List list = JavaBeanTools.retrieveAllPropertyInfo("prop[0][foo].prop2[9]"); 402 assertEquals(2, list.size()); 403 404 JavaBeanTools.PropertyInfo pi1 = (JavaBeanTools.PropertyInfo) list.get(0); 405 JavaBeanTools.PropertyInfo pi2 = (JavaBeanTools.PropertyInfo) list.get(1); 406 assertEquals("prop", pi1.propertyName); 407 assertEquals(new Integer (0), pi1.indices.get(0)); 408 assertEquals("foo", pi1.indices.get(1)); 409 410 assertEquals("prop2", pi2.propertyName); 411 assertEquals(new Integer (9), pi2.indices.get(0)); 412 } catch (BeanException e) { 413 fail(e.toString()); 414 } 415 } 416 } | Popular Tags |