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