KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > ConstructorUtilsTestCase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28
29 /**
30  * <p> Test case for <code>ConstructorUtils</code> </p>
31  *
32  */

33 public class ConstructorUtilsTestCase extends TestCase {
34
35     // ---------------------------------------------------------- Constructors
36

37     /**
38      * Construct a new instance of this test case.
39      *
40      * @param name Name of the test case
41      */

42     public ConstructorUtilsTestCase(String JavaDoc name) {
43         super(name);
44     }
45
46
47     // -------------------------------------------------- Overall Test Methods
48

49
50     /**
51      * Set up instance variables required by this test case.
52      */

53     public void setUp() throws Exception JavaDoc {
54         super.setUp();
55     }
56
57
58     /**
59      * Return the tests included in this test suite.
60      */

61     public static Test suite() {
62         return (new TestSuite(ConstructorUtilsTestCase.class));
63     }
64
65     /**
66      * Tear down instance variables required by this test case.
67      */

68     public void tearDown() throws Exception JavaDoc {
69         super.tearDown();
70     }
71
72
73     // ------------------------------------------------ Individual Test Methods
74

75     public void testInvokeConstructor() throws Exception JavaDoc {
76         {
77             Object JavaDoc obj = ConstructorUtils.invokeConstructor(TestBean.class,"TEST");
78             assertNotNull(obj);
79             assertTrue(obj instanceof TestBean);
80             assertEquals("TEST",((TestBean)obj).getStringProperty());
81         }
82         {
83             Object JavaDoc obj = ConstructorUtils.invokeConstructor(TestBean.class,new Float JavaDoc(17.3f));
84             assertNotNull(obj);
85             assertTrue(obj instanceof TestBean);
86             assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
87         }
88     }
89
90     public void testInvokeConstructorWithArgArray() throws Exception JavaDoc {
91         Object JavaDoc[] args = { new Float JavaDoc(17.3f), "TEST" };
92         Object JavaDoc obj = ConstructorUtils.invokeConstructor(TestBean.class,args);
93         assertNotNull(obj);
94         assertTrue(obj instanceof TestBean);
95         assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
96         assertEquals("TEST",((TestBean)obj).getStringProperty());
97     }
98
99     public void testInvokeConstructorWithTypeArray() throws Exception JavaDoc {
100         {
101             Object JavaDoc[] args = { Boolean.TRUE, "TEST" };
102             Class JavaDoc[] types = { Boolean.TYPE, String JavaDoc.class };
103             Object JavaDoc obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
104             assertNotNull(obj);
105             assertTrue(obj instanceof TestBean);
106             assertEquals(true,((TestBean)obj).getBooleanProperty());
107             assertEquals("TEST",((TestBean)obj).getStringProperty());
108         }
109         {
110             Object JavaDoc[] args = { Boolean.TRUE, "TEST" };
111             Class JavaDoc[] types = { Boolean JavaDoc.class, String JavaDoc.class };
112             Object JavaDoc obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
113             assertNotNull(obj);
114             assertTrue(obj instanceof TestBean);
115             assertEquals(true,((TestBean)obj).isBooleanSecond());
116             assertEquals("TEST",((TestBean)obj).getStringProperty());
117         }
118     }
119
120     public void testInvokeExactConstructor() throws Exception JavaDoc {
121         {
122             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,"TEST");
123             assertNotNull(obj);
124             assertTrue(obj instanceof TestBean);
125             assertEquals("TEST",((TestBean)obj).getStringProperty());
126         }
127         {
128             try {
129                 Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,new Float JavaDoc(17.3f));
130                 fail("Expected NoSuchMethodException");
131             } catch(NoSuchMethodException JavaDoc e) {
132                 // expected
133
}
134         }
135         {
136             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,Boolean.TRUE);
137             assertNotNull(obj);
138             assertTrue(obj instanceof TestBean);
139             assertEquals(true,((TestBean)obj).isBooleanSecond());
140         }
141     }
142
143     public void testInvokeExactConstructorWithArgArray() throws Exception JavaDoc {
144         {
145             Object JavaDoc[] args = { new Float JavaDoc(17.3f), "TEST" };
146             try {
147                 Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
148                 fail("Expected NoSuchMethodException");
149             } catch(NoSuchMethodException JavaDoc e) {
150                 // expected
151
}
152         }
153         {
154             Object JavaDoc[] args = { Boolean.TRUE, "TEST" };
155             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
156             assertNotNull(obj);
157             assertTrue(obj instanceof TestBean);
158             assertEquals(true,((TestBean)obj).isBooleanSecond());
159             assertEquals("TEST",((TestBean)obj).getStringProperty());
160         }
161     }
162     
163     public void testInvokeExactConstructorWithTypeArray() throws Exception JavaDoc {
164         {
165             Object JavaDoc[] args = { Boolean.TRUE, "TEST" };
166             Class JavaDoc[] types = { Boolean.TYPE, String JavaDoc.class };
167             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
168             assertNotNull(obj);
169             assertTrue(obj instanceof TestBean);
170             assertEquals(true,((TestBean)obj).getBooleanProperty());
171             assertEquals("TEST",((TestBean)obj).getStringProperty());
172         }
173         {
174             Object JavaDoc[] args = { Boolean.TRUE, "TEST" };
175             Class JavaDoc[] types = { Boolean JavaDoc.class, String JavaDoc.class };
176             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
177             assertNotNull(obj);
178             assertTrue(obj instanceof TestBean);
179             assertEquals(true,((TestBean)obj).isBooleanSecond());
180             assertEquals("TEST",((TestBean)obj).getStringProperty());
181         }
182         {
183             Object JavaDoc[] args = { new Float JavaDoc(17.3f), "TEST" };
184             Class JavaDoc[] types = { Float.TYPE, String JavaDoc.class };
185             Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
186             assertNotNull(obj);
187             assertTrue(obj instanceof TestBean);
188             assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
189             assertEquals("TEST",((TestBean)obj).getStringProperty());
190         }
191         {
192             Object JavaDoc[] args = { new Float JavaDoc(17.3f), "TEST" };
193             Class JavaDoc[] types = { Float JavaDoc.class, String JavaDoc.class };
194             try {
195                 Object JavaDoc obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
196                 fail("Expected NoSuchMethodException");
197             } catch(NoSuchMethodException JavaDoc e) {
198                 // expected
199
}
200         }
201     }
202
203     public void testGetAccessibleConstructor() throws Exception JavaDoc {
204         {
205             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,String JavaDoc.class);
206             assertNotNull(ctor);
207             assertTrue(Modifier.isPublic(ctor.getModifiers()));
208         }
209         {
210             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer JavaDoc.class);
211             assertNotNull(ctor);
212             assertTrue(Modifier.isPublic(ctor.getModifiers()));
213         }
214         {
215             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.TYPE);
216             assertNull(ctor);
217         }
218     }
219
220     public void testGetAccessibleConstructorWithTypeArray() throws Exception JavaDoc {
221         {
222             Class JavaDoc[] types = { Boolean.TYPE, String JavaDoc.class };
223             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
224             assertNotNull(ctor);
225             assertTrue(Modifier.isPublic(ctor.getModifiers()));
226         }
227         {
228             Class JavaDoc[] types = { Boolean.TYPE, Boolean.TYPE, String JavaDoc.class };
229             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
230             assertNull(ctor);
231         }
232     }
233
234     public void testGetAccessibleConstructorWithConstructorArg() throws Exception JavaDoc {
235         {
236             Class JavaDoc[] types = { Integer JavaDoc.class };
237             Constructor JavaDoc c1 = TestBean.class.getConstructor(types);
238             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(c1);
239             assertNotNull(ctor);
240             assertTrue(Modifier.isPublic(ctor.getModifiers()));
241         }
242         {
243             Class JavaDoc[] types = { Integer JavaDoc.class };
244             Constructor JavaDoc c1 = TestBean.class.getDeclaredConstructor(types);
245             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(c1);
246             assertNotNull(ctor);
247             assertTrue(Modifier.isPublic(ctor.getModifiers()));
248         }
249         {
250             Class JavaDoc[] types = { Integer.TYPE };
251             Constructor JavaDoc c1 = TestBean.class.getDeclaredConstructor(types);
252             Constructor JavaDoc ctor = ConstructorUtils.getAccessibleConstructor(c1);
253             assertNull(ctor);
254         }
255     }
256
257 }
258
Popular Tags