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.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.beans.IntrospectionException;
25 import java.beans.PropertyDescriptor;
26 import java.net.URI;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 public class FluentPropertyBeanIntrospectorTest {
36 public static final class CapsBean {
37 private URI mURI;
38
39 public URI getURI() {
40 return mURI;
41 }
42
43 public void setURI(final URI theURI) {
44 mURI = theURI;
45 }
46 }
47
48
49
50
51
52
53
54 private static Map<String, PropertyDescriptor> createDescriptorMap(final PropertyDescriptor[] descs) {
55 final Map<String, PropertyDescriptor> map = new HashMap<>();
56 for (final PropertyDescriptor pd : descs) {
57 map.put(pd.getName(), pd);
58 }
59 return map;
60 }
61
62
63
64
65
66
67
68
69 private static PropertyDescriptor fetchDescriptor(final Map<String, PropertyDescriptor> props, final String name) {
70 assertTrue(props.containsKey(name), "Property not found: " + name);
71 return props.get(name);
72 }
73
74
75
76
77 @Test
78 public void testInitNoPrefix() {
79 assertThrows(NullPointerException.class, () -> new FluentPropertyBeanIntrospector(null));
80 }
81
82
83
84
85 @Test
86 public void testIntrospection() throws IntrospectionException {
87 final PropertyUtilsBean pu = new PropertyUtilsBean();
88 final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
89 pu.addBeanIntrospector(introspector);
90 final Map<String, PropertyDescriptor> props = createDescriptorMap(pu.getPropertyDescriptors(FluentIntrospectionTestBean.class));
91 PropertyDescriptor pd = fetchDescriptor(props, "name");
92 assertNotNull(pd.getReadMethod(), "No read method for name");
93 assertNotNull(pd.getWriteMethod(), "No write method for name");
94 fetchDescriptor(props, "stringProperty");
95 pd = fetchDescriptor(props, "fluentProperty");
96 assertNull(pd.getReadMethod(), "Read method for fluentProperty");
97 assertNotNull(pd.getWriteMethod(), "No write method for fluentProperty");
98 pd = fetchDescriptor(props, "fluentGetProperty");
99 assertNotNull(pd.getReadMethod(), "No read method for fluentGetProperty");
100 assertNotNull(pd.getWriteMethod(), "No write method for fluentGetProperty");
101 }
102
103 @Test
104 public void testIntrospectionCaps() throws Exception {
105 final PropertyUtilsBean pu = new PropertyUtilsBean();
106
107 final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
108
109 pu.addBeanIntrospector(introspector);
110
111 final Map<String, PropertyDescriptor> props = createDescriptorMap(pu.getPropertyDescriptors(CapsBean.class));
112
113 final PropertyDescriptor aDescriptor = fetchDescriptor(props, "URI");
114
115 assertNotNull(aDescriptor, "missing property");
116
117 assertNotNull(aDescriptor.getReadMethod(), "No read method for uri");
118 assertNotNull(aDescriptor.getWriteMethod(), "No write method for uri");
119
120 assertNull(props.get("uRI"), "Should not find mis-capitalized property");
121 }
122 }