1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.spi.beans.impl;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import org.junit.Test;
26 import org.seasar.cubby.spi.BeanDescProvider;
27 import org.seasar.cubby.spi.beans.BeanDesc;
28 import org.seasar.cubby.spi.beans.PropertyDesc;
29 import org.seasar.cubby.spi.beans.PropertyNotFoundException;
30 import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
31
32 public class DefaultBeanDescProviderTest {
33
34 BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
35
36 @Test
37 public void propertyDesc() throws Exception {
38 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
39 PropertyDesc propDesc = beanDesc.getPropertyDesc("aaa");
40 assertEquals("aaa", propDesc.getPropertyName());
41 assertEquals(String.class, propDesc.getPropertyType());
42 assertNotNull(propDesc.getReadMethod());
43 assertNull(propDesc.getWriteMethod());
44
45 propDesc = beanDesc.getPropertyDesc("CCC");
46 assertEquals("CCC", propDesc.getPropertyName());
47 assertEquals(boolean.class, propDesc.getPropertyType());
48 assertNotNull(propDesc.getReadMethod());
49 assertNull(propDesc.getWriteMethod());
50
51 propDesc = beanDesc.getPropertyDesc("eee");
52 assertEquals("eee", propDesc.getPropertyName());
53 assertEquals(String.class, propDesc.getPropertyType());
54 assertNotNull(propDesc.getReadMethod());
55 assertNotNull(propDesc.getWriteMethod());
56
57 try {
58 propDesc = beanDesc.getPropertyDesc("fff");
59 fail();
60 assertEquals("fff", propDesc.getPropertyName());
61 assertEquals(Boolean.class, propDesc.getPropertyType());
62 } catch (PropertyNotFoundException e) {
63
64 }
65
66 assertFalse(beanDesc.hasPropertyDesc("hhh"));
67 assertFalse(beanDesc.hasPropertyDesc("iii"));
68 }
69
70 @Test
71 public void invalidProperty() throws Exception {
72 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean2.class);
73 assertTrue("1", beanDesc.hasPropertyDesc("aaa"));
74 assertFalse("1", beanDesc.hasPropertyDesc("bbb"));
75 }
76
77
78
79
80 public static interface MyInterface {
81
82
83
84 String HOGE = "hoge";
85 }
86
87
88
89
90 public static interface MyInterface2 extends MyInterface {
91
92
93
94 String HOGE = "hoge2";
95 }
96
97
98
99
100 public static class MyBean implements MyInterface2 {
101
102 private String aaa;
103
104 private String eee;
105
106
107
108
109 public String ggg;
110
111
112
113
114 public String getAaa() {
115 return aaa;
116 }
117
118
119
120
121
122 public String getBbb(Object a) {
123 return null;
124 }
125
126
127
128
129 public boolean isCCC() {
130 return true;
131 }
132
133
134
135
136 public Object isDdd() {
137 return null;
138 }
139
140
141
142
143 public String getEee() {
144 return eee;
145 }
146
147
148
149
150 public void setEee(String eee) {
151 this.eee = eee;
152 }
153
154
155
156
157 public Boolean isFff() {
158 return null;
159 }
160
161
162
163
164
165 public MyBean setHhh(String hhh) {
166 return this;
167 }
168
169
170
171
172 public void getIii() {
173 }
174
175
176
177
178
179
180 public Number add(Number arg1, Number arg2) {
181 return new Integer(3);
182 }
183
184
185
186
187
188
189 public int add2(int arg1, int arg2) {
190 return arg1 + arg2;
191 }
192
193
194
195
196
197 public Integer echo(Integer arg) {
198 return arg;
199 }
200
201
202
203
204 public void throwException() {
205 throw new IllegalStateException("hoge");
206 }
207 }
208
209
210
211
212 public class MyBean2 {
213
214
215
216 public MyBean2() {
217 }
218
219
220
221
222
223
224
225 public MyBean2(int num, String text, MyBean bean1, MyBean2 bean2) {
226 }
227
228
229
230
231 public void setAaa(int i) {
232 }
233
234
235
236
237 public void setAaa(String s) {
238 }
239
240 public Boolean isBbb() {
241 return null;
242 }
243 }
244
245 }