1 16 17 package org.springframework.beans; 18 19 import java.beans.Introspector ; 20 import java.beans.PropertyDescriptor ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import junit.framework.TestCase; 25 26 import org.springframework.beans.propertyeditors.CustomDateEditor; 27 28 32 public class BeanUtilsTests extends TestCase { 33 34 public void testInstantiateClass() { 35 BeanUtils.instantiateClass(ArrayList .class); 37 38 try { 39 BeanUtils.instantiateClass(List .class); 41 fail("Should have thrown FatalBeanException"); 42 } 43 catch (FatalBeanException ex) { 44 } 46 47 try { 48 BeanUtils.instantiateClass(CustomDateEditor.class); 50 fail("Should have thrown FatalBeanException"); 51 } 52 catch (FatalBeanException ex) { 53 } 55 } 56 57 public void testCopyProperties() throws Exception { 58 TestBean tb = new TestBean(); 59 tb.setName("rod"); 60 tb.setAge(32); 61 tb.setTouchy("touchy"); 62 TestBean tb2 = new TestBean(); 63 assertTrue("Name empty", tb2.getName() == null); 64 assertTrue("Age empty", tb2.getAge() == 0); 65 assertTrue("Touchy empty", tb2.getTouchy() == null); 66 67 try { 68 BeanUtils.copyProperties(tb, ""); 69 fail("Should have thrown IllegalArgumentException"); 70 } 71 catch (InvalidPropertyException ex) { 72 } 74 75 BeanUtils.copyProperties(tb, tb2); 76 assertTrue("Name copied", tb2.getName().equals(tb.getName())); 77 assertTrue("Age copied", tb2.getAge() == tb.getAge()); 78 assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); 79 } 80 81 public void testCopyPropertiesWithIgnore() { 82 TestBean tb = new TestBean(); 83 assertTrue("Name empty", tb.getName() == null); 84 tb.setAge(32); 85 TestBean tb2 = new TestBean(); 86 tb2.setName("rod"); 87 assertTrue("Age empty", tb2.getAge() == 0); 88 assertTrue("Touchy empty", tb2.getTouchy() == null); 89 90 BeanUtils.copyProperties(tb, tb2, new String []{"spouse", "touchy", "age"}); 91 assertTrue("Name copied", tb2.getName() == null); 92 assertTrue("Age still empty", tb2.getAge() == 0); 93 assertTrue("Touchy still empty", tb2.getTouchy() == null); 94 } 95 96 public void testCopyPropertiesWithIgnoredNonExistingProperty() { 97 NameAndSpecialProperty source = new NameAndSpecialProperty(); 98 source.setName("name"); 99 TestBean target = new TestBean(); 100 BeanUtils.copyProperties(source, target, new String [] {"specialProperty"}); 101 assertEquals(target.getName(), "name"); 102 } 103 104 public void testGetPropertyDescriptors() throws Exception { 105 PropertyDescriptor [] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors(); 106 PropertyDescriptor [] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class); 107 assertNotNull("Descriptors should not be null", descriptors); 108 assertEquals("Invalid number of descriptors returned", actual.length, descriptors.length); 109 } 110 111 112 private static class NameAndSpecialProperty { 113 114 private String name; 115 116 private int specialProperty; 117 118 public void setName(String name) { 119 this.name = name; 120 } 121 122 public String getName() { 123 return this.name; 124 } 125 126 public void setSpecialProperty(int specialProperty) { 127 this.specialProperty = specialProperty; 128 } 129 130 public int getSpecialProperty() { 131 return specialProperty; 132 } 133 } 134 135 } 136 | Popular Tags |