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.plugins.s2.beans;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  
22  import org.junit.Test;
23  import org.seasar.cubby.plugins.s2.spi.S2BeanDescProvider;
24  import org.seasar.cubby.spi.BeanDescProvider;
25  import org.seasar.cubby.spi.beans.Attribute;
26  import org.seasar.cubby.spi.beans.BeanDesc;
27  
28  public class S2BeanDescProviderTest {
29  
30  	BeanDescProvider beanDescProvider = new S2BeanDescProvider();
31  
32  	@Test
33  	public void propertyDesc() throws Exception {
34  		final BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
35  		assertEquals(5, beanDesc.findtPropertyAttributes().size());
36  		Attribute propDesc = beanDesc.getPropertyAttribute("aaa");
37  		assertEquals("aaa", propDesc.getName());
38  		assertEquals(String.class, propDesc.getType());
39  		assertTrue(propDesc.isReadable());
40  		assertFalse(propDesc.isWritable());
41  
42  		propDesc = beanDesc.getPropertyAttribute("CCC");
43  		assertEquals("CCC", propDesc.getName());
44  		assertEquals(boolean.class, propDesc.getType());
45  		assertTrue(propDesc.isReadable());
46  		assertFalse(propDesc.isWritable());
47  
48  		propDesc = beanDesc.getPropertyAttribute("eee");
49  		assertEquals("eee", propDesc.getName());
50  		assertEquals(String.class, propDesc.getType());
51  		assertTrue(propDesc.isReadable());
52  		assertTrue(propDesc.isWritable());
53  
54  		propDesc = beanDesc.getPropertyAttribute("fff");
55  		assertEquals("fff", propDesc.getName());
56  		assertEquals(Boolean.class, propDesc.getType());
57  
58  		assertFalse(beanDesc.hasPropertyAttribute("hhh"));
59  		assertFalse(beanDesc.hasPropertyAttribute("iii"));
60  	}
61  
62  	@Test
63  	public void invalidProperty() throws Exception {
64  		final BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean2.class);
65  		assertEquals("1", false, beanDesc.hasPropertyAttribute("aaa"));
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(final 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(final 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(final 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(final Number arg1, final Number arg2) {
174 			return Integer.valueOf(3);
175 		}
176 
177 		/**
178 		 * @param arg1
179 		 * @param arg2
180 		 * @return
181 		 */
182 		public int add2(final int arg1, final int arg2) {
183 			return arg1 + arg2;
184 		}
185 
186 		/**
187 		 * @param arg
188 		 * @return
189 		 */
190 		public Integer echo(final 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 		/**
207          * 
208          */
209 		public MyBean2() {
210 		}
211 
212 		/**
213 		 * @param num
214 		 * @param text
215 		 * @param bean1
216 		 * @param bean2
217 		 */
218 		public MyBean2(final int num, final String text, final MyBean bean1,
219 				final MyBean2 bean2) {
220 		}
221 
222 		/**
223 		 * @param i
224 		 */
225 		public void setAaa(final int i) {
226 		}
227 
228 		/**
229 		 * @param s
230 		 */
231 		public void setAaa(final String s) {
232 		}
233 	}
234 
235 }