KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > LazyDynaClassTestCase


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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 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 JavaDoc testProperty = "myProperty";
31
32     // ---------------------------------------------------------- Constructors
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 JavaDoc name) {
40         super(name);
41     }
42
43     // -------------------------------------------------- Overall Test Methods
44

45     /**
46      * Run this Test
47      */

48     public static void main(String JavaDoc[] 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 JavaDoc {
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     // ------------------------------------------------ Individual Test Methods
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 JavaDoc.class, dynaProperty.getType());
83     }
84
85     /**
86      * Test add(name, type) method
87      */

88     public void testAddProperty2() {
89         dynaClass.add(testProperty, String JavaDoc.class);
90         DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
91         assertEquals("name is correct", testProperty, dynaProperty.getName());
92         assertEquals("type is correct", String JavaDoc.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 JavaDoc.class, true, true);
101             fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
102         } catch (UnsupportedOperationException JavaDoc expected) {
103             // expected result
104
}
105     }
106
107     /**
108      * Test add(name) method with 'null' name
109      */

110     public void testAddPropertyNullName1() {
111         try {
112             dynaClass.add((String JavaDoc)null);
113             fail("null property name not prevented");
114         } catch (IllegalArgumentException JavaDoc expected) {
115             // expected result
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 JavaDoc.class);
125             fail("null property name not prevented");
126         } catch (IllegalArgumentException JavaDoc expected) {
127             // expected result
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 JavaDoc.class, true, true);
137             fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
138         } catch (UnsupportedOperationException JavaDoc expected) {
139             // expected result
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 JavaDoc expected) {
153             // expected result
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 JavaDoc.class);
165             fail("add(name, type) did not throw IllegalStateException");
166         } catch (IllegalStateException JavaDoc expected) {
167             // expected result
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 JavaDoc.class, true, true);
179             fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
180         } catch (UnsupportedOperationException JavaDoc t) {
181             // expected result
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 JavaDoc.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 JavaDoc expected) {
228             // expected result
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 JavaDoc expected) {
244             // expected result
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 }
Popular Tags