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