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.assertNotNull;
21  import static org.junit.Assert.assertNull;
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.BeanDesc;
27  import org.seasar.cubby.spi.beans.PropertyDesc;
28  
29  public class S2BeanDescProviderTest {
30  
31  	BeanDescProvider beanDescProvider = new S2BeanDescProvider();
32  
33  	@Test
34      public void propertyDesc() throws Exception {
35          BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
36          assertEquals(5, beanDesc.getPropertyDescs().length);
37          PropertyDesc propDesc = beanDesc.getPropertyDesc("aaa");
38          assertEquals("aaa", propDesc.getPropertyName());
39          assertEquals(String.class, propDesc.getPropertyType());
40          assertNotNull(propDesc.getReadMethod());
41          assertNull(propDesc.getWriteMethod());
42  
43          propDesc = beanDesc.getPropertyDesc("CCC");
44          assertEquals("CCC", propDesc.getPropertyName());
45          assertEquals(boolean.class, propDesc.getPropertyType());
46          assertNotNull(propDesc.getReadMethod());
47          assertNull(propDesc.getWriteMethod());
48  
49          propDesc = beanDesc.getPropertyDesc("eee");
50          assertEquals("eee", propDesc.getPropertyName());
51          assertEquals(String.class, propDesc.getPropertyType());
52          assertNotNull(propDesc.getReadMethod());
53          assertNotNull(propDesc.getWriteMethod());
54  
55          propDesc = beanDesc.getPropertyDesc("fff");
56          assertEquals("fff", propDesc.getPropertyName());
57          assertEquals(Boolean.class, propDesc.getPropertyType());
58  
59          assertFalse(beanDesc.hasPropertyDesc("hhh"));
60          assertFalse(beanDesc.hasPropertyDesc("iii"));
61      }
62  
63  	@Test
64      public void invalidProperty() throws Exception {
65          BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean2.class);
66          assertEquals("1", false, beanDesc.hasPropertyDesc("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          /**
99           * 
100          */
101         public String ggg;
102 
103         /**
104          * @return
105          */
106         public String getAaa() {
107             return aaa;
108         }
109 
110         /**
111          * @param a
112          * @return
113          */
114         public String getBbb(Object a) {
115             return null;
116         }
117 
118         /**
119          * @return
120          */
121         public boolean isCCC() {
122             return true;
123         }
124 
125         /**
126          * @return
127          */
128         public Object isDdd() {
129             return null;
130         }
131 
132         /**
133          * @return
134          */
135         public String getEee() {
136             return eee;
137         }
138 
139         /**
140          * @param eee
141          */
142         public void setEee(String eee) {
143             this.eee = eee;
144         }
145 
146         /**
147          * @return
148          */
149         public Boolean isFff() {
150             return null;
151         }
152 
153         /**
154          * @param hhh
155          * @return
156          */
157         public MyBean setHhh(String hhh) {
158             return this;
159         }
160 
161         /**
162          * 
163          */
164         public void getIii() {
165         }
166 
167         /**
168          * @param arg1
169          * @param arg2
170          * @return
171          */
172         public Number add(Number arg1, Number arg2) {
173             return new Integer(3);
174         }
175 
176         /**
177          * @param arg1
178          * @param arg2
179          * @return
180          */
181         public int add2(int arg1, int arg2) {
182             return arg1 + arg2;
183         }
184 
185         /**
186          * @param arg
187          * @return
188          */
189         public Integer echo(Integer arg) {
190             return arg;
191         }
192 
193         /**
194          * 
195          */
196         public void throwException() {
197             throw new IllegalStateException("hoge");
198         }
199     }
200 
201     /**
202      * 
203      */
204     public class MyBean2 {
205         /**
206          * 
207          */
208         public MyBean2() {
209         }
210 
211         /**
212          * @param num
213          * @param text
214          * @param bean1
215          * @param bean2
216          */
217         public MyBean2(int num, String text, MyBean bean1, MyBean2 bean2) {
218         }
219 
220         /**
221          * @param i
222          */
223         public void setAaa(int i) {
224         }
225 
226         /**
227          * @param s
228          */
229         public void setAaa(String s) {
230         }
231     }
232 
233 }