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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.beans.IntrospectionException;
27  import java.beans.PropertyDescriptor;
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test class for {@code IntrospectionContext}.
36   */
37  public class DefaultIntrospectionContextTest {
38  
39      /** Constant for the name of a property. */
40      private static final String PROP = "foo";
41  
42      /**
43       * Creates a property descriptor object for a property with the given name.
44       *
45       * @param propName the property name
46       * @return the descriptor for this property
47       */
48      private static PropertyDescriptor createDescriptor(final String propName) {
49          try {
50              return new PropertyDescriptor(propName, DefaultIntrospectionContextTest.class, null, null);
51          } catch (final IntrospectionException e) {
52              throw new IllegalStateException("Unexpected exception: " + e);
53          }
54      }
55  
56      /** The context to be tested. */
57      private DefaultIntrospectionContext context;
58  
59      @BeforeEach
60      protected void setUp() throws Exception {
61          context = new DefaultIntrospectionContext(getClass());
62      }
63  
64      /**
65       * Tests whether a property descriptor can be added.
66       */
67      @Test
68      public void testAddPropertyDescriptor() {
69          final PropertyDescriptor desc = createDescriptor(PROP);
70          context.addPropertyDescriptor(desc);
71          assertTrue(context.hasProperty(PROP), "Property not found");
72          assertSame(desc, context.getPropertyDescriptor(PROP), "Wrong descriptor");
73      }
74  
75      /**
76       * Tries to add a null descriptor.
77       */
78      @Test
79      public void testAddPropertyDescriptorNull() {
80          assertThrows(NullPointerException.class, () -> context.addPropertyDescriptor(null));
81      }
82  
83      /**
84       * Tests whether multiple descriptors can be added.
85       */
86      @Test
87      public void testAddPropertyDescriptors() {
88          final int count = 4;
89          final PropertyDescriptor[] descs = new PropertyDescriptor[count];
90          final Set<PropertyDescriptor> descSet = new HashSet<>();
91          for (int i = 0; i < count; i++) {
92              descs[i] = createDescriptor(PROP + i);
93              descSet.add(descs[i]);
94          }
95          context.addPropertyDescriptors(descs);
96          final PropertyDescriptor d = createDescriptor(PROP);
97          context.addPropertyDescriptor(d);
98          descSet.add(d);
99          final Set<String> names = context.propertyNames();
100         assertEquals(count + 1, names.size(), "Wrong number of property names");
101         assertTrue(names.contains(PROP), "Property not found: " + PROP);
102         for (int i = 0; i < count; i++) {
103             assertTrue(names.contains(PROP + i), "Property not found: " + PROP + i);
104         }
105         final PropertyDescriptor[] addedDescs = context.getPropertyDescriptors();
106         assertEquals(count + 1, addedDescs.length, "Wrong number of added descriptors");
107         for (final PropertyDescriptor pd : addedDescs) {
108             assertTrue(descSet.remove(pd), "Unexpected descriptor: " + pd);
109         }
110     }
111 
112     /**
113      * Tries to add a null array with property descriptors.
114      */
115     @Test
116     public void testAddPropertyDescriptorsNull() {
117         assertThrows(NullPointerException.class, () -> context.addPropertyDescriptors(null));
118     }
119 
120     /**
121      * Tests getPropertyDescriptor() if the property name is unknown.
122      */
123     @Test
124     public void testGetPropertyDescriptorUnknown() {
125         assertNull(context.getPropertyDescriptor(PROP), "Got a property (1)");
126         context.addPropertyDescriptor(createDescriptor(PROP));
127         assertNull(context.getPropertyDescriptor("other"), "Got a property (2)");
128     }
129 
130     /**
131      * Tests hasProperty() if the expected result is false.
132      */
133     @Test
134     public void testHasPropertyFalse() {
135         assertFalse(context.hasProperty(PROP), "Wrong result (1)");
136         context.addPropertyDescriptor(createDescriptor(PROP));
137         assertFalse(context.hasProperty("other"), "Wrong result (2)");
138     }
139 
140     /**
141      * Tests a newly created instance.
142      */
143     @Test
144     public void testInit() {
145         assertEquals(getClass(), context.getTargetClass(), "Wrong current class");
146         assertTrue(context.propertyNames().isEmpty(), "Got property names");
147     }
148 
149     /**
150      * Tests that the set with property names cannot be changed.
151      */
152     @Test
153     public void testPropertyNamesModify() {
154         final Set<String> names = context.propertyNames();
155         assertThrows(UnsupportedOperationException.class, () -> names.add(PROP));
156     }
157 
158     /**
159      * Tests whether a descriptor can be removed.
160      */
161     @Test
162     public void testRemovePropertyDescriptor() {
163         context.addPropertyDescriptor(createDescriptor(PROP));
164         context.removePropertyDescriptor(PROP);
165         assertTrue(context.propertyNames().isEmpty(), "Got property names");
166         assertEquals(0, context.getPropertyDescriptors().length, "Got descriptors");
167     }
168 }