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;
22  import java.lang.reflect.Modifier;
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 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 {
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 {
69          super.tearDown();
70      }
71  
72  
73      // ------------------------------------------------ Individual Test Methods
74  
75      public void testInvokeConstructor() throws Exception {
76          {
77              Object obj = ConstructorUtils.invokeConstructor(TestBean.class,"TEST");
78              assertNotNull(obj);
79              assertTrue(obj instanceof TestBean);
80              assertEquals("TEST",((TestBean)obj).getStringProperty());
81          }
82          {
83              Object obj = ConstructorUtils.invokeConstructor(TestBean.class,new Float(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 {
91          Object[] args = { new Float(17.3f), "TEST" };
92          Object 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 {
100         {
101             Object[] args = { Boolean.TRUE, "TEST" };
102             Class[] types = { Boolean.TYPE, String.class };
103             Object 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[] args = { Boolean.TRUE, "TEST" };
111             Class[] types = { Boolean.class, String.class };
112             Object 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 {
121         {
122             Object 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 obj = ConstructorUtils.invokeExactConstructor(TestBean.class,new Float(17.3f));
130                 fail("Expected NoSuchMethodException");
131             } catch(NoSuchMethodException e) {
132                 // expected
133             }
134         }
135         {
136             Object 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 {
144         {
145             Object[] args = { new Float(17.3f), "TEST" };
146             try {
147                 Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
148                 fail("Expected NoSuchMethodException");
149             } catch(NoSuchMethodException e) {
150                 // expected
151             }
152         }
153         {
154             Object[] args = { Boolean.TRUE, "TEST" };
155             Object 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 {
164         {
165             Object[] args = { Boolean.TRUE, "TEST" };
166             Class[] types = { Boolean.TYPE, String.class };
167             Object 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[] args = { Boolean.TRUE, "TEST" };
175             Class[] types = { Boolean.class, String.class };
176             Object 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[] args = { new Float(17.3f), "TEST" };
184             Class[] types = { Float.TYPE, String.class };
185             Object 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[] args = { new Float(17.3f), "TEST" };
193             Class[] types = { Float.class, String.class };
194             try {
195                 Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
196                 fail("Expected NoSuchMethodException");
197             } catch(NoSuchMethodException e) {
198                 // expected
199             }
200         }
201     }
202 
203     public void testGetAccessibleConstructor() throws Exception {
204         {
205             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,String.class);       
206             assertNotNull(ctor);
207             assertTrue(Modifier.isPublic(ctor.getModifiers()));
208         }
209         {
210             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.class);       
211             assertNotNull(ctor);
212             assertTrue(Modifier.isPublic(ctor.getModifiers()));
213         }
214         {
215             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.TYPE);       
216             assertNull(ctor);
217         }
218     }
219 
220     public void testGetAccessibleConstructorWithTypeArray() throws Exception {
221         {
222             Class[] types = { Boolean.TYPE, String.class };
223             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);       
224             assertNotNull(ctor);
225             assertTrue(Modifier.isPublic(ctor.getModifiers()));
226         }
227         {
228             Class[] types = { Boolean.TYPE, Boolean.TYPE, String.class };
229             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);       
230             assertNull(ctor);
231         }
232     }
233 
234     public void testGetAccessibleConstructorWithConstructorArg() throws Exception {
235         {
236             Class[] types = { Integer.class };
237             Constructor c1 = TestBean.class.getConstructor(types);
238             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);       
239             assertNotNull(ctor);
240             assertTrue(Modifier.isPublic(ctor.getModifiers()));
241         }
242         {
243             Class[] types = { Integer.class };
244             Constructor c1 = TestBean.class.getDeclaredConstructor(types);
245             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);       
246             assertNotNull(ctor);
247             assertTrue(Modifier.isPublic(ctor.getModifiers()));
248         }
249         {
250             Class[] types = { Integer.TYPE };
251             Constructor c1 = TestBean.class.getDeclaredConstructor(types);
252             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);       
253             assertNull(ctor);
254         }
255     }
256 
257 }