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.spi.beans.impl;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import org.junit.Test;
26  import org.seasar.cubby.spi.BeanDescProvider;
27  import org.seasar.cubby.spi.beans.BeanDesc;
28  import org.seasar.cubby.spi.beans.PropertyDesc;
29  import org.seasar.cubby.spi.beans.PropertyNotFoundException;
30  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
31  
32  public class DefaultBeanDescProviderTest {
33  
34  	BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
35  
36  	@Test
37  	public void propertyDesc() throws Exception {
38  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
39  		PropertyDesc propDesc = beanDesc.getPropertyDesc("aaa");
40  		assertEquals("aaa", propDesc.getPropertyName());
41  		assertEquals(String.class, propDesc.getPropertyType());
42  		assertNotNull(propDesc.getReadMethod());
43  		assertNull(propDesc.getWriteMethod());
44  
45  		propDesc = beanDesc.getPropertyDesc("CCC");
46  		assertEquals("CCC", propDesc.getPropertyName());
47  		assertEquals(boolean.class, propDesc.getPropertyType());
48  		assertNotNull(propDesc.getReadMethod());
49  		assertNull(propDesc.getWriteMethod());
50  
51  		propDesc = beanDesc.getPropertyDesc("eee");
52  		assertEquals("eee", propDesc.getPropertyName());
53  		assertEquals(String.class, propDesc.getPropertyType());
54  		assertNotNull(propDesc.getReadMethod());
55  		assertNotNull(propDesc.getWriteMethod());
56  
57  		try {
58  			propDesc = beanDesc.getPropertyDesc("fff");
59  			fail();
60  			assertEquals("fff", propDesc.getPropertyName());
61  			assertEquals(Boolean.class, propDesc.getPropertyType());
62  		} catch (PropertyNotFoundException e) {
63  
64  		}
65  
66  		assertFalse(beanDesc.hasPropertyDesc("hhh"));
67  		assertFalse(beanDesc.hasPropertyDesc("iii"));
68  	}
69  
70  	@Test
71  	public void invalidProperty() throws Exception {
72  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean2.class);
73  		assertTrue("1", beanDesc.hasPropertyDesc("aaa"));
74  		assertFalse("1", beanDesc.hasPropertyDesc("bbb"));
75  	}
76  
77  	/**
78       * 
79       */
80  	public static interface MyInterface {
81  		/**
82           * 
83           */
84  		String HOGE = "hoge";
85  	}
86  
87  	/**
88       * 
89       */
90  	public static interface MyInterface2 extends MyInterface {
91  		/**
92           * 
93           */
94  		String HOGE = "hoge2";
95  	}
96  
97  	/**
98       * 
99       */
100 	public static class MyBean implements MyInterface2 {
101 
102 		private String aaa;
103 
104 		private String eee;
105 
106 		/**
107          * 
108          */
109 		public String ggg;
110 
111 		/**
112 		 * @return
113 		 */
114 		public String getAaa() {
115 			return aaa;
116 		}
117 
118 		/**
119 		 * @param a
120 		 * @return
121 		 */
122 		public String getBbb(Object a) {
123 			return null;
124 		}
125 
126 		/**
127 		 * @return
128 		 */
129 		public boolean isCCC() {
130 			return true;
131 		}
132 
133 		/**
134 		 * @return
135 		 */
136 		public Object isDdd() {
137 			return null;
138 		}
139 
140 		/**
141 		 * @return
142 		 */
143 		public String getEee() {
144 			return eee;
145 		}
146 
147 		/**
148 		 * @param eee
149 		 */
150 		public void setEee(String eee) {
151 			this.eee = eee;
152 		}
153 
154 		/**
155 		 * @return
156 		 */
157 		public Boolean isFff() {
158 			return null;
159 		}
160 
161 		/**
162 		 * @param hhh
163 		 * @return
164 		 */
165 		public MyBean setHhh(String hhh) {
166 			return this;
167 		}
168 
169 		/**
170          * 
171          */
172 		public void getIii() {
173 		}
174 
175 		/**
176 		 * @param arg1
177 		 * @param arg2
178 		 * @return
179 		 */
180 		public Number add(Number arg1, Number arg2) {
181 			return new Integer(3);
182 		}
183 
184 		/**
185 		 * @param arg1
186 		 * @param arg2
187 		 * @return
188 		 */
189 		public int add2(int arg1, int arg2) {
190 			return arg1 + arg2;
191 		}
192 
193 		/**
194 		 * @param arg
195 		 * @return
196 		 */
197 		public Integer echo(Integer arg) {
198 			return arg;
199 		}
200 
201 		/**
202          * 
203          */
204 		public void throwException() {
205 			throw new IllegalStateException("hoge");
206 		}
207 	}
208 
209 	/**
210      * 
211      */
212 	public class MyBean2 {
213 		/**
214          * 
215          */
216 		public MyBean2() {
217 		}
218 
219 		/**
220 		 * @param num
221 		 * @param text
222 		 * @param bean1
223 		 * @param bean2
224 		 */
225 		public MyBean2(int num, String text, MyBean bean1, MyBean2 bean2) {
226 		}
227 
228 		/**
229 		 * @param i
230 		 */
231 		public void setAaa(int i) {
232 		}
233 
234 		/**
235 		 * @param s
236 		 */
237 		public void setAaa(String s) {
238 		}
239 
240 		public Boolean isBbb() {
241 			return null;
242 		}
243 	}
244 
245 }