📄 beanutilstestcase.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed 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.InvocationTargetException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;/** * <p> * Test Case for the BeanUtils class. The majority of these tests use * instances of the TestBean class, so be sure to update the tests if you * change the characteristics of that class. * </p> * * <p> * Template for this stolen from Craigs PropertyUtilsTestCase * </p> * * <p> * Note that the tests are dependant upon the static aspects * (such as array sizes...) of the TestBean.java class, so ensure * than all changes to TestBean are reflected here. * </p> * * <p> * So far, this test case has tests for the following methods of the * <code>BeanUtils</code> class: * </p> * <ul> * <li>getArrayProperty(Object bean, String name)</li> * </ul> * * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> * @version $Revision: 1.29 $ */public class BeanUtilsTestCase extends TestCase { // ---------------------------------------------------- Instance Variables /** * The test bean for each test. */ protected TestBean bean = null; /** * The set of properties that should be described. */ protected String describes[] = { "booleanProperty", "booleanSecond", "byteProperty", "doubleProperty", "dupProperty", "floatProperty", "intArray", // "intIndexed", "longProperty", "listIndexed", "longProperty", // "mappedProperty", // "mappedIntProperty", "nested", "nullProperty", "readOnlyProperty", "shortProperty", "stringArray", // "stringIndexed", "stringProperty" }; // ---------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public BeanUtilsTestCase(String name) { super(name); } // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() { bean = new TestBean(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(BeanUtilsTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { bean = null; } // ------------------------------------------------ Individual Test Methods /** * Test the copyProperties() method from a DynaBean. */ public void testCopyPropertiesDynaBean() { // Set up an origin bean with customized properties DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); DynaBean orig = null; try { orig = dynaClass.newInstance(); } catch (Exception e) { fail("newInstance(): " + e); } orig.set("booleanProperty", Boolean.FALSE); orig.set("byteProperty", new Byte((byte) 111)); orig.set("doubleProperty", new Double(333.33)); orig.set("dupProperty", new String[] { "New 0", "New 1", "New 2" }); orig.set("intArray", new int[] { 100, 200, 300 }); orig.set("intProperty", new Integer(333)); orig.set("longProperty", new Long(3333)); orig.set("shortProperty", new Short((short) 33)); orig.set("stringArray", new String[] { "New 0", "New 1" }); orig.set("stringProperty", "Custom string"); // Copy the origin bean to our destination test bean try { BeanUtils.copyProperties(bean, orig); } catch (Exception e) { fail("Threw exception: " + e); } // Validate the results for scalar properties assertEquals("Copied boolean property", false, bean.getBooleanProperty()); assertEquals("Copied byte property", (byte) 111, bean.getByteProperty()); assertEquals("Copied double property", 333.33, bean.getDoubleProperty(), 0.005); assertEquals("Copied int property", 333, bean.getIntProperty()); assertEquals("Copied long property", (long) 3333, bean.getLongProperty()); assertEquals("Copied short property", (short) 33, bean.getShortProperty()); assertEquals("Copied string property", "Custom string", bean.getStringProperty()); // Validate the results for array properties String dupProperty[] = bean.getDupProperty(); assertNotNull("dupProperty present", dupProperty); assertEquals("dupProperty length", 3, dupProperty.length); assertEquals("dupProperty[0]", "New 0", dupProperty[0]); assertEquals("dupProperty[1]", "New 1", dupProperty[1]); assertEquals("dupProperty[2]", "New 2", dupProperty[2]); int intArray[] = bean.getIntArray(); assertNotNull("intArray present", intArray); assertEquals("intArray length", 3, intArray.length); assertEquals("intArray[0]", 100, intArray[0]); assertEquals("intArray[1]", 200, intArray[1]); assertEquals("intArray[2]", 300, intArray[2]); String stringArray[] = bean.getStringArray(); assertNotNull("stringArray present", stringArray); assertEquals("stringArray length", 2, stringArray.length); assertEquals("stringArray[0]", "New 0", stringArray[0]); assertEquals("stringArray[1]", "New 1", stringArray[1]); } /** * Test copyProperties() when the origin is a a <code>Map</code>. */ public void testCopyPropertiesMap() { Map map = new HashMap(); map.put("booleanProperty", "false"); map.put("byteProperty", "111"); map.put("doubleProperty", "333.0"); map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" }); map.put("floatProperty", "222.0"); map.put("intArray", new String[] { "0", "100", "200" }); map.put("intProperty", "111"); map.put("longProperty", "444"); map.put("shortProperty", "555"); map.put("stringProperty", "New String Property"); try { BeanUtils.copyProperties(bean, map); } catch (Throwable t) { fail("Threw " + t.toString()); } // Scalar properties assertEquals("booleanProperty", false, bean.getBooleanProperty()); assertEquals("byteProperty", (byte) 111, bean.getByteProperty()); assertEquals("doubleProperty", 333.0, bean.getDoubleProperty(), 0.005); assertEquals("floatProperty", (float) 222.0, bean.getFloatProperty(), (float) 0.005); assertEquals("longProperty", 111, bean.getIntProperty()); assertEquals("longProperty", (long) 444, bean.getLongProperty()); assertEquals("shortProperty", (short) 555, bean.getShortProperty()); assertEquals("stringProperty", "New String Property", bean.getStringProperty()); // Indexed Properties String dupProperty[] = bean.getDupProperty(); assertNotNull("dupProperty present", dupProperty); assertEquals("dupProperty length", 3, dupProperty.length); assertEquals("dupProperty[0]", "New 0", dupProperty[0]); assertEquals("dupProperty[1]", "New 1", dupProperty[1]); assertEquals("dupProperty[2]", "New 2", dupProperty[2]); int intArray[] = bean.getIntArray(); assertNotNull("intArray present", intArray); assertEquals("intArray length", 3, intArray.length); assertEquals("intArray[0]", 0, intArray[0]); assertEquals("intArray[1]", 100, intArray[1]); assertEquals("intArray[2]", 200, intArray[2]); } /** * Test the copyProperties() method from a standard JavaBean. */ public void testCopyPropertiesStandard() { // Set up an origin bean with customized properties TestBean orig = new TestBean(); orig.setBooleanProperty(false); orig.setByteProperty((byte) 111); orig.setDoubleProperty(333.33); orig.setDupProperty(new String[] { "New 0", "New 1", "New 2" }); orig.setIntArray(new int[] { 100, 200, 300 }); orig.setIntProperty(333); orig.setLongProperty(3333); orig.setShortProperty((short) 33); orig.setStringArray(new String[] { "New 0", "New 1" }); orig.setStringProperty("Custom string"); // Copy the origin bean to our destination test bean try { BeanUtils.copyProperties(bean, orig); } catch (Exception e) { fail("Threw exception: " + e); } // Validate the results for scalar properties assertEquals("Copied boolean property", false, bean.getBooleanProperty()); assertEquals("Copied byte property", (byte) 111, bean.getByteProperty()); assertEquals("Copied double property", 333.33, bean.getDoubleProperty(), 0.005); assertEquals("Copied int property", 333, bean.getIntProperty()); assertEquals("Copied long property", (long) 3333, bean.getLongProperty()); assertEquals("Copied short property", (short) 33, bean.getShortProperty()); assertEquals("Copied string property", "Custom string", bean.getStringProperty()); // Validate the results for array properties String dupProperty[] = bean.getDupProperty(); assertNotNull("dupProperty present", dupProperty); assertEquals("dupProperty length", 3, dupProperty.length); assertEquals("dupProperty[0]", "New 0", dupProperty[0]); assertEquals("dupProperty[1]", "New 1", dupProperty[1]); assertEquals("dupProperty[2]", "New 2", dupProperty[2]); int intArray[] = bean.getIntArray(); assertNotNull("intArray present", intArray); assertEquals("intArray length", 3, intArray.length); assertEquals("intArray[0]", 100, intArray[0]); assertEquals("intArray[1]", 200, intArray[1]); assertEquals("intArray[2]", 300, intArray[2]); String stringArray[] = bean.getStringArray(); assertNotNull("stringArray present", stringArray);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -