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