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    *      https://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.beanutils2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * <p>
32   * Test Case for the {@code LazyDynaClass} implementation class.
33   * </p>
34   */
35  public class LazyDynaClassTest {
36  
37      protected LazyDynaClass dynaClass;
38  
39      protected String testProperty = "myProperty";
40  
41      /**
42       * Sets up instance variables required by this test case.
43       */
44      @BeforeEach
45      public void setUp() throws Exception {
46          dynaClass = new LazyDynaClass();
47      }
48  
49      /**
50       * Tear down instance variables required by this test case.
51       */
52      @AfterEach
53      public void tearDown() {
54          dynaClass = null;
55      }
56  
57      /**
58       * Test add(name) method
59       */
60      @Test
61      public void testAddProperty1() {
62          dynaClass.add(testProperty);
63          final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
64          assertEquals(testProperty, dynaProperty.getName(), "name is correct");
65          assertEquals(Object.class, dynaProperty.getType(), "type is correct");
66      }
67  
68      /**
69       * Test add(name, type) method
70       */
71      @Test
72      public void testAddProperty2() {
73          dynaClass.add(testProperty, String.class);
74          final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
75          assertEquals(testProperty, dynaProperty.getName(), "name is correct");
76          assertEquals(String.class, dynaProperty.getType(), "type is correct");
77      }
78  
79      /**
80       * Test add(name, type, readable, writable) method
81       */
82      @Test
83      public void testAddProperty3() {
84          assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(testProperty, String.class, true, true));
85      }
86  
87      /**
88       * Test add(name) method with 'null' name
89       */
90      @Test
91      public void testAddPropertyNullName1() {
92          assertThrows(NullPointerException.class, () -> dynaClass.add((String) null));
93      }
94  
95      /**
96       * Test add(name, type) method with 'null' name
97       */
98      @Test
99      public void testAddPropertyNullName2() {
100         assertThrows(NullPointerException.class, () -> dynaClass.add(null, String.class));
101     }
102 
103     /**
104      * Test add(name, type, readable, writable) method with 'null' name
105      */
106     @Test
107     public void testAddPropertyNullName3() {
108         assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(null, String.class, true, true));
109     }
110 
111     /**
112      * Test add(name) method when restricted is set to 'true'
113      */
114     @Test
115     public void testAddPropertyRestricted1() {
116         dynaClass.setRestricted(true);
117         assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted");
118         assertThrows(IllegalStateException.class, () -> dynaClass.add(testProperty));
119     }
120 
121     /**
122      * Test add(name, type) method when restricted is set to 'true'
123      */
124     @Test
125     public void testAddPropertyRestricted2() {
126         dynaClass.setRestricted(true);
127         assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted");
128         assertThrows(IllegalStateException.class, () -> dynaClass.add(testProperty, String.class));
129     }
130 
131     /**
132      * Test add(name, type, readable, writable) method when restricted is set to 'true'
133      */
134     @Test
135     public void testAddPropertyRestricted3() {
136         dynaClass.setRestricted(true);
137         assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted");
138         assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(testProperty, String.class, true, true));
139     }
140 
141     /**
142      * Test retrieving a property which doesn't exist (returnNull is 'false')
143      */
144     @Test
145     public void testGetPropertyDoesntExist1() {
146         dynaClass.setReturnNull(false);
147         assertFalse(dynaClass.isReturnNull(), "returnNull is 'false'");
148         final DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty);
149         assertEquals(testProperty, dynaProperty.getName(), "name is correct");
150         assertEquals(Object.class, dynaProperty.getType(), "type is correct");
151         assertFalse(dynaClass.isDynaProperty(testProperty), "property doesn't exist");
152     }
153 
154     /**
155      * Test retrieving a property which doesn't exist (returnNull is 'true')
156      */
157     @Test
158     public void testGetPropertyDoesntExist2() {
159         dynaClass.setReturnNull(true);
160         assertTrue(dynaClass.isReturnNull(), "returnNull is 'true'");
161         assertNull(dynaClass.getDynaProperty(testProperty), "property is null");
162     }
163 
164     /**
165      * Test removing a property
166      */
167     @Test
168     public void testRemoveProperty() {
169         dynaClass.setReturnNull(true);
170         dynaClass.add(testProperty);
171         assertTrue(dynaClass.isDynaProperty(testProperty), "Property exists");
172         assertNotNull(dynaClass.getDynaProperty(testProperty), "property is Not null");
173         dynaClass.remove(testProperty);
174         assertFalse(dynaClass.isDynaProperty(testProperty), "Property doesn't exist");
175         assertNull(dynaClass.getDynaProperty(testProperty), "property is null");
176     }
177 
178     /**
179      * Test removing a property which doesn't exist
180      */
181     @Test
182     public void testRemovePropertyDoesntExist() {
183         assertFalse(dynaClass.isDynaProperty(testProperty), "property doesn't exist");
184         dynaClass.remove(testProperty);
185         assertFalse(dynaClass.isDynaProperty(testProperty), "property still doesn't exist");
186     }
187 
188     /**
189      * Test removing a property, name is null
190      */
191     @Test
192     public void testRemovePropertyNullName() {
193         assertThrows(NullPointerException.class, () -> dynaClass.remove(null));
194     }
195 
196     /**
197      * Test removing a property, DynaClass is restricted
198      */
199     @Test
200     public void testRemovePropertyRestricted() {
201         dynaClass.add(testProperty);
202         assertTrue(dynaClass.isDynaProperty(testProperty), "Property exists");
203         dynaClass.setRestricted(true);
204         assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted");
205         assertThrows(IllegalStateException.class, () -> dynaClass.remove(testProperty));
206     }
207 }