    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   */
    34  public class ConstructorUtilsTestCase extends TestCase {
    35
    36      // ---------------------------------------------------------- Constructors
    37
    38      /***
    39       * Construct a new instance of this test case.
    40       *
    41       * @param name Name of the test case
    42       */
    43      public ConstructorUtilsTestCase(String name) {
    44          super(name);
    45      }
    46
    47
    48      // -------------------------------------------------- Overall Test Methods
    49
    50
    51      /***
    52       * Set up instance variables required by this test case.
    53       */
    54      public void setUp() throws Exception {
    55          super.setUp();
    56      }
    57
    58
    59      /***
    60       * Return the tests included in this test suite.
    61       */
    62      public static Test suite() {
    63          return (new TestSuite(ConstructorUtilsTestCase.class));
    64      }
    65
    66      /***
    67       * Tear down instance variables required by this test case.
    68       */
    69      public void tearDown() throws Exception {
    70          super.tearDown();
    71      }
    72
    73
    74      // ------------------------------------------------ Individual Test Methods
    75
    76      public void testInvokeConstructor() throws Exception {
    77          {
    78              Object obj = ConstructorUtils.invokeConstructor(TestBean.class,"TEST");
    79              assertNotNull(obj);
    80              assertTrue(obj instanceof TestBean);
    81              assertEquals("TEST",((TestBean)obj).getStringProperty());
    82          }
    83          {
    84              Object obj = ConstructorUtils.invokeConstructor(TestBean.class,new Float(17.3f));
    85              assertNotNull(obj);
    86              assertTrue(obj instanceof TestBean);
    87              assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
    88          }
    89      }
    90
    91      public void testInvokeConstructorWithArgArray() throws Exception {
    92          Object[] args = { new Float(17.3f), "TEST" };
    93          Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args);
    94          assertNotNull(obj);
    95          assertTrue(obj instanceof TestBean);
    96          assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
    97          assertEquals("TEST",((TestBean)obj).getStringProperty());
    98      }
    99
    100     public void testInvokeConstructorWithTypeArray() throws Exception {
    101         {
    102             Object[] args = { Boolean.TRUE, "TEST" };
    103             Class[] types = { Boolean.TYPE, String.class };
    104             Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
    105             assertNotNull(obj);
    106             assertTrue(obj instanceof TestBean);
    107             assertEquals(true,((TestBean)obj).getBooleanProperty());
    108             assertEquals("TEST",((TestBean)obj).getStringProperty());
    109         }
    110         {
    111             Object[] args = { Boolean.TRUE, "TEST" };
    112             Class[] types = { Boolean.class, String.class };
    113             Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types);
    114             assertNotNull(obj);
    115             assertTrue(obj instanceof TestBean);
    116             assertEquals(true,((TestBean)obj).isBooleanSecond());
    117             assertEquals("TEST",((TestBean)obj).getStringProperty());
    118         }
    119     }
    120
    121     public void testInvokeExactConstructor() throws Exception {
    122         {
    123             Object obj = ConstructorUtils.invokeExactConstructor(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(TestBean.class,new Float(17.3f));
    131                 fail("Expected NoSuchMethodException");
    132             } catch(NoSuchMethodException e) {
    133                 // expected
    134             }
    135         }
    136         {
    137             Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,Boolean.TRUE);
    138             assertNotNull(obj);
    139             assertTrue(obj instanceof TestBean);
    140             assertEquals(true,((TestBean)obj).isBooleanSecond());
    141         }
    142     }
    143
    144     public void testInvokeExactConstructorWithArgArray() throws Exception {
    145         {
    146             Object[] args = { new Float(17.3f), "TEST" };
    147             try {
    148                 Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
    149                 fail("Expected NoSuchMethodException");
    150             } catch(NoSuchMethodException e) {
    151                 // expected
    152             }
    153         }
    154         {
    155             Object[] args = { Boolean.TRUE, "TEST" };
    156             Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args);
    157             assertNotNull(obj);
    158             assertTrue(obj instanceof TestBean);
    159             assertEquals(true,((TestBean)obj).isBooleanSecond());
    160             assertEquals("TEST",((TestBean)obj).getStringProperty());
    161         }
    162     }
    163
    164     public void testInvokeExactConstructorWithTypeArray() throws Exception {
    165         {
    166             Object[] args = { Boolean.TRUE, "TEST" };
    167             Class[] types = { Boolean.TYPE, String.class };
    168             Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
    169             assertNotNull(obj);
    170             assertTrue(obj instanceof TestBean);
    171             assertEquals(true,((TestBean)obj).getBooleanProperty());
    172             assertEquals("TEST",((TestBean)obj).getStringProperty());
    173         }
    174         {
    175             Object[] args = { Boolean.TRUE, "TEST" };
    176             Class[] types = { Boolean.class, String.class };
    177             Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
    178             assertNotNull(obj);
    179             assertTrue(obj instanceof TestBean);
    180             assertEquals(true,((TestBean)obj).isBooleanSecond());
    181             assertEquals("TEST",((TestBean)obj).getStringProperty());
    182         }
    183         {
    184             Object[] args = { new Float(17.3f), "TEST" };
    185             Class[] types = { Float.TYPE, String.class };
    186             Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
    187             assertNotNull(obj);
    188             assertTrue(obj instanceof TestBean);
    189             assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f);
    190             assertEquals("TEST",((TestBean)obj).getStringProperty());
    191         }
    192         {
    193             Object[] args = { new Float(17.3f), "TEST" };
    194             Class[] types = { Float.class, String.class };
    195             try {
    196                 Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types);
    197                 fail("Expected NoSuchMethodException");
    198             } catch(NoSuchMethodException e) {
    199                 // expected
    200             }
    201         }
    202     }
    203
    204     public void testGetAccessibleConstructor() throws Exception {
    205         {
    206             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,String.class);
    207             assertNotNull(ctor);
    208             assertTrue(Modifier.isPublic(ctor.getModifiers()));
    209         }
    210         {
    211             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.class);
    212             assertNotNull(ctor);
    213             assertTrue(Modifier.isPublic(ctor.getModifiers()));
    214         }
    215         {
    216             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.TYPE);
    217             assertNull(ctor);
    218         }
    219     }
    220
    221     public void testGetAccessibleConstructorWithTypeArray() throws Exception {
    222         {
    223             Class[] types = { Boolean.TYPE, String.class };
    224             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
    225             assertNotNull(ctor);
    226             assertTrue(Modifier.isPublic(ctor.getModifiers()));
    227         }
    228         {
    229             Class[] types = { Boolean.TYPE, Boolean.TYPE, String.class };
    230             Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types);
    231             assertNull(ctor);
    232         }
    233     }
    234
    235     public void testGetAccessibleConstructorWithConstructorArg() throws Exception {
    236         {
    237             Class[] types = { Integer.class };
    238             Constructor c1 = TestBean.class.getConstructor(types);
    239             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);
    240             assertNotNull(ctor);
    241             assertTrue(Modifier.isPublic(ctor.getModifiers()));
    242         }
    243         {
    244             Class[] types = { Integer.class };
    245             Constructor c1 = TestBean.class.getDeclaredConstructor(types);
    246             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);
    247             assertNotNull(ctor);
    248             assertTrue(Modifier.isPublic(ctor.getModifiers()));
    249         }
    250         {
    251             Class[] types = { Integer.TYPE };
    252             Constructor c1 = TestBean.class.getDeclaredConstructor(types);
    253             Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1);
    254             assertNull(ctor);
    255         }
    256     }
    257
    258 }