1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import static org.apache.commons.beanutils2.BeanUtils.on;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.Serializable;
26 import java.util.Map;
27
28 import org.apache.commons.beanutils2.testbeans.TestBean;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class DescribeTestCase
34 {
35
36 private TestBean testBean;
37
38 @Before
39 public void setUp()
40 {
41 testBean = new TestBean();
42 }
43
44 @After
45 public void tearDown()
46 {
47 testBean = null;
48 }
49
50
51
52
53 private final String describes[] =
54 { "booleanProperty",
55 "booleanSecond",
56 "byteProperty",
57 "doubleProperty",
58 "dupProperty",
59 "floatProperty",
60 "intArray",
61
62 "longProperty",
63 "listIndexed",
64 "longProperty",
65
66
67 "nested",
68 "nullProperty",
69 "readOnlyProperty",
70 "shortProperty",
71 "stringArray",
72
73 "stringProperty"
74 };
75
76 @Test
77 public void describe()
78 throws Exception
79 {
80 Map<String, Object> properties = on( testBean ).describe();
81 assertNotNull( properties );
82
83
84 for ( int i = 0; i < describes.length; i++ )
85 {
86 assertTrue( "Property '" + describes[i] + "' is not present", properties.containsKey( describes[i] ) );
87 }
88
89 assertTrue( properties.containsKey( "class" ) );
90
91 assertTrue( "Property 'writeOnlyProperty' is not present", !properties.containsKey( "writeOnlyProperty" ) );
92
93
94 assertEquals( "Value of 'booleanProperty'", true, properties.get( "booleanProperty" ) );
95 assertEquals( "Value of 'byteProperty'", (byte) 121, properties.get( "byteProperty" ) );
96 assertEquals( "Value of 'doubleProperty'", 321.0d, properties.get( "doubleProperty" ) );
97 assertEquals( "Value of 'floatProperty'", 123.0f, properties.get( "floatProperty" ) );
98 assertEquals( "Value of 'intProperty'", 123, properties.get( "intProperty" ) );
99 assertEquals( "Value of 'longProperty'", 321l, properties.get( "longProperty" ) );
100 assertEquals( "Value of 'shortProperty'", (short) 987, properties.get( "shortProperty" ) );
101 assertEquals( "Value of 'stringProperty'", "This is a string", properties.get( "stringProperty" ) );
102 }
103
104
105
106
107
108
109 @Test
110 public void describeSerializable()
111 throws Exception
112 {
113 Object bean = new Serializable()
114 {
115 private static final long serialVersionUID = 1L;
116
117 @SuppressWarnings( "unused" )
118 public String getX()
119 {
120 return "x-value";
121 }
122
123 @SuppressWarnings( "unused" )
124 public String getY()
125 {
126 return "y-value";
127 }
128 };
129 Map<String, Object> result = on( bean ).describe();
130
131 assertEquals( "Check Size", 3, result.size() );
132 assertTrue( "Class", result.containsKey( "class" ) );
133 }
134
135
136
137
138
139
140 @Test
141 public void describeInterface()
142 throws Exception
143 {
144 Object bean = new XY()
145 {
146 public String getX()
147 {
148 return "x-value";
149 }
150
151 public String getY()
152 {
153 return "y-value";
154 }
155 };
156 Map<String, Object> result = on( bean ).describe();
157 assertEquals( "Check Size", 3, result.size() );
158 assertTrue( "Class", result.containsKey( "class" ) );
159 assertTrue( "X Key", result.containsKey( "x" ) );
160 assertTrue( "Y Key", result.containsKey( "y" ) );
161 assertEquals( "X Value", "x-value", result.get( "x" ) );
162 assertEquals( "Y Value", "y-value", result.get( "y" ) );
163 }
164
165
166
167
168
169
170 @Test
171 public void describeBean()
172 throws Exception
173 {
174 Object bean = new FooBar();
175 Map<String, Object> result = on( bean ).describe();
176 assertEquals( "Check Size", 2, result.size() );
177 assertTrue( "Class", result.containsKey( "class" ) );
178 assertTrue( "publicFoo Key", result.containsKey( "publicFoo" ) );
179 assertEquals( "publicFoo Value", "PublicFoo Value", result.get( "publicFoo" ) );
180 }
181
182 public static interface XY
183 {
184 String getX();
185
186 String getY();
187 }
188
189 public static class FooBar
190 {
191 String getPackageFoo()
192 {
193 return "Package Value";
194 }
195
196 @SuppressWarnings( "unused" )
197 private String getPrivateFoo()
198 {
199 return "PrivateFoo Value";
200 }
201
202 protected String getProtectedFoo()
203 {
204 return "ProtectedFoo Value";
205 }
206
207 public String getPublicFoo()
208 {
209 return "PublicFoo Value";
210 }
211 }
212
213 }