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.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22
23 import java.beans.PropertyDescriptor;
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class BeanIntrospectionDataTest {
34
35 private static final Class<?> BEAN_CLASS = FluentIntrospectionTestBean.class;
36
37
38 private static final String TEST_PROP = "fluentGetProperty";
39
40
41
42
43
44
45 private static PropertyDescriptor[] fetchDescriptors() {
46 final PropertyUtilsBean pub = new PropertyUtilsBean();
47 pub.removeBeanIntrospector(SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS);
48 pub.addBeanIntrospector(new FluentPropertyBeanIntrospector());
49 return pub.getPropertyDescriptors(BEAN_CLASS);
50 }
51
52
53
54
55
56
57
58 private static PropertyDescriptor fetchTestDescriptor(final BeanIntrospectionData bid) {
59 return bid.getDescriptor(TEST_PROP);
60 }
61
62
63
64
65
66
67 private static BeanIntrospectionData setUpData() {
68 return new BeanIntrospectionData(fetchDescriptors());
69 }
70
71
72
73
74 @Test
75 public void testGetWriteMethodDefined() {
76 final BeanIntrospectionData data = setUpData();
77 final PropertyDescriptor pd = fetchTestDescriptor(data);
78 assertNotNull(pd.getWriteMethod(), "No write method");
79 assertEquals(pd.getWriteMethod(), data.getWriteMethod(BEAN_CLASS, pd), "Wrong write method");
80 }
81
82
83
84
85 @Test
86 public void testGetWriteMethodNonExisting() throws Exception {
87 final PropertyDescriptor pd = new PropertyDescriptor(TEST_PROP, BEAN_CLASS.getMethod("getFluentGetProperty"),
88 BEAN_CLASS.getMethod("setFluentGetProperty", String.class));
89 final Map<String, String> methods = new HashMap<>();
90 methods.put(TEST_PROP, "hashCode");
91 final BeanIntrospectionData data = new BeanIntrospectionData(new PropertyDescriptor[] { pd }, methods);
92 pd.setWriteMethod(null);
93 assertNull(data.getWriteMethod(BEAN_CLASS, pd), "Got a write method");
94 }
95
96
97
98
99 @Test
100 public void testGetWriteMethodUndefined() throws Exception {
101 final BeanIntrospectionData data = setUpData();
102 final PropertyDescriptor pd = fetchTestDescriptor(data);
103 final Method writeMethod = pd.getWriteMethod();
104 pd.setWriteMethod(null);
105 assertEquals(writeMethod, data.getWriteMethod(BEAN_CLASS, pd), "Wrong write method");
106 assertEquals(writeMethod, pd.getWriteMethod(), "Method not set in descriptor");
107 }
108
109
110
111
112 @Test
113 public void testGetWriteMethodUnknown() {
114 final BeanIntrospectionData data = setUpData();
115 final PropertyDescriptor pd = data.getDescriptor("class");
116 assertNull(data.getWriteMethod(BEAN_CLASS, pd), "Got a write method");
117 }
118 }