001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.lang.reflect.Constructor;
021: import java.lang.reflect.Modifier;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: * <p> Test case for <code>ConstructorUtils</code> </p>
029: *
030: */
031: public class ConstructorUtilsTestCase extends TestCase {
032:
033: // ---------------------------------------------------------- Constructors
034:
035: /**
036: * Construct a new instance of this test case.
037: *
038: * @param name Name of the test case
039: */
040: public ConstructorUtilsTestCase(String name) {
041: super (name);
042: }
043:
044: // -------------------------------------------------- Overall Test Methods
045:
046: /**
047: * Set up instance variables required by this test case.
048: */
049: public void setUp() throws Exception {
050: super .setUp();
051: }
052:
053: /**
054: * Return the tests included in this test suite.
055: */
056: public static Test suite() {
057: return (new TestSuite(ConstructorUtilsTestCase.class));
058: }
059:
060: /**
061: * Tear down instance variables required by this test case.
062: */
063: public void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: // ------------------------------------------------ Individual Test Methods
068:
069: public void testInvokeConstructor() throws Exception {
070: {
071: Object obj = ConstructorUtils.invokeConstructor(
072: TestBean.class, "TEST");
073: assertNotNull(obj);
074: assertTrue(obj instanceof TestBean);
075: assertEquals("TEST", ((TestBean) obj).getStringProperty());
076: }
077: {
078: Object obj = ConstructorUtils.invokeConstructor(
079: TestBean.class, new Float(17.3f));
080: assertNotNull(obj);
081: assertTrue(obj instanceof TestBean);
082: assertEquals(17.3f, ((TestBean) obj).getFloatProperty(),
083: 0.0f);
084: }
085: }
086:
087: public void testInvokeConstructorWithArgArray() throws Exception {
088: Object[] args = { new Float(17.3f), "TEST" };
089: Object obj = ConstructorUtils.invokeConstructor(TestBean.class,
090: args);
091: assertNotNull(obj);
092: assertTrue(obj instanceof TestBean);
093: assertEquals(17.3f, ((TestBean) obj).getFloatProperty(), 0.0f);
094: assertEquals("TEST", ((TestBean) obj).getStringProperty());
095: }
096:
097: public void testInvokeConstructorWithTypeArray() throws Exception {
098: {
099: Object[] args = { Boolean.TRUE, "TEST" };
100: Class[] types = { Boolean.TYPE, String.class };
101: Object obj = ConstructorUtils.invokeConstructor(
102: TestBean.class, args, types);
103: assertNotNull(obj);
104: assertTrue(obj instanceof TestBean);
105: assertEquals(true, ((TestBean) obj).getBooleanProperty());
106: assertEquals("TEST", ((TestBean) obj).getStringProperty());
107: }
108: {
109: Object[] args = { Boolean.TRUE, "TEST" };
110: Class[] types = { Boolean.class, String.class };
111: Object obj = ConstructorUtils.invokeConstructor(
112: 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(
123: TestBean.class, "TEST");
124: assertNotNull(obj);
125: assertTrue(obj instanceof TestBean);
126: assertEquals("TEST", ((TestBean) obj).getStringProperty());
127: }
128: {
129: try {
130: Object obj = ConstructorUtils.invokeExactConstructor(
131: TestBean.class, new Float(17.3f));
132: fail("Expected NoSuchMethodException");
133: } catch (NoSuchMethodException e) {
134: // expected
135: }
136: }
137: {
138: Object obj = ConstructorUtils.invokeExactConstructor(
139: TestBean.class, Boolean.TRUE);
140: assertNotNull(obj);
141: assertTrue(obj instanceof TestBean);
142: assertEquals(true, ((TestBean) obj).isBooleanSecond());
143: }
144: }
145:
146: public void testInvokeExactConstructorWithArgArray()
147: throws Exception {
148: {
149: Object[] args = { new Float(17.3f), "TEST" };
150: try {
151: Object obj = ConstructorUtils.invokeExactConstructor(
152: TestBean.class, args);
153: fail("Expected NoSuchMethodException");
154: } catch (NoSuchMethodException e) {
155: // expected
156: }
157: }
158: {
159: Object[] args = { Boolean.TRUE, "TEST" };
160: Object obj = ConstructorUtils.invokeExactConstructor(
161: TestBean.class, args);
162: assertNotNull(obj);
163: assertTrue(obj instanceof TestBean);
164: assertEquals(true, ((TestBean) obj).isBooleanSecond());
165: assertEquals("TEST", ((TestBean) obj).getStringProperty());
166: }
167: }
168:
169: public void testInvokeExactConstructorWithTypeArray()
170: throws Exception {
171: {
172: Object[] args = { Boolean.TRUE, "TEST" };
173: Class[] types = { Boolean.TYPE, String.class };
174: Object obj = ConstructorUtils.invokeExactConstructor(
175: TestBean.class, args, types);
176: assertNotNull(obj);
177: assertTrue(obj instanceof TestBean);
178: assertEquals(true, ((TestBean) obj).getBooleanProperty());
179: assertEquals("TEST", ((TestBean) obj).getStringProperty());
180: }
181: {
182: Object[] args = { Boolean.TRUE, "TEST" };
183: Class[] types = { Boolean.class, String.class };
184: Object obj = ConstructorUtils.invokeExactConstructor(
185: TestBean.class, args, types);
186: assertNotNull(obj);
187: assertTrue(obj instanceof TestBean);
188: assertEquals(true, ((TestBean) obj).isBooleanSecond());
189: assertEquals("TEST", ((TestBean) obj).getStringProperty());
190: }
191: {
192: Object[] args = { new Float(17.3f), "TEST" };
193: Class[] types = { Float.TYPE, String.class };
194: Object obj = ConstructorUtils.invokeExactConstructor(
195: TestBean.class, args, types);
196: assertNotNull(obj);
197: assertTrue(obj instanceof TestBean);
198: assertEquals(17.3f, ((TestBean) obj).getFloatProperty(),
199: 0.0f);
200: assertEquals("TEST", ((TestBean) obj).getStringProperty());
201: }
202: {
203: Object[] args = { new Float(17.3f), "TEST" };
204: Class[] types = { Float.class, String.class };
205: try {
206: Object obj = ConstructorUtils.invokeExactConstructor(
207: TestBean.class, args, types);
208: fail("Expected NoSuchMethodException");
209: } catch (NoSuchMethodException e) {
210: // expected
211: }
212: }
213: }
214:
215: public void testGetAccessibleConstructor() throws Exception {
216: {
217: Constructor ctor = ConstructorUtils
218: .getAccessibleConstructor(TestBean.class,
219: String.class);
220: assertNotNull(ctor);
221: assertTrue(Modifier.isPublic(ctor.getModifiers()));
222: }
223: {
224: Constructor ctor = ConstructorUtils
225: .getAccessibleConstructor(TestBean.class,
226: Integer.class);
227: assertNotNull(ctor);
228: assertTrue(Modifier.isPublic(ctor.getModifiers()));
229: }
230: {
231: Constructor ctor = ConstructorUtils
232: .getAccessibleConstructor(TestBean.class,
233: Integer.TYPE);
234: assertNull(ctor);
235: }
236: }
237:
238: public void testGetAccessibleConstructorWithTypeArray()
239: throws Exception {
240: {
241: Class[] types = { Boolean.TYPE, String.class };
242: Constructor ctor = ConstructorUtils
243: .getAccessibleConstructor(TestBean.class, types);
244: assertNotNull(ctor);
245: assertTrue(Modifier.isPublic(ctor.getModifiers()));
246: }
247: {
248: Class[] types = { Boolean.TYPE, Boolean.TYPE, String.class };
249: Constructor ctor = ConstructorUtils
250: .getAccessibleConstructor(TestBean.class, types);
251: assertNull(ctor);
252: }
253: }
254:
255: public void testGetAccessibleConstructorWithConstructorArg()
256: throws Exception {
257: {
258: Class[] types = { Integer.class };
259: Constructor c1 = TestBean.class.getConstructor(types);
260: Constructor ctor = ConstructorUtils
261: .getAccessibleConstructor(c1);
262: assertNotNull(ctor);
263: assertTrue(Modifier.isPublic(ctor.getModifiers()));
264: }
265: {
266: Class[] types = { Integer.class };
267: Constructor c1 = TestBean.class
268: .getDeclaredConstructor(types);
269: Constructor ctor = ConstructorUtils
270: .getAccessibleConstructor(c1);
271: assertNotNull(ctor);
272: assertTrue(Modifier.isPublic(ctor.getModifiers()));
273: }
274: {
275: Class[] types = { Integer.TYPE };
276: Constructor c1 = TestBean.class
277: .getDeclaredConstructor(types);
278: Constructor ctor = ConstructorUtils
279: .getAccessibleConstructor(c1);
280: assertNull(ctor);
281: }
282: }
283:
284: }
|