1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35 public class SuppressPropertiesBeanIntrospectorTest {
36
37
38
39
40 private static class IntrospectionContextTestImpl implements IntrospectionContext {
41
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
61
62
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
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
101
102 @Test
103 public void testInitNoPropertyNames() {
104 assertThrows(NullPointerException.class, () -> new SuppressPropertiesBeanIntrospector(null));
105 }
106
107
108
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
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 }