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.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
36
37 public class DefaultIntrospectionContextTest {
38
39
40 private static final String PROP = "foo";
41
42
43
44
45
46
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
57 private DefaultIntrospectionContext context;
58
59 @BeforeEach
60 protected void setUp() throws Exception {
61 context = new DefaultIntrospectionContext(getClass());
62 }
63
64
65
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
77
78 @Test
79 public void testAddPropertyDescriptorNull() {
80 assertThrows(NullPointerException.class, () -> context.addPropertyDescriptor(null));
81 }
82
83
84
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
114
115 @Test
116 public void testAddPropertyDescriptorsNull() {
117 assertThrows(NullPointerException.class, () -> context.addPropertyDescriptors(null));
118 }
119
120
121
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
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
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
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
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 }