alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Beanutils example source code file (ConstructorUtilsTestCase.java)

This example Commons Beanutils source code file (ConstructorUtilsTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Beanutils tags/keywords

class, constructor, constructor, constructorutilstestcase, exception, expected, nosuchmethodexception, object, object, reflection, test, test, testbean, testbean, testcase

The Commons Beanutils ConstructorUtilsTestCase.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 


package org.apache.commons.beanutils;


import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


/**
 * <p> Test case for ConstructorUtils 

* */ public class ConstructorUtilsTestCase extends TestCase { // ---------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public ConstructorUtilsTestCase(String name) { super(name); } // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() throws Exception { super.setUp(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(ConstructorUtilsTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() throws Exception { super.tearDown(); } // ------------------------------------------------ Individual Test Methods public void testInvokeConstructor() throws Exception { { Object obj = ConstructorUtils.invokeConstructor(TestBean.class,"TEST"); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { Object obj = ConstructorUtils.invokeConstructor(TestBean.class,new Float(17.3f)); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f); } } public void testInvokeConstructorWithArgArray() throws Exception { Object[] args = { new Float(17.3f), "TEST" }; Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f); assertEquals("TEST",((TestBean)obj).getStringProperty()); } public void testInvokeConstructorWithTypeArray() throws Exception { { Object[] args = { Boolean.TRUE, "TEST" }; Class[] types = { Boolean.TYPE, String.class }; Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).getBooleanProperty()); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { Object[] args = { Boolean.TRUE, "TEST" }; Class[] types = { Boolean.class, String.class }; Object obj = ConstructorUtils.invokeConstructor(TestBean.class,args,types); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).isBooleanSecond()); assertEquals("TEST",((TestBean)obj).getStringProperty()); } } public void testInvokeExactConstructor() throws Exception { { Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,"TEST"); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { try { Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,new Float(17.3f)); fail("Expected NoSuchMethodException"); } catch(NoSuchMethodException e) { // expected } } { Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,Boolean.TRUE); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).isBooleanSecond()); } } public void testInvokeExactConstructorWithArgArray() throws Exception { { Object[] args = { new Float(17.3f), "TEST" }; try { Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args); fail("Expected NoSuchMethodException"); } catch(NoSuchMethodException e) { // expected } } { Object[] args = { Boolean.TRUE, "TEST" }; Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).isBooleanSecond()); assertEquals("TEST",((TestBean)obj).getStringProperty()); } } public void testInvokeExactConstructorWithTypeArray() throws Exception { { Object[] args = { Boolean.TRUE, "TEST" }; Class[] types = { Boolean.TYPE, String.class }; Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).getBooleanProperty()); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { Object[] args = { Boolean.TRUE, "TEST" }; Class[] types = { Boolean.class, String.class }; Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(true,((TestBean)obj).isBooleanSecond()); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { Object[] args = { new Float(17.3f), "TEST" }; Class[] types = { Float.TYPE, String.class }; Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types); assertNotNull(obj); assertTrue(obj instanceof TestBean); assertEquals(17.3f,((TestBean)obj).getFloatProperty(),0.0f); assertEquals("TEST",((TestBean)obj).getStringProperty()); } { Object[] args = { new Float(17.3f), "TEST" }; Class[] types = { Float.class, String.class }; try { Object obj = ConstructorUtils.invokeExactConstructor(TestBean.class,args,types); fail("Expected NoSuchMethodException"); } catch(NoSuchMethodException e) { // expected } } } public void testGetAccessibleConstructor() throws Exception { { Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,String.class); assertNotNull(ctor); assertTrue(Modifier.isPublic(ctor.getModifiers())); } { Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.class); assertNotNull(ctor); assertTrue(Modifier.isPublic(ctor.getModifiers())); } { Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,Integer.TYPE); assertNull(ctor); } } public void testGetAccessibleConstructorWithTypeArray() throws Exception { { Class[] types = { Boolean.TYPE, String.class }; Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types); assertNotNull(ctor); assertTrue(Modifier.isPublic(ctor.getModifiers())); } { Class[] types = { Boolean.TYPE, Boolean.TYPE, String.class }; Constructor ctor = ConstructorUtils.getAccessibleConstructor(TestBean.class,types); assertNull(ctor); } } public void testGetAccessibleConstructorWithConstructorArg() throws Exception { { Class[] types = { Integer.class }; Constructor c1 = TestBean.class.getConstructor(types); Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1); assertNotNull(ctor); assertTrue(Modifier.isPublic(ctor.getModifiers())); } { Class[] types = { Integer.class }; Constructor c1 = TestBean.class.getDeclaredConstructor(types); Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1); assertNotNull(ctor); assertTrue(Modifier.isPublic(ctor.getModifiers())); } { Class[] types = { Integer.TYPE }; Constructor c1 = TestBean.class.getDeclaredConstructor(types); Constructor ctor = ConstructorUtils.getAccessibleConstructor(c1); assertNull(ctor); } } }

Other Commons Beanutils examples (source code examples)

Here is a short list of links related to this Commons Beanutils ConstructorUtilsTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.