KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > BeanUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans;
18
19 import java.beans.Introspector JavaDoc;
20 import java.beans.PropertyDescriptor JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.springframework.beans.propertyeditors.CustomDateEditor;
27
28 /**
29  * @author Juergen Hoeller
30  * @since 19.05.2003
31  */

32 public class BeanUtilsTests extends TestCase {
33
34     public void testInstantiateClass() {
35         // give proper class
36
BeanUtils.instantiateClass(ArrayList JavaDoc.class);
37
38         try {
39             // give interface
40
BeanUtils.instantiateClass(List JavaDoc.class);
41             fail("Should have thrown FatalBeanException");
42         }
43         catch (FatalBeanException ex) {
44             // expected
45
}
46
47         try {
48             // give class without default constructor
49
BeanUtils.instantiateClass(CustomDateEditor.class);
50             fail("Should have thrown FatalBeanException");
51         }
52         catch (FatalBeanException ex) {
53             // expected
54
}
55     }
56
57     public void testCopyProperties() throws Exception JavaDoc {
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             // expected
73
}
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 JavaDoc[]{"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 JavaDoc[] {"specialProperty"});
101         assertEquals(target.getName(), "name");
102     }
103
104     public void testGetPropertyDescriptors() throws Exception JavaDoc {
105         PropertyDescriptor JavaDoc[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors();
106         PropertyDescriptor JavaDoc[] 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 JavaDoc name;
115
116         private int specialProperty;
117
118         public void setName(String JavaDoc name) {
119             this.name = name;
120         }
121
122         public String JavaDoc 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