View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.commons.beanutils;
20  
21  
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.Modifier;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  
30  /**
31   * <p> Test case for <code>ConstructorUtils</code> </p>
32   *
33   * @version $Id$
34   */
35  public class ConstructorUtilsTestCase extends TestCase {
36  
37      // ---------------------------------------------------------- Constructors
38  
39      /**
40       * Construct a new instance of this test case.
41       *
42       * @param name Name of the test case
43       */
44      public ConstructorUtilsTestCase(final String name) {
45          super(name);
46      }
47  
48  
49      // -------------------------------------------------- Overall Test Methods
50  
51  
52      /**
53       * Set up instance variables required by this test case.
54       */
55      @Override
56      public void setUp() throws Exception {
57          super.setUp();
58      }
59  
60  
61      /**
62       * Return the tests included in this test suite.
63       */
64      public static Test suite() {
65          return (new TestSuite(ConstructorUtilsTestCase.class));
66      }
67  
68      /**
69       * Tear down instance variables required by this test case.
70       */
71      @Override
72      public void tearDown() throws Exception {
73          super.tearDown();
74      }
75  
76  
77      // ------------------------------------------------ Individual Test Methods
78  
79      public void testInvokeConstructor() throws Exception {
80          {
81              final Object obj = ConstructorUtils.invokeConstructor(TestBean.class,"TEST");
82              assertNotNull(obj);
83              assertTrue(obj instanceof TestBean);
84              assertEquals("TEST",((TestBean)obj).getStringProperty());
85          }
86          {
87              final Object obj = ConstructorUtils.invokeConstructor(TestBean.class,new Float(17.3f));
88              assertNotNull(obj);
89              assertTrue(obj instanceof TestBean);
90              assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
91          }
92      }
93  
94      public void testInvokeConstructorNull() throws Exception {
95          final Object obj = ConstructorUtils.invokeConstructor(TestBean.class, (Object) null);
96          assertNotNull(obj);
97          assertTrue(obj instanceof TestBean);
98      }
99  
100     public void testInvokeConstructorWithArgArray() throws Exception {
101         final Object[] args = { new Float(17.3f), "TEST" };
102         final Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args);
103         assertNotNull(obj);
104         assertTrue(obj instanceof TestBean);
105         assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
106         assertEquals("TEST",((TestBean)obj).getStringProperty());
107     }
108 
109     public void testInvokeConstructorWithTypeArray() throws Exception {
110         {
111             final Object[] args = { Boolean.TRUE, "TEST" };
112             final Class<?>[] types = { Boolean.TYPE, String.class };
113             final Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
114             assertNotNull(obj);
115             assertTrue(obj instanceof TestBean);
116             assertEquals(true,((TestBean)obj).getBooleanProperty());
117             assertEquals("TEST",((TestBean)obj).getStringProperty());
118         }
119         {
120             final Object[] args = { Boolean.TRUE, "TEST" };
121             final Class<?>[] types = { Boolean.class, String.class };
122             final Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
123             assertNotNull(obj);
124             assertTrue(obj instanceof TestBean);
125             assertEquals(true,((TestBean)obj).isBooleanSecond());
126             assertEquals("TEST",((TestBean)obj).getStringProperty());
127         }
128     }
129 
130     public void testInvokeExactConstructor() throws Exception {
131         {
132             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,"TEST");
133             assertNotNull(obj);
134             assertTrue(obj instanceof TestBean);
135             assertEquals("TEST",((TestBean)obj).getStringProperty());
136         }
137         {
138             try {
139                 ConstructorUtils.invokeExactConstructor(TestBean.class,new Float(17.3f));
140                 fail("Expected NoSuchMethodException");
141             } catch(final NoSuchMethodException e) {
142                 // expected
143             }
144         }
145         {
146             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,Boolean.TRUE);
147             assertNotNull(obj);
148             assertTrue(obj instanceof TestBean);
149             assertEquals(true,((TestBean)obj).isBooleanSecond());
150         }
151     }
152 
153     public void testInvokeExactConstructorWithNull() throws Exception {
154         final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class, (Object) null);
155         assertNotNull(obj);
156         assertTrue(obj instanceof TestBean);
157     }
158 
159     public void testInvokeExactConstructorWithArgArray() throws Exception {
160         {
161             final Object[] args = { new Float(17.3f), "TEST" };
162             try {
163                 ConstructorUtils.invokeExactConstructor(TestBean.class,args);
164                 fail("Expected NoSuchMethodException");
165             } catch(final NoSuchMethodException e) {
166                 // expected
167             }
168         }
169         {
170             final Object[] args = { Boolean.TRUE, "TEST" };
171             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
172             assertNotNull(obj);
173             assertTrue(obj instanceof TestBean);
174             assertEquals(true,((TestBean)obj).isBooleanSecond());
175             assertEquals("TEST",((TestBean)obj).getStringProperty());
176         }
177     }
178 
179     public void testInvokeExactConstructorWithTypeArray() throws Exception {
180         {
181             final Object[] args = { Boolean.TRUE, "TEST" };
182             final Class<?>[] types = { Boolean.TYPE, String.class };
183             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
184             assertNotNull(obj);
185             assertTrue(obj instanceof TestBean);
186             assertEquals(true,((TestBean)obj).getBooleanProperty());
187             assertEquals("TEST",((TestBean)obj).getStringProperty());
188         }
189         {
190             final Object[] args = { Boolean.TRUE, "TEST" };
191             final Class<?>[] types = { Boolean.class, String.class };
192             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
193             assertNotNull(obj);
194             assertTrue(obj instanceof TestBean);
195             assertEquals(true,((TestBean)obj).isBooleanSecond());
196             assertEquals("TEST",((TestBean)obj).getStringProperty());
197         }
198         {
199             final Object[] args = { new Float(17.3f), "TEST" };
200             final Class<?>[] types = { Float.TYPE, String.class };
201             final Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
202             assertNotNull(obj);
203             assertTrue(obj instanceof TestBean);
204             assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
205             assertEquals("TEST",((TestBean)obj).getStringProperty());
206         }
207         {
208             final Object[] args = { new Float(17.3f), "TEST" };
209             final Class<?>[] types = { Float.class, String.class };
210             try {
211                 ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
212                 fail("Expected NoSuchMethodException");
213             } catch(final NoSuchMethodException e) {
214                 // expected
215             }
216         }
217     }
218 
219     public void testGetAccessibleConstructor() throws Exception {
220         {
221             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,String.class);
222             assertNotNull(ctor);
223             assertTrue(Modifier.isPublic(ctor.getModifiers()));
224         }
225         {
226             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.class);
227             assertNotNull(ctor);
228             assertTrue(Modifier.isPublic(ctor.getModifiers()));
229         }
230         {
231             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.TYPE);
232             assertNull(ctor);
233         }
234     }
235 
236     public void testGetAccessibleConstructorWithTypeArray() throws Exception {
237         {
238             final Class<?>[] types = { Boolean.TYPE, String.class };
239             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
240             assertNotNull(ctor);
241             assertTrue(Modifier.isPublic(ctor.getModifiers()));
242         }
243         {
244             final Class<?>[] types = { Boolean.TYPE, Boolean.TYPE, String.class };
245             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
246             assertNull(ctor);
247         }
248     }
249 
250     public void testGetAccessibleConstructorWithConstructorArg() throws Exception {
251         {
252             final Class<?>[] types = { Integer.class };
253             final Constructor<?> c1 = TestBean.class.getConstructor(types);
254             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(c1);
255             assertNotNull(ctor);
256             assertTrue(Modifier.isPublic(ctor.getModifiers()));
257         }
258         {
259             final Class<?>[] types = { Integer.class };
260             final Constructor<?> c1 = TestBean.class.getDeclaredConstructor(types);
261             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(c1);
262             assertNotNull(ctor);
263             assertTrue(Modifier.isPublic(ctor.getModifiers()));
264         }
265         {
266             final Class<?>[] types = { Integer.TYPE };
267             final Constructor<?> c1 = TestBean.class.getDeclaredConstructor(types);
268             final Constructor<?> ctor = ConstructorUtils.getAccessibleConstructor(c1);
269             assertNull(ctor);
270         }
271     }
272 
273 }