1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
18
19 import junit.framework.TestCase;
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23
24
25
26
27
28 public class LazyDynaClassTestCase extends TestCase {
29
30 protected LazyDynaClass dynaClass = null;
31 protected String testProperty = "myProperty";
32
33
34
35
36
37
38
39
40 public LazyDynaClassTestCase(final String name) {
41 super(name);
42 }
43
44
45
46
47
48
49 public static void main(final String[] args) {
50 junit.textui.TestRunner.run(suite());
51 }
52
53
54
55
56 @Override
57 public void setUp() throws Exception {
58 dynaClass = new LazyDynaClass();
59 }
60
61
62
63
64 public static Test suite() {
65 return (new TestSuite(LazyDynaClassTestCase.class));
66 }
67
68
69
70
71 @Override
72 public void tearDown() {
73 dynaClass = null;
74 }
75
76
77
78
79
80
81 public void testAddProperty1() {
82 dynaClass.add(testProperty);
83 final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
84 assertEquals("name is correct", testProperty, dynaProperty.getName());
85 assertEquals("type is correct", Object.class, dynaProperty.getType());
86 }
87
88
89
90
91 public void testAddProperty2() {
92 dynaClass.add(testProperty, String.class);
93 final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
94 assertEquals("name is correct", testProperty, dynaProperty.getName());
95 assertEquals("type is correct", String.class, dynaProperty.getType());
96 }
97
98
99
100
101 public void testAddProperty3() {
102 try {
103 dynaClass.add(testProperty, String.class, true, true);
104 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
105 } catch (final UnsupportedOperationException expected) {
106
107 }
108 }
109
110
111
112
113 public void testAddPropertyNullName1() {
114 try {
115 dynaClass.add((String)null);
116 fail("null property name not prevented");
117 } catch (final IllegalArgumentException expected) {
118
119 }
120 }
121
122
123
124
125 public void testAddPropertyNullName2() {
126 try {
127 dynaClass.add(null, String.class);
128 fail("null property name not prevented");
129 } catch (final IllegalArgumentException expected) {
130
131 }
132 }
133
134
135
136
137 public void testAddPropertyNullName3() {
138 try {
139 dynaClass.add(null, String.class, true, true);
140 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
141 } catch (final UnsupportedOperationException expected) {
142
143 }
144 }
145
146
147
148
149 public void testAddPropertyRestricted1() {
150 dynaClass.setRestricted(true);
151 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted());
152 try {
153 dynaClass.add(testProperty);
154 fail("add(name) did not throw IllegalStateException");
155 } catch (final IllegalStateException expected) {
156
157 }
158 }
159
160
161
162
163 public void testAddPropertyRestricted2() {
164 dynaClass.setRestricted(true);
165 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted());
166 try {
167 dynaClass.add(testProperty, String.class);
168 fail("add(name, type) did not throw IllegalStateException");
169 } catch (final IllegalStateException expected) {
170
171 }
172 }
173
174
175
176
177 public void testAddPropertyRestricted3() {
178 dynaClass.setRestricted(true);
179 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted());
180 try {
181 dynaClass.add(testProperty, String.class, true, true);
182 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
183 } catch (final UnsupportedOperationException t) {
184
185 }
186 }
187
188
189
190
191 public void testGetPropertyDoesntExist1() {
192 dynaClass.setReturnNull(false);
193 assertFalse("returnNull is 'false'", dynaClass.isReturnNull());
194 final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
195 assertEquals("name is correct", testProperty, dynaProperty.getName());
196 assertEquals("type is correct", Object.class, dynaProperty.getType());
197 assertFalse("property doesnt exist", dynaClass.isDynaProperty(testProperty));
198 }
199
200
201
202
203
204 public void testGetPropertyDoesntExist2() {
205 dynaClass.setReturnNull(true);
206 assertTrue("returnNull is 'true'", dynaClass.isReturnNull());
207 assertNull("property is null", dynaClass.getDynaProperty(testProperty));
208 }
209
210
211
212
213 public void testRemoveProperty() {
214 dynaClass.setReturnNull(true);
215 dynaClass.add(testProperty);
216 assertTrue("Property exists", dynaClass.isDynaProperty(testProperty));
217 assertNotNull("property is Not null", dynaClass.getDynaProperty(testProperty));
218 dynaClass.remove(testProperty);
219 assertFalse("Property doesn't exist", dynaClass.isDynaProperty(testProperty));
220 assertNull("property is null", dynaClass.getDynaProperty(testProperty));
221 }
222
223
224
225
226 public void testRemovePropertyNullName() {
227 try {
228 dynaClass.remove(null);
229 fail("remove(null) did not throw IllegalArgumentException");
230 } catch (final IllegalArgumentException expected) {
231
232 }
233 }
234
235
236
237
238 public void testRemovePropertyRestricted() {
239 dynaClass.add(testProperty);
240 assertTrue("Property exists", dynaClass.isDynaProperty(testProperty));
241 dynaClass.setRestricted(true);
242 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted());
243 try {
244 dynaClass.remove(testProperty);
245 fail("remove property when MutableDynaClassis restricted did not throw IllegalStateException");
246 } catch (final IllegalStateException expected) {
247
248 }
249 }
250
251
252
253
254 public void testRemovePropertyDoesntExist() {
255 assertFalse("property doesn't exist", dynaClass.isDynaProperty(testProperty));
256 dynaClass.remove(testProperty);
257 assertFalse("property still doesn't exist", dynaClass.isDynaProperty(testProperty));
258 }
259 }