1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.spi.beans.impl;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21
22 import java.math.BigDecimal;
23 import java.net.URL;
24 import java.sql.Timestamp;
25 import java.util.Calendar;
26
27 import org.junit.Test;
28 import org.seasar.cubby.spi.BeanDescProvider;
29 import org.seasar.cubby.spi.beans.Attribute;
30 import org.seasar.cubby.spi.beans.BeanDesc;
31 import org.seasar.cubby.spi.beans.IllegalAttributeException;
32
33 public class DefaultBeanDescProviderAttributeTest {
34
35 BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
36
37 @Test
38 public void setValue() throws Exception {
39 MyBean myBean = new MyBean();
40 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
41 Attribute propDesc = beanDesc.getPropertyAttribute("fff");
42 propDesc.setValue(myBean, 2);
43 assertEquals(2, myBean.getFff());
44 }
45
46 @Test
47 public void setValue_null() throws Exception {
48 MyBean myBean = new MyBean();
49 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
50 Attribute propDesc = beanDesc.getPropertyAttribute("fff");
51 propDesc.setValue(myBean, null);
52 assertEquals(0, myBean.getFff());
53 }
54
55 @Test
56 public void setValue_notWritable() throws Exception {
57 MyBean myBean = new MyBean();
58 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
59 Attribute propDesc = beanDesc.getPropertyAttribute("aaa");
60 try {
61 propDesc.setValue(myBean, null);
62 fail();
63 } catch (IllegalAttributeException e) {
64 System.out.println(e);
65 }
66 }
67
68 @Test
69 public void setValue_notWritableWithField() throws Exception {
70 MyBean myBean = new MyBean();
71 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
72 Attribute propDesc = beanDesc.getPropertyAttribute("jjj");
73 try {
74 propDesc.setValue(myBean, null);
75 fail();
76 } catch (IllegalAttributeException e) {
77 System.out.println(e);
78 }
79 }
80
81 @Test
82 public void getValue_notReable() throws Exception {
83 MyBean myBean = new MyBean();
84 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
85 Attribute propDesc = beanDesc.getPropertyAttribute("iii");
86 try {
87 propDesc.getValue(myBean);
88 fail();
89 } catch (IllegalAttributeException e) {
90 System.out.println(e);
91 }
92 }
93
94 @Test
95 public void getValue_notReableWithField() throws Exception {
96 MyBean myBean = new MyBean();
97 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
98 Attribute propDesc = beanDesc.getPropertyAttribute("kkk");
99 try {
100 propDesc.getValue(myBean);
101 fail();
102 } catch (IllegalAttributeException e) {
103 System.out.println(e);
104 }
105 }
106
107 @Test
108 public void setIllegalValue() throws Exception {
109 MyBean myBean = new MyBean();
110 BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
111 Attribute propDesc = beanDesc.getPropertyAttribute("fff");
112 try {
113 propDesc.setValue(myBean, "hoge");
114 fail("1");
115 } catch (IllegalAttributeException ex) {
116 System.out.println(ex);
117 }
118 }
119
120 public static class MyBean {
121
122 private int fff_;
123
124 private BigDecimal ggg_;
125
126 private Timestamp hhh_;
127
128 private String jjj;
129
130 String kkk;
131
132 private URL url_;
133
134 private Calendar cal;
135
136
137
138
139 public String str;
140
141
142
143
144 public String getAaa() {
145 return null;
146 }
147
148
149
150
151
152 public String getBbb(Object a) {
153 return null;
154 }
155
156
157
158
159 public boolean isCCC() {
160 return true;
161 }
162
163
164
165
166 public Object isDdd() {
167 return null;
168 }
169
170
171
172
173 public String getEee() {
174 return null;
175 }
176
177
178
179
180 public void setEee(String eee) {
181 }
182
183
184
185
186 public int getFff() {
187 return fff_;
188 }
189
190
191
192
193 public void setFff(int fff) {
194 fff_ = fff;
195 }
196
197
198
199
200 public String getJjj() {
201 return jjj;
202 }
203
204
205
206
207 public void setKkk(String kkk) {
208 this.kkk = kkk;
209 }
210
211
212
213
214
215
216 public Number add(Number arg1, Number arg2) {
217 return Integer.valueOf(3);
218 }
219
220
221
222
223 public BigDecimal getGgg() {
224 return ggg_;
225 }
226
227
228
229
230 public void setGgg(BigDecimal ggg) {
231 this.ggg_ = ggg;
232 }
233
234
235
236
237 public Timestamp getHhh() {
238 return hhh_;
239 }
240
241
242
243
244 public void setHhh(Timestamp hhh) {
245 this.hhh_ = hhh;
246 }
247
248
249
250
251 public void setIii(String iii) {
252 }
253
254
255
256
257 public URL getURL() {
258 return url_;
259 }
260
261
262
263
264 public void setURL(URL url) {
265 url_ = url;
266 }
267
268
269
270
271 public Calendar getCal() {
272 return cal;
273 }
274
275
276
277
278
279 public void setCal(Calendar cal) {
280 this.cal = cal;
281 }
282 }
283
284 }