1   /*
2    * Copyright 2004-2010 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  
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 		 * @return
143 		 */
144 		public String getAaa() {
145 			return null;
146 		}
147 
148 		/**
149 		 * @param a
150 		 * @return
151 		 */
152 		public String getBbb(Object a) {
153 			return null;
154 		}
155 
156 		/**
157 		 * @return
158 		 */
159 		public boolean isCCC() {
160 			return true;
161 		}
162 
163 		/**
164 		 * @return
165 		 */
166 		public Object isDdd() {
167 			return null;
168 		}
169 
170 		/**
171 		 * @return
172 		 */
173 		public String getEee() {
174 			return null;
175 		}
176 
177 		/**
178 		 * @param eee
179 		 */
180 		public void setEee(String eee) {
181 		}
182 
183 		/**
184 		 * @return
185 		 */
186 		public int getFff() {
187 			return fff_;
188 		}
189 
190 		/**
191 		 * @param fff
192 		 */
193 		public void setFff(int fff) {
194 			fff_ = fff;
195 		}
196 
197 		/**
198 		 * @return
199 		 */
200 		public String getJjj() {
201 			return jjj;
202 		}
203 
204 		/**
205 		 * @param kkk
206 		 */
207 		public void setKkk(String kkk) {
208 			this.kkk = kkk;
209 		}
210 
211 		/**
212 		 * @param arg1
213 		 * @param arg2
214 		 * @return
215 		 */
216 		public Number add(Number arg1, Number arg2) {
217 			return Integer.valueOf(3);
218 		}
219 
220 		/**
221 		 * @return
222 		 */
223 		public BigDecimal getGgg() {
224 			return ggg_;
225 		}
226 
227 		/**
228 		 * @param ggg
229 		 */
230 		public void setGgg(BigDecimal ggg) {
231 			this.ggg_ = ggg;
232 		}
233 
234 		/**
235 		 * @return
236 		 */
237 		public Timestamp getHhh() {
238 			return hhh_;
239 		}
240 
241 		/**
242 		 * @param hhh
243 		 */
244 		public void setHhh(Timestamp hhh) {
245 			this.hhh_ = hhh;
246 		}
247 
248 		/**
249 		 * @param iii
250 		 */
251 		public void setIii(String iii) {
252 		}
253 
254 		/**
255 		 * @return
256 		 */
257 		public URL getURL() {
258 			return url_;
259 		}
260 
261 		/**
262 		 * @param url
263 		 */
264 		public void setURL(URL url) {
265 			url_ = url;
266 		}
267 
268 		/**
269 		 * @return Returns the cal.
270 		 */
271 		public Calendar getCal() {
272 			return cal;
273 		}
274 
275 		/**
276 		 * @param cal
277 		 *            The cal to set.
278 		 */
279 		public void setCal(Calendar cal) {
280 			this.cal = cal;
281 		}
282 	}
283 
284 }