📄 dynapropertyutilstestcase.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.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import junit.framework.TestCase;import junit.framework.Test;import junit.framework.TestSuite;/** * Test accessing DynaBeans transparently via PropertyUtils. * * @author Craig R. McClanahan * @version $Revision: 1.12 $ $Date: 2004/02/28 13:18:36 $ */public class DynaPropertyUtilsTestCase extends TestCase { // ----------------------------------------------------- Instance Variables /** * The basic test bean for each test. */ protected DynaBean bean = null; /** * The set of properties that should be described. */ protected String describes[] = { "booleanProperty", "booleanSecond", "doubleProperty", "floatProperty", "intArray", "intIndexed", "intProperty", "listIndexed", "longProperty", "mappedObjects", "mappedProperty", "mappedIntProperty", "nested", "nullProperty", // "readOnlyProperty", "shortProperty", "stringArray", "stringIndexed", "stringProperty" }; /** * The nested bean pointed at by the "nested" property. */ protected TestBean nested = null; // ----------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public DynaPropertyUtilsTestCase(String name) { super(name); } // --------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() throws Exception { // Instantiate a new DynaBean instance DynaClass dynaClass = createDynaClass(); bean = dynaClass.newInstance(); // Initialize the DynaBean's property values (like TestBean) bean.set("booleanProperty", new Boolean(true)); bean.set("booleanSecond", new Boolean(true)); bean.set("doubleProperty", new Double(321.0)); bean.set("floatProperty", new Float((float) 123.0)); int intArray[] = { 0, 10, 20, 30, 40 }; bean.set("intArray", intArray); int intIndexed[] = { 0, 10, 20, 30, 40 }; bean.set("intIndexed", intIndexed); bean.set("intProperty", new Integer(123)); List listIndexed = new ArrayList(); listIndexed.add("String 0"); listIndexed.add("String 1"); listIndexed.add("String 2"); listIndexed.add("String 3"); listIndexed.add("String 4"); bean.set("listIndexed", listIndexed); bean.set("longProperty", new Long((long) 321)); HashMap mapProperty = new HashMap(); mapProperty.put("First Key", "First Value"); mapProperty.put("Second Key", "Second Value"); bean.set("mapProperty", mapProperty); HashMap mappedObjects = new HashMap(); mappedObjects.put("First Key", "First Value"); mappedObjects.put("Second Key", "Second Value"); bean.set("mappedObjects", mappedObjects); HashMap mappedProperty = new HashMap(); mappedProperty.put("First Key", "First Value"); mappedProperty.put("Second Key", "Second Value"); bean.set("mappedProperty", mappedProperty); HashMap mappedIntProperty = new HashMap(); mappedIntProperty.put("One", new Integer(1)); mappedIntProperty.put("Two", new Integer(2)); bean.set("mappedIntProperty", mappedIntProperty); nested = new TestBean(); bean.set("nested", nested); // Property "nullProperty" is not initialized, so it should return null bean.set("shortProperty", new Short((short) 987)); String stringArray[] = { "String 0", "String 1", "String 2", "String 3", "String 4" }; bean.set("stringArray", stringArray); String stringIndexed[] = { "String 0", "String 1", "String 2", "String 3", "String 4" }; bean.set("stringIndexed", stringIndexed); bean.set("stringProperty", "This is a string"); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(DynaPropertyUtilsTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { bean = null; nested = null; } // ------------------------------------------------ Individual Test Methods /** * Test copyProperties() when the origin is a a <code>Map</code>. */ public void testCopyPropertiesMap() { Map map = new HashMap(); map.put("booleanProperty", Boolean.FALSE); map.put("doubleProperty", new Double(333.0)); map.put("dupProperty", new String[] { "New 0", "New 1", "New 2" }); map.put("floatProperty", new Float((float) 222.0)); map.put("intArray", new int[] { 0, 100, 200 }); map.put("intProperty", new Integer(111)); map.put("longProperty", new Long(444)); map.put("shortProperty", new Short((short) 555)); map.put("stringProperty", "New String Property"); try { PropertyUtils.copyProperties(bean, map); } catch (Throwable t) { fail("Threw " + t.toString()); } // Scalar properties assertEquals("booleanProperty", false, ((Boolean) bean.get("booleanProperty")).booleanValue()); assertEquals("doubleProperty", 333.0, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005); assertEquals("floatProperty", (float) 222.0, ((Float) bean.get("floatProperty")).floatValue(), (float) 0.005); assertEquals("intProperty", 111, ((Integer) bean.get("intProperty")).intValue()); assertEquals("longProperty", (long) 444, ((Long) bean.get("longProperty")).longValue()); assertEquals("shortProperty", (short) 555, ((Short) bean.get("shortProperty")).shortValue()); assertEquals("stringProperty", "New String Property", (String) bean.get("stringProperty")); // Indexed Properties String dupProperty[] = (String[]) bean.get("dupProperty"); 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[] = (int[]) bean.get("intArray"); 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 describe() method. */ public void testDescribe() { Map map = null; try { map = PropertyUtils.describe(bean); } catch (Exception e) { fail("Threw exception " + e); } // Verify existence of all the properties that should be present for (int i = 0; i < describes.length; i++) { assertTrue("Property '" + describes[i] + "' is present", map.containsKey(describes[i])); } assertTrue("Property 'writeOnlyProperty' is not present", !map.containsKey("writeOnlyProperty")); // Verify the values of scalar properties assertEquals("Value of 'booleanProperty'", Boolean.TRUE, (Boolean) map.get("booleanProperty")); assertEquals("Value of 'doubleProperty'", new Double(321.0), (Double) map.get("doubleProperty")); assertEquals("Value of 'floatProperty'", new Float((float) 123.0), (Float) map.get("floatProperty")); assertEquals("Value of 'intProperty'", new Integer(123), (Integer) map.get("intProperty")); assertEquals("Value of 'longProperty'", new Long(321), (Long) map.get("longProperty")); assertEquals("Value of 'shortProperty'", new Short((short) 987), (Short) map.get("shortProperty")); assertEquals("Value of 'stringProperty'", "This is a string", (String) map.get("stringProperty")); } /** * Corner cases on getIndexedProperty invalid arguments. */ public void testGetIndexedArguments() { // Use explicit index argument try { PropertyUtils.getIndexedProperty(null, "intArray", 0); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getIndexedProperty(bean, null, 0); fail("Should throw IllegalArgumentException 2"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 2"); } // Use index expression try { PropertyUtils.getIndexedProperty(null, "intArray[0]"); fail("Should throw IllegalArgumentException 3"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 3"); } try { PropertyUtils.getIndexedProperty(bean, "[0]"); fail("Should throw NoSuchMethodException 4"); } catch (NoSuchMethodException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of NoSuchMethodException 4"); } try { PropertyUtils.getIndexedProperty(bean, "intArray"); fail("Should throw IllegalArgumentException 5"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 5"); } // Use explicit index argument try { PropertyUtils.getIndexedProperty(null, "intIndexed", 0); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getIndexedProperty(bean, null, 0); fail("Should throw IllegalArgumentException 2"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 2"); } // Use index expression try { PropertyUtils.getIndexedProperty(null, "intIndexed[0]"); fail("Should throw IllegalArgumentException 3"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 3"); } try { PropertyUtils.getIndexedProperty(bean, "[0]"); fail("Should throw NoSuchMethodException 4");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -