1 15 package org.apache.hivemind.util; 16 17 import java.awt.Image ; 18 import java.beans.BeanDescriptor ; 19 import java.beans.BeanInfo ; 20 import java.beans.EventSetDescriptor ; 21 import java.beans.MethodDescriptor ; 22 import java.beans.PropertyDescriptor ; 23 import java.io.InputStream ; 24 import java.util.Collections ; 25 import java.util.List ; 26 27 import org.apache.hivemind.ApplicationRuntimeException; 28 import org.apache.hivemind.test.HiveMindTestCase; 29 30 35 public class TestPropertyUtils extends HiveMindTestCase 36 { 37 public static class Bean 38 { 39 private int _value; 40 41 public int getValue() 42 { 43 return _value; 44 } 45 46 public void setValue(int value) 47 { 48 _value = value; 49 } 50 51 public String toString() 52 { 53 return "PropertyUtilsTestBean"; 54 } 55 56 public void setWriteOnly(boolean b) 57 { 58 } 59 } 60 61 public static class ExceptionBean 62 { 63 public boolean getFailure() 64 { 65 throw new RuntimeException ("getFailure"); 66 } 67 68 public void setFailure(boolean b) 69 { 70 throw new RuntimeException ("setFailure"); 71 } 72 73 public String toString() 74 { 75 return "PropertyUtilsExceptionBean"; 76 } 77 } 78 79 public static class UglyBean 80 { 81 } 82 83 public static class UglyBeanBeanInfo implements BeanInfo 84 { 85 86 public BeanInfo [] getAdditionalBeanInfo() 87 { 88 return null; 89 } 90 91 public BeanDescriptor getBeanDescriptor() 92 { 93 return null; 94 } 95 96 public int getDefaultEventIndex() 97 { 98 return 0; 99 } 100 101 public int getDefaultPropertyIndex() 102 { 103 return 0; 104 } 105 106 public EventSetDescriptor [] getEventSetDescriptors() 107 { 108 return null; 109 } 110 111 public Image getIcon(int iconKind) 112 { 113 return null; 114 } 115 116 public MethodDescriptor [] getMethodDescriptors() 117 { 118 return null; 119 } 120 121 public PropertyDescriptor [] getPropertyDescriptors() 122 { 123 throw new RuntimeException ("This is the UglyBean."); 124 } 125 126 } 127 128 129 130 public static class BooleanHolder 131 { 132 private boolean _flag; 133 134 public boolean isFlag() 135 { 136 return _flag; 137 } 138 139 public void setFlag(boolean flag) 140 { 141 _flag = flag; 142 } 143 } 144 145 146 147 public static class MultiPropertyBean extends BooleanHolder 148 { 149 private String _string; 150 151 private long _long; 152 153 private Integer _integer; 154 155 public Integer getInteger() 156 { 157 return _integer; 158 } 159 160 public void setInteger(Integer integer) 161 { 162 _integer = integer; 163 } 164 165 public long getLong() 166 { 167 return _long; 168 } 169 170 public void setLong(long l) 171 { 172 _long = l; 173 } 174 175 public String getString() 176 { 177 return _string; 178 } 179 180 public void setString(String string) 181 { 182 _string = string; 183 } 184 } 185 186 187 188 public static class StreamHolder 189 { 190 private InputStream _stream; 191 192 public InputStream getStream() 193 { 194 return _stream; 195 } 196 197 public void setStream(InputStream stream) 198 { 199 _stream = stream; 200 } 201 202 } 203 204 public void testRead() 205 { 206 Bean b = new Bean(); 207 208 b.setValue(37); 209 210 assertEquals(new Integer (37), PropertyUtils.read(b, "value")); 211 } 212 213 public void testWrite() 214 { 215 Bean b = new Bean(); 216 217 PropertyUtils.write(b, "value", new Integer (412)); 218 219 assertEquals(412, b.getValue()); 220 } 221 222 public void testMissingProperty() 223 { 224 Bean b = new Bean(); 225 226 try 227 { 228 PropertyUtils.read(b, "zaphod"); 229 230 unreachable(); 231 } 232 catch (ApplicationRuntimeException ex) 233 { 234 assertEquals("Class org.apache.hivemind.util.TestPropertyUtils$Bean does not " 235 + "contain a property named 'zaphod'.", ex.getMessage()); 236 assertEquals(b, ex.getComponent()); 237 } 238 } 239 240 public void testReadOnly() 241 { 242 Bean b = new Bean(); 243 244 try 245 { 246 PropertyUtils.write(b, "class", null); 247 unreachable(); 248 } 249 catch (ApplicationRuntimeException ex) 250 { 251 assertEquals("Property class of object PropertyUtilsTestBean is read-only.", ex 252 .getMessage()); 253 assertEquals(b, ex.getComponent()); 254 } 255 } 256 257 public void testWriteOnly() 258 { 259 Bean b = new Bean(); 260 261 try 262 { 263 PropertyUtils.read(b, "writeOnly"); 264 unreachable(); 265 } 266 catch (ApplicationRuntimeException ex) 267 { 268 assertEquals("Property writeOnly of object PropertyUtilsTestBean is write-only.", ex 269 .getMessage()); 270 assertEquals(b, ex.getComponent()); 271 } 272 } 273 274 public void testReadFailure() 275 { 276 ExceptionBean b = new ExceptionBean(); 277 278 try 279 { 280 PropertyUtils.read(b, "failure"); 281 unreachable(); 282 } 283 catch (ApplicationRuntimeException ex) 284 { 285 assertEquals( 286 "Unable to read property failure of object PropertyUtilsExceptionBean: java.lang.reflect.InvocationTargetException", 287 ex.getMessage()); 288 assertEquals(b, ex.getComponent()); 289 } 290 } 291 292 public void testWriteFailure() 293 { 294 ExceptionBean b = new ExceptionBean(); 295 296 try 297 { 298 PropertyUtils.write(b, "failure", Boolean.FALSE); 299 unreachable(); 300 } 301 catch (ApplicationRuntimeException ex) 302 { 303 assertEquals( 304 "Unable to update property failure of object PropertyUtilsExceptionBean: java.lang.reflect.InvocationTargetException", 305 ex.getMessage()); 306 assertEquals(b, ex.getComponent()); 307 } 308 } 309 310 public void testIntrospectFailure() 311 { 312 UglyBean b = new UglyBean(); 313 314 try 315 { 316 PropertyUtils.read(b, "google"); 317 unreachable(); 318 } 319 catch (ApplicationRuntimeException ex) 320 { 321 assertEquals( 322 "Unable to introspect properties of class " 323 + "org.apache.hivemind.util.TestPropertyUtils$UglyBean: java.lang.NullPointerException", 324 ex.getMessage()); 325 assertEquals(b, ex.getComponent()); 326 } 327 } 328 329 public void testNull() 330 { 331 try 332 { 333 PropertyUtils.read(null, "fred"); 334 unreachable(); 335 336 } 337 catch (ApplicationRuntimeException ex) 338 { 339 assertEquals("Attempt to read or update properties of null.", ex.getMessage()); 340 } 341 } 342 343 public void testGetPropertyType() 344 { 345 Bean b = new Bean(); 346 347 assertEquals(int.class, PropertyUtils.getPropertyType(b, "value")); 348 } 349 350 public void testIsReadable() 351 { 352 Bean b = new Bean(); 353 354 assertEquals(true, PropertyUtils.isReadable(b, "value")); 355 assertEquals(false, PropertyUtils.isReadable(b, "noSuchProperty")); 356 assertEquals(true, PropertyUtils.isReadable(b, "class")); 357 assertEquals(false, PropertyUtils.isReadable(b, "writeOnly")); 358 } 359 360 public void testIsWriteable() 361 { 362 Bean b = new Bean(); 363 364 assertEquals(true, PropertyUtils.isWritable(b, "value")); 365 assertEquals(true, PropertyUtils.isWritable(b, "writeOnly")); 366 assertEquals(false, PropertyUtils.isWritable(b, "doesNotExist")); 367 assertEquals(false, PropertyUtils.isWritable(b, "class")); 368 } 369 370 public void testGetReadable() 371 { 372 Bean b = new Bean(); 373 374 List actual = PropertyUtils.getReadableProperties(b); 375 376 Collections.sort(actual); 377 378 assertListsEqual(new String [] 379 { "class", "value" }, actual); 380 } 381 382 public void testGetWriteable() 383 { 384 Bean b = new Bean(); 385 386 List actual = PropertyUtils.getWriteableProperties(b); 387 388 Collections.sort(actual); 389 390 assertListsEqual(new String [] 391 { "value", "writeOnly" }, actual); 392 } 393 394 public void testGetPropertyAdaptor() 395 { 396 Bean b = new Bean(); 397 398 PropertyAdaptor a = PropertyUtils.getPropertyAdaptor(b, "writeOnly"); 399 400 assertEquals("setWriteOnly", a.getWriteMethodName()); 401 assertNull(a.getReadMethodName()); 402 403 a = PropertyUtils.getPropertyAdaptor(b, "class"); 404 405 assertEquals("getClass", a.getReadMethodName()); 406 assertNull(a.getWriteMethodName()); 407 } 408 409 410 public void testSmartWrite() 411 { 412 413 Bean b = new Bean(); 414 415 PropertyUtils.smartWrite(b, "value", "2170"); 416 417 assertEquals(2170, b.getValue()); 418 } 419 420 421 public void testSmartWriteWrapper() 422 { 423 MultiPropertyBean b = new MultiPropertyBean(); 424 425 PropertyUtils.smartWrite(b, "integer", "42"); 426 427 assertEquals(new Integer (42), b.getInteger()); 428 } 429 430 431 public void testSmartWriteFailure() 432 { 433 434 Bean b = new Bean(); 435 436 try 437 { 438 PropertyUtils.smartWrite(b, "value", "fred"); 439 unreachable(); 440 } 441 catch (ApplicationRuntimeException ex) 442 { 443 assertExceptionSubstring( 444 ex, 445 "Unable to convert 'fred' to type int (for property value of object PropertyUtilsTestBean)"); 446 } 447 } 448 449 public void testSmartWriteNoPropertyEditor() 450 { 451 StreamHolder sh = new StreamHolder(); 452 453 try 454 { 455 PropertyUtils.smartWrite(sh, "stream", "good luck"); 456 unreachable(); 457 } 458 catch (ApplicationRuntimeException ex) 459 { 460 assertEquals( 461 "No property editor exists for property stream of class org.apache.hivemind.util.TestPropertyUtils$StreamHolder.", 462 ex.getMessage()); 463 } 464 } 465 466 467 public void testConfigureProperties() 468 { 469 Bean b = new Bean(); 470 471 PropertyUtils.configureProperties(b, "value=8192"); 472 473 assertEquals(8192, b.getValue()); 474 } 475 476 477 public void testConfigureBooleanProperty() 478 { 479 BooleanHolder bh = new BooleanHolder(); 480 assertEquals(false, bh.isFlag()); 481 482 PropertyUtils.configureProperties(bh, "flag"); 483 484 assertEquals(true, bh.isFlag()); 485 486 PropertyUtils.configureProperties(bh, "!flag"); 487 488 assertEquals(false, bh.isFlag()); 489 } 490 491 492 493 public void testConfigureMultipleProperties() 494 { 495 MultiPropertyBean b = new MultiPropertyBean(); 496 497 PropertyUtils.configureProperties(b, "integer=42,long=-937, string=HiveMind,flag"); 499 500 assertEquals(new Integer (42), b.getInteger()); 501 assertEquals(-937, b.getLong()); 502 assertEquals("HiveMind", b.getString()); 503 assertEquals(true, b.isFlag()); 504 } 505 506 } | Popular Tags |