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.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.beans.IntrospectionException;
24  import java.beans.PropertyDescriptor;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashSet;
28  import java.util.Set;
29  
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Test class for {@code SuppressPropertiesBeanIntrospector}.
34   */
35  public class SuppressPropertiesBeanIntrospectorTest {
36  
37      /**
38       * A test implementation of IntrospectionContext which collects the properties which have been removed.
39       */
40      private static class IntrospectionContextTestImpl implements IntrospectionContext {
41          /** Stores the names of properties which have been removed. */
42          private final Set<String> removedProperties = new HashSet<>();
43  
44          @Override
45          public void addPropertyDescriptor(final PropertyDescriptor desc) {
46              throw new UnsupportedOperationException("Unexpected method call!");
47          }
48  
49          @Override
50          public void addPropertyDescriptors(final PropertyDescriptor[] descriptors) {
51              throw new UnsupportedOperationException("Unexpected method call!");
52          }
53  
54          @Override
55          public PropertyDescriptor getPropertyDescriptor(final String name) {
56              throw new UnsupportedOperationException("Unexpected method call!");
57          }
58  
59          /**
60           * Returns the names of properties which have been removed.
61           *
62           * @return the set with removed properties
63           */
64          public Set<String> getRemovedProperties() {
65              return removedProperties;
66          }
67  
68          @Override
69          public Class<?> getTargetClass() {
70              throw new UnsupportedOperationException("Unexpected method call!");
71          }
72  
73          @Override
74          public boolean hasProperty(final String name) {
75              throw new UnsupportedOperationException("Unexpected method call!");
76          }
77  
78          @Override
79          public Set<String> propertyNames() {
80              throw new UnsupportedOperationException("Unexpected method call!");
81          }
82  
83          @Override
84          public void removePropertyDescriptor(final String name) {
85              removedProperties.add(name);
86          }
87      }
88  
89      /**
90       * Tests that the set with properties to be removed cannot be modified.
91       */
92      @Test
93      public void testGetSuppressedPropertiesModify() {
94          final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(Arrays.asList("p1", "p2"));
95          final Set<String> properties = introspector.getSuppressedProperties();
96          assertThrows(UnsupportedOperationException.class, () -> properties.add("anotherProperty"));
97      }
98  
99      /**
100      * Tries to create an instance without properties.
101      */
102     @Test
103     public void testInitNoPropertyNames() {
104         assertThrows(NullPointerException.class, () -> new SuppressPropertiesBeanIntrospector(null));
105     }
106 
107     /**
108      * Tests that a defensive copy is created from the collection with properties to be removed.
109      */
110     @Test
111     public void testPropertyNamesDefensiveCopy() throws IntrospectionException {
112         final Collection<String> properties = new HashSet<>();
113         properties.add("prop1");
114         final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(properties);
115         properties.add("prop2");
116         final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
117 
118         introspector.introspect(context);
119         assertEquals(1, context.getRemovedProperties().size(), "Wrong number of removed properties");
120         assertTrue(context.getRemovedProperties().contains("prop1"), "Wrong removed property");
121     }
122 
123     /**
124      * Tests whether the expected properties have been removed during introspection.
125      */
126     @Test
127     public void testRemovePropertiesDuringIntrospection() throws IntrospectionException {
128         final String[] properties = { "test", "other", "oneMore" };
129         final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(Arrays.asList(properties));
130         final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
131 
132         introspector.introspect(context);
133         assertEquals(properties.length, context.getRemovedProperties().size(), "Wrong number of removed properties");
134         for (final String property : properties) {
135             assertTrue(context.getRemovedProperties().contains(property), "Property not removed: " + property);
136         }
137     }
138 }