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.util.HashSet;
22 import java.util.Set;
23
24 import junit.framework.TestCase;
25
26
27
28
29
30
31 public class DefaultIntrospectionContextTestCase extends TestCase {
32
33 private static final String PROP = "foo";
34
35
36 private DefaultIntrospectionContext context;
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 context = new DefaultIntrospectionContext(getClass());
42 }
43
44
45
46
47
48
49
50 private static PropertyDescriptor createDescriptor(final String propName) {
51 try {
52 return new PropertyDescriptor(propName,
53 DefaultIntrospectionContextTestCase.class, null, null);
54 } catch (final IntrospectionException e) {
55 throw new IllegalStateException("Unexpected exception: " + e);
56 }
57 }
58
59
60
61
62 public void testInit() {
63 assertEquals("Wrong current class", getClass(),
64 context.getTargetClass());
65 assertTrue("Got property names", context.propertyNames().isEmpty());
66 }
67
68
69
70
71 public void testAddPropertyDescriptor() {
72 final PropertyDescriptor desc = createDescriptor(PROP);
73 context.addPropertyDescriptor(desc);
74 assertTrue("Property not found", context.hasProperty(PROP));
75 assertSame("Wrong descriptor", desc,
76 context.getPropertyDescriptor(PROP));
77 }
78
79
80
81
82 public void testAddPropertyDescriptorNull() {
83 try {
84 context.addPropertyDescriptor(null);
85 fail("Could add null descriptor!");
86 } catch (final IllegalArgumentException iex) {
87
88 }
89 }
90
91
92
93
94 public void testAddPropertyDescriptors() {
95 final int count = 4;
96 final PropertyDescriptor[] descs = new PropertyDescriptor[count];
97 final Set<PropertyDescriptor> descSet = new HashSet<PropertyDescriptor>();
98 for (int i = 0; i < count; i++) {
99 descs[i] = createDescriptor(PROP + i);
100 descSet.add(descs[i]);
101 }
102 context.addPropertyDescriptors(descs);
103 final PropertyDescriptor d = createDescriptor(PROP);
104 context.addPropertyDescriptor(d);
105 descSet.add(d);
106 final Set<String> names = context.propertyNames();
107 assertEquals("Wrong number of property names", count + 1, names.size());
108 assertTrue("Property not found: " + PROP, names.contains(PROP));
109 for (int i = 0; i < count; i++) {
110 assertTrue("Property not found: " + (PROP + i),
111 names.contains(PROP + i));
112 }
113 final PropertyDescriptor[] addedDescs = context.getPropertyDescriptors();
114 assertEquals("Wrong number of added descriptors", count + 1,
115 addedDescs.length);
116 for (final PropertyDescriptor pd : addedDescs) {
117 assertTrue("Unexpected descriptor: " + pd, descSet.remove(pd));
118 }
119 }
120
121
122
123
124 public void testAddPropertyDescriptorsNull() {
125 try {
126 context.addPropertyDescriptors(null);
127 fail("Could add a null array with descriptors!");
128 } catch (final IllegalArgumentException iex) {
129
130 }
131 }
132
133
134
135
136 public void testHasPropertyFalse() {
137 assertFalse("Wrong result (1)", context.hasProperty(PROP));
138 context.addPropertyDescriptor(createDescriptor(PROP));
139 assertFalse("Wrong result (2)", context.hasProperty("other"));
140 }
141
142
143
144
145 public void testGetPropertyDescriptorUnknown() {
146 assertNull("Got a property (1)", context.getPropertyDescriptor(PROP));
147 context.addPropertyDescriptor(createDescriptor(PROP));
148 assertNull("Got a property (2)", context.getPropertyDescriptor("other"));
149 }
150
151
152
153
154 public void testPropertyNamesModify() {
155 final Set<String> names = context.propertyNames();
156 try {
157 names.add(PROP);
158 fail("Could modify property names set!");
159 } catch (final UnsupportedOperationException uex) {
160
161 }
162 }
163
164
165
166
167 public void testRemovePropertyDescriptor() {
168 context.addPropertyDescriptor(createDescriptor(PROP));
169 context.removePropertyDescriptor(PROP);
170 assertTrue("Got property names", context.propertyNames().isEmpty());
171 assertEquals("Got descriptors", 0,
172 context.getPropertyDescriptors().length);
173 }
174 }