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