📄 propertyutilstestcase.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.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.beanutils.priv.PrivateBeanFactory;import org.apache.commons.beanutils.priv.PrivateDirect;import junit.framework.TestCase;import junit.framework.Test;import junit.framework.TestSuite;/** * <p>Test Case for the PropertyUtils 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>So far, this test case has tests for the following methods of the * <code>PropertyUtils</code> class:</p> * <ul> * <li>getIndexedProperty(Object,String)</li> * <li>getIndexedProperty(Object,String,int)</li> * <li>getMappedProperty(Object,String)</li> * <li>getMappedProperty(Object,String,String</li> * <li>getNestedProperty(Object,String)</li> * <li>getPropertyDescriptor(Object,String)</li> * <li>getPropertyDescriptors(Object)</li> * <li>getPropertyType(Object,String)</li> * <li>getSimpleProperty(Object,String)</li> * <li>setIndexedProperty(Object,String,Object)</li> * <li>setIndexedProperty(Object,String,String,Object)</li> * <li>setMappedProperty(Object,String,Object)</li> * <li>setMappedProperty(Object,String,String,Object)</li> * <li>setNestedProperty(Object,String,Object)</li> * <li>setSimpleProperty(Object,String,Object)</li> * </ul> * * @author Craig R. McClanahan * @author Jan Sorensen * @version $Revision: 1.34 $ $Date: 2004/02/28 13:18:36 $ */public class PropertyUtilsTestCase extends TestCase { // ---------------------------------------------------- Instance Variables /** * The fully qualified class name of our private directly * implemented interface. */ private static final String PRIVATE_DIRECT_CLASS = "org.apache.commons.beanutils.priv.PrivateDirect"; /** * The fully qualified class name of our private indirectly * implemented interface. */ private static final String PRIVATE_INDIRECT_CLASS = "org.apache.commons.beanutils.priv.PrivateIndirect"; /** * The fully qualified class name of our test bean class. */ private static final String TEST_BEAN_CLASS = "org.apache.commons.beanutils.TestBean"; /** * The basic test bean for each test. */ protected TestBean bean = null; /** * The "package private subclass" test bean for each test. */ protected TestBeanPackageSubclass beanPackageSubclass = null; /** * The test bean for private access tests. */ protected PrivateDirect beanPrivate = null; /** * The test bean for private access tests of subclasses. */ protected PrivateDirect beanPrivateSubclass = null; /** * The "public subclass" test bean for each test. */ protected TestBeanPublicSubclass beanPublicSubclass = 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 set of property names we expect to have returned when calling * <code>getPropertyDescriptors()</code>. You should update this list * when new properties are added to TestBean. */ protected final static String[] properties = { "booleanProperty", "booleanSecond", "doubleProperty", "dupProperty", "floatProperty", "intArray", "intIndexed", "intProperty", "listIndexed", "longProperty", "nested", "nullProperty", "readOnlyProperty", "shortProperty", "stringArray", "stringIndexed", "stringProperty", "writeOnlyProperty", }; // ---------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public PropertyUtilsTestCase(String name) { super(name); } // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() { bean = new TestBean(); beanPackageSubclass = new TestBeanPackageSubclass(); beanPrivate = PrivateBeanFactory.create(); beanPrivateSubclass = PrivateBeanFactory.createSubclass(); beanPublicSubclass = new TestBeanPublicSubclass(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(PropertyUtilsTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { bean = null; beanPackageSubclass = null; beanPrivate = null; beanPrivateSubclass = null; beanPublicSubclass = 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, bean.getBooleanProperty()); assertEquals("doubleProperty", 333.0, bean.getDoubleProperty(), 0.005); assertEquals("floatProperty", (float) 222.0, bean.getFloatProperty(), (float) 0.005); assertEquals("intProperty", 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 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 getPropertyDescriptor invalid arguments. */ public void testGetDescriptorArguments() { try { PropertyUtils.getPropertyDescriptor(null, "stringProperty"); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getPropertyDescriptor(bean, null); fail("Should throw IllegalArgumentException 2"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 2"); } } /** * Positive getPropertyDescriptor on property <code>booleanProperty</code>. */ public void testGetDescriptorBoolean() { testGetDescriptorBase("booleanProperty", "getBooleanProperty", "setBooleanProperty"); } /** * Positive getPropertyDescriptor on property <code>doubleProperty</code>. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -