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