1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
18
19 import java.beans.IntrospectionException;
20 import java.beans.PropertyDescriptor;
21 import java.net.URI;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import junit.framework.TestCase;
26
27
28
29
30
31
32 public class FluentPropertyBeanIntrospectorTestCase extends TestCase {
33
34
35
36
37
38
39
40 private static Map<String, PropertyDescriptor> createDescriptorMap(
41 final PropertyDescriptor[] descs) {
42 final Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
43 for (final PropertyDescriptor pd : descs) {
44 map.put(pd.getName(), pd);
45 }
46 return map;
47 }
48
49
50
51
52
53
54
55
56
57 private static PropertyDescriptor fetchDescriptor(
58 final Map<String, PropertyDescriptor> props, final String name) {
59 assertTrue("Property not found: " + name, props.containsKey(name));
60 return props.get(name);
61 }
62
63
64
65
66 public void testInitNoPrefix() {
67 try {
68 new FluentPropertyBeanIntrospector(null);
69 fail("Missing prefix for write methods not detected!");
70 } catch (final IllegalArgumentException iex) {
71
72 }
73 }
74
75
76
77
78 public void testIntrospection() throws IntrospectionException {
79 final PropertyUtilsBean pu = new PropertyUtilsBean();
80 final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
81 pu.addBeanIntrospector(introspector);
82 final Map<String, PropertyDescriptor> props = createDescriptorMap(pu
83 .getPropertyDescriptors(FluentIntrospectionTestBean.class));
84 PropertyDescriptor pd = fetchDescriptor(props, "name");
85 assertNotNull("No read method for name", pd.getReadMethod());
86 assertNotNull("No write method for name", pd.getWriteMethod());
87 fetchDescriptor(props, "stringProperty");
88 pd = fetchDescriptor(props, "fluentProperty");
89 assertNull("Read method for fluentProperty", pd.getReadMethod());
90 assertNotNull("No write method for fluentProperty", pd.getWriteMethod());
91 pd = fetchDescriptor(props, "fluentGetProperty");
92 assertNotNull("No read method for fluentGetProperty",
93 pd.getReadMethod());
94 assertNotNull("No write method for fluentGetProperty",
95 pd.getWriteMethod());
96 }
97
98 public void testIntrospectionCaps() throws Exception {
99 final PropertyUtilsBean pu = new PropertyUtilsBean();
100
101 final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
102
103 pu.addBeanIntrospector(introspector);
104
105 final Map<String, PropertyDescriptor> props = createDescriptorMap(
106 pu.getPropertyDescriptors(CapsBean.class));
107
108 PropertyDescriptor aDescriptor = fetchDescriptor(props, "URI");
109
110 assertNotNull("missing property", aDescriptor);
111
112 assertNotNull("No read method for uri", aDescriptor.getReadMethod());
113 assertNotNull("No write method for uri", aDescriptor.getWriteMethod());
114
115 assertNull("Should not find mis-capitalized property", props.get("uRI"));
116 }
117
118 public static final class CapsBean {
119 private URI mURI;
120
121 public URI getURI() {
122 return mURI;
123 }
124
125 public void setURI(final URI theURI) {
126 mURI = theURI;
127 }
128 }
129 }