1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.s2.beans;
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.plugins.s2.spi.S2BeanDescProvider;
28  import org.seasar.cubby.spi.BeanDescProvider;
29  import org.seasar.cubby.spi.beans.BeanDesc;
30  import org.seasar.cubby.spi.beans.IllegalAttributeException;
31  import org.seasar.cubby.spi.beans.Attribute;
32  
33  public class S2BeanDescProviderPropertyDescTest {
34  
35  	BeanDescProvider beanDescProvider = new S2BeanDescProvider();
36  
37  	@Test
38  	public void setValue() throws Exception {
39          MyBean myBean = new MyBean();
40          BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
41          Attribute attribute = beanDesc.getPropertyAttribute("fff");
42          attribute.setValue(myBean, new BigDecimal(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 attribute = beanDesc.getPropertyAttribute("fff");
51          attribute.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 attribute = beanDesc.getPropertyAttribute("aaa");
60          try {
61              attribute.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 attribute = beanDesc.getPropertyAttribute("jjj");
73          try {
74              attribute.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 attribute = beanDesc.getPropertyAttribute("iii");
86          try {
87              attribute.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 attribute = beanDesc.getPropertyAttribute("kkk");
99          try {
100             attribute.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 attribute = beanDesc.getPropertyAttribute("fff");
112         try {
113             attribute.setValue(myBean, "hoge");
114             fail("1");
115         } catch (IllegalAttributeException ex) {
116             System.out.println(ex);
117         }
118     }
119 
120     /**
121      * 
122      */
123     public static class MyBean {
124 
125         private int fff_;
126 
127         private BigDecimal ggg_;
128 
129         private Timestamp hhh_;
130 
131         private String jjj;
132 
133         String kkk;
134 
135         private URL url_;
136 
137         private Calendar cal;
138 
139         /**
140          * 
141          */
142         public String str;
143 
144         /**
145          * @return
146          */
147         public String getAaa() {
148             return null;
149         }
150 
151         /**
152          * @param a
153          * @return
154          */
155         public String getBbb(Object a) {
156             return null;
157         }
158 
159         /**
160          * @return
161          */
162         public boolean isCCC() {
163             return true;
164         }
165 
166         /**
167          * @return
168          */
169         public Object isDdd() {
170             return null;
171         }
172 
173         /**
174          * @return
175          */
176         public String getEee() {
177             return null;
178         }
179 
180         /**
181          * @param eee
182          */
183         public void setEee(String eee) {
184         }
185 
186         /**
187          * @return
188          */
189         public int getFff() {
190             return fff_;
191         }
192 
193         /**
194          * @param fff
195          */
196         public void setFff(int fff) {
197             fff_ = fff;
198         }
199 
200         /**
201          * @return
202          */
203         public String getJjj() {
204             return jjj;
205         }
206 
207         /**
208          * @param kkk
209          */
210         public void setKkk(String kkk) {
211             this.kkk = kkk;
212         }
213 
214         /**
215          * @param arg1
216          * @param arg2
217          * @return
218          */
219         public Number add(Number arg1, Number arg2) {
220             return Integer.valueOf(3);
221         }
222 
223         /**
224          * @return
225          */
226         public BigDecimal getGgg() {
227             return ggg_;
228         }
229 
230         /**
231          * @param ggg
232          */
233         public void setGgg(BigDecimal ggg) {
234             this.ggg_ = ggg;
235         }
236 
237         /**
238          * @return
239          */
240         public Timestamp getHhh() {
241             return hhh_;
242         }
243 
244         /**
245          * @param hhh
246          */
247         public void setHhh(Timestamp hhh) {
248             this.hhh_ = hhh;
249         }
250 
251         /**
252          * @param iii
253          */
254         public void setIii(String iii) {
255         }
256 
257         /**
258          * @return
259          */
260         public URL getURL() {
261             return url_;
262         }
263 
264         /**
265          * @param url
266          */
267         public void setURL(URL url) {
268             url_ = url;
269         }
270 
271         /**
272          * @return Returns the cal.
273          */
274         public Calendar getCal() {
275             return cal;
276         }
277 
278         /**
279          * @param cal
280          *            The cal to set.
281          */
282         public void setCal(Calendar cal) {
283             this.cal = cal;
284         }
285     }
286 
287 }