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