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