001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils;
018:
019: import junit.framework.TestCase;
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: /**
024: * <p>Test Case for the <code>LazyDynaClass</code> implementation class.</p>
025: *
026: * @author Niall Pemberton
027: */
028: public class LazyDynaClassTestCase extends TestCase {
029:
030: protected LazyDynaClass dynaClass = null;
031: protected String testProperty = "myProperty";
032:
033: // ---------------------------------------------------------- Constructors
034:
035: /**
036: * Construct a new instance of this test case.
037: *
038: * @param name Name of the test case
039: */
040: public LazyDynaClassTestCase(String name) {
041: super (name);
042: }
043:
044: // -------------------------------------------------- Overall Test Methods
045:
046: /**
047: * Run this Test
048: */
049: public static void main(String[] args) {
050: junit.textui.TestRunner.run(suite());
051: }
052:
053: /**
054: * Set up instance variables required by this test case.
055: */
056: public void setUp() throws Exception {
057: dynaClass = new LazyDynaClass();
058: }
059:
060: /**
061: * Return the tests included in this test suite.
062: */
063: public static Test suite() {
064: return (new TestSuite(LazyDynaClassTestCase.class));
065: }
066:
067: /**
068: * Tear down instance variables required by this test case.
069: */
070: public void tearDown() {
071: dynaClass = null;
072: }
073:
074: // ------------------------------------------------ Individual Test Methods
075:
076: /**
077: * Test add(name) method
078: */
079: public void testAddProperty1() {
080: dynaClass.add(testProperty);
081: DynaProperty dynaProperty = dynaClass
082: .getDynaProperty(testProperty);
083: assertEquals("name is correct", testProperty, dynaProperty
084: .getName());
085: assertEquals("type is correct", Object.class, dynaProperty
086: .getType());
087: }
088:
089: /**
090: * Test add(name, type) method
091: */
092: public void testAddProperty2() {
093: dynaClass.add(testProperty, String.class);
094: DynaProperty dynaProperty = dynaClass
095: .getDynaProperty(testProperty);
096: assertEquals("name is correct", testProperty, dynaProperty
097: .getName());
098: assertEquals("type is correct", String.class, dynaProperty
099: .getType());
100: }
101:
102: /**
103: * Test add(name, type, readable, writable) method
104: */
105: public void testAddProperty3() {
106: try {
107: dynaClass.add(testProperty, String.class, true, true);
108: fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
109: } catch (UnsupportedOperationException expected) {
110: // expected result
111: }
112: }
113:
114: /**
115: * Test add(name) method with 'null' name
116: */
117: public void testAddPropertyNullName1() {
118: try {
119: dynaClass.add((String) null);
120: fail("null property name not prevented");
121: } catch (IllegalArgumentException expected) {
122: // expected result
123: }
124: }
125:
126: /**
127: * Test add(name, type) method with 'null' name
128: */
129: public void testAddPropertyNullName2() {
130: try {
131: dynaClass.add(null, String.class);
132: fail("null property name not prevented");
133: } catch (IllegalArgumentException expected) {
134: // expected result
135: }
136: }
137:
138: /**
139: * Test add(name, type, readable, writable) method with 'null' name
140: */
141: public void testAddPropertyNullName3() {
142: try {
143: dynaClass.add(null, String.class, true, true);
144: fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
145: } catch (UnsupportedOperationException expected) {
146: // expected result
147: }
148: }
149:
150: /**
151: * Test add(name) method when restricted is set to 'true'
152: */
153: public void testAddPropertyRestricted1() {
154: dynaClass.setRestricted(true);
155: assertTrue("MutableDynaClass is restricted", dynaClass
156: .isRestricted());
157: try {
158: dynaClass.add(testProperty);
159: fail("add(name) did not throw IllegalStateException");
160: } catch (IllegalStateException expected) {
161: // expected result
162: }
163: }
164:
165: /**
166: * Test add(name, type) method when restricted is set to 'true'
167: */
168: public void testAddPropertyRestricted2() {
169: dynaClass.setRestricted(true);
170: assertTrue("MutableDynaClass is restricted", dynaClass
171: .isRestricted());
172: try {
173: dynaClass.add(testProperty, String.class);
174: fail("add(name, type) did not throw IllegalStateException");
175: } catch (IllegalStateException expected) {
176: // expected result
177: }
178: }
179:
180: /**
181: * Test add(name, type, readable, writable) method when restricted is set to 'true'
182: */
183: public void testAddPropertyRestricted3() {
184: dynaClass.setRestricted(true);
185: assertTrue("MutableDynaClass is restricted", dynaClass
186: .isRestricted());
187: try {
188: dynaClass.add(testProperty, String.class, true, true);
189: fail("add(name, type, readable, writable) did not throw UnsupportedOperationException");
190: } catch (UnsupportedOperationException t) {
191: // expected result
192: }
193: }
194:
195: /**
196: * Test retrieving a property which doesn't exist (returnNull is 'false')
197: */
198: public void testGetPropertyDoesntExist1() {
199: dynaClass.setReturnNull(false);
200: assertFalse("returnNull is 'false'", dynaClass.isReturnNull());
201: DynaProperty dynaProperty = dynaClass
202: .getDynaProperty(testProperty);
203: assertEquals("name is correct", testProperty, dynaProperty
204: .getName());
205: assertEquals("type is correct", Object.class, dynaProperty
206: .getType());
207: assertFalse("property doesnt exist", dynaClass
208: .isDynaProperty(testProperty));
209: }
210:
211: /**
212: * Test retrieving a property which doesn't exist (returnNull is 'true')
213: */
214: public void testGetPropertyDoesntExist2() {
215: dynaClass.setReturnNull(true);
216: assertTrue("returnNull is 'true'", dynaClass.isReturnNull());
217: assertNull("property is null", dynaClass
218: .getDynaProperty(testProperty));
219: }
220:
221: /**
222: * Test removing a property
223: */
224: public void testRemoveProperty() {
225: dynaClass.setReturnNull(true);
226: dynaClass.add(testProperty);
227: assertTrue("Property exists", dynaClass
228: .isDynaProperty(testProperty));
229: assertNotNull("property is Not null", dynaClass
230: .getDynaProperty(testProperty));
231: dynaClass.remove(testProperty);
232: assertFalse("Property doesn't exist", dynaClass
233: .isDynaProperty(testProperty));
234: assertNull("property is null", dynaClass
235: .getDynaProperty(testProperty));
236: }
237:
238: /**
239: * Test removing a property, name is null
240: */
241: public void testRemovePropertyNullName() {
242: try {
243: dynaClass.remove(null);
244: fail("remove(null) did not throw IllegalArgumentException");
245: } catch (IllegalArgumentException expected) {
246: // expected result
247: }
248: }
249:
250: /**
251: * Test removing a property, DynaClass is restricted
252: */
253: public void testRemovePropertyRestricted() {
254: dynaClass.add(testProperty);
255: assertTrue("Property exists", dynaClass
256: .isDynaProperty(testProperty));
257: dynaClass.setRestricted(true);
258: assertTrue("MutableDynaClass is restricted", dynaClass
259: .isRestricted());
260: try {
261: dynaClass.remove(testProperty);
262: fail("remove property when MutableDynaClassis restricted did not throw IllegalStateException");
263: } catch (IllegalStateException expected) {
264: // expected result
265: }
266: }
267:
268: /**
269: * Test removing a property which doesn't exist
270: */
271: public void testRemovePropertyDoesntExist() {
272: assertFalse("property doesn't exist", dynaClass
273: .isDynaProperty(testProperty));
274: dynaClass.remove(testProperty);
275: assertFalse("property still doesn't exist", dynaClass
276: .isDynaProperty(testProperty));
277: }
278: }
|