View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * <p>Test Case for the <code>LazyDynaClass</code> implementation class.</p>
25   *
26   * @version $Id$
27   */
28  public class LazyDynaClassTestCase extends TestCase {
29  
30      protected LazyDynaClass dynaClass = null;
31      protected String testProperty     = "myProperty";
32  
33      // ---------------------------------------------------------- Constructors
34  
35      /**
36       * Construct a new instance of this test case.
37       *
38       * @param name Name of the test case
39       */
40      public LazyDynaClassTestCase(final String name) {
41          super(name);
42      }
43  
44      // -------------------------------------------------- Overall Test Methods
45  
46      /**
47       * Run this Test
48       */
49      public static void main(final String[] args) {
50        junit.textui.TestRunner.run(suite());
51      }
52  
53      /**
54       * Set up instance variables required by this test case.
55       */
56      @Override
57      public void setUp() throws Exception {
58          dynaClass = new LazyDynaClass();
59      }
60  
61      /**
62       * Return the tests included in this test suite.
63       */
64      public static Test suite() {
65          return (new TestSuite(LazyDynaClassTestCase.class));
66      }
67  
68      /**
69       * Tear down instance variables required by this test case.
70       */
71      @Override
72      public void tearDown() {
73          dynaClass = null;
74      }
75  
76      // ------------------------------------------------ Individual Test Methods
77  
78      /**
79       * Test add(name) method
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       * Test add(name, type) method
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       * Test add(name, type, readable, writable) method
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             // expected result
107         }
108     }
109 
110     /**
111      * Test add(name) method with 'null' name
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             // expected result
119         }
120     }
121 
122     /**
123      * Test add(name, type) method with 'null' name
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             // expected result
131         }
132     }
133 
134     /**
135      * Test add(name, type, readable, writable) method with 'null' name
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             // expected result
143         }
144     }
145 
146     /**
147      * Test add(name) method when restricted is set to 'true'
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             // expected result
157         }
158     }
159 
160     /**
161      * Test add(name, type) method when restricted is set to 'true'
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             // expected result
171         }
172     }
173 
174     /**
175      * Test add(name, type, readable, writable) method when restricted is set to 'true'
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             // expected result
185         }
186     }
187 
188     /**
189      * Test retrieving a property which doesn't exist (returnNull is 'false')
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      * Test retrieving a property which doesn't exist (returnNull is 'true')
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      * Test removing a property
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      * Test removing a property, name is null
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             // expected result
232         }
233     }
234 
235     /**
236      * Test removing a property, DynaClass is restricted
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             // expected result
248         }
249     }
250 
251     /**
252      * Test removing a property which doesn't exist
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 }