📄 basicdynabeantestcase.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.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;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;/** * <p>Test Case for the <code>BasicDynaBean</code> implementation class. * These tests were based on the ones in <code>PropertyUtilsTestCase</code> * because the two classes provide similar levels of functionality.</p> * * @author Craig R. McClanahan * @version $Revision: 1.10 $ $Date: 2004/02/28 13:18:36 $ */public class BasicDynaBeanTestCase extends TestCase { // ---------------------------------------------------- Instance Variables /** * The basic test bean for each test. */ protected DynaBean bean = null; /** * The set of property names we expect to have returned when calling * <code>getDynaProperties()</code>. You should update this list * when new properties are added to TestBean. */ protected final static String[] properties = { "booleanProperty", "booleanSecond", "doubleProperty", "floatProperty", "intArray", "intIndexed", "intProperty", "listIndexed", "longProperty", "mappedProperty", "mappedIntProperty", "nullProperty", "shortProperty", "stringArray", "stringIndexed", "stringProperty", }; // ---------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public BasicDynaBeanTestCase(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 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); // 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(BasicDynaBeanTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { bean = null; } // ------------------------------------------------ Individual Test Methods /** * Corner cases on getDynaProperty invalid arguments. */ public void testGetDescriptorArguments() { try { DynaProperty descriptor = bean.getDynaClass().getDynaProperty("unknown"); assertNull("Unknown property descriptor should be null", descriptor); } catch (Throwable t) { fail("Threw " + t + " instead of returning null"); } try { bean.getDynaClass().getDynaProperty(null); fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException"); } } /** * Positive getDynaProperty on property <code>booleanProperty</code>. */ public void testGetDescriptorBoolean() { testGetDescriptorBase("booleanProperty", Boolean.TYPE); } /** * Positive getDynaProperty on property <code>doubleProperty</code>. */ public void testGetDescriptorDouble() { testGetDescriptorBase("doubleProperty", Double.TYPE); } /** * Positive getDynaProperty on property <code>floatProperty</code>. */ public void testGetDescriptorFloat() { testGetDescriptorBase("floatProperty", Float.TYPE); } /** * Positive getDynaProperty on property <code>intProperty</code>. */ public void testGetDescriptorInt() { testGetDescriptorBase("intProperty", Integer.TYPE); } /** * Positive getDynaProperty on property <code>longProperty</code>. */ public void testGetDescriptorLong() { testGetDescriptorBase("longProperty", Long.TYPE); } /** * Positive getDynaProperty on property <code>booleanSecond</code> * that uses an "is" method as the getter. */ public void testGetDescriptorSecond() { testGetDescriptorBase("booleanSecond", Boolean.TYPE); } /** * Positive getDynaProperty on property <code>shortProperty</code>. */ public void testGetDescriptorShort() { testGetDescriptorBase("shortProperty", Short.TYPE); } /** * Positive getDynaProperty on property <code>stringProperty</code>. */ public void testGetDescriptorString() { testGetDescriptorBase("stringProperty", String.class); } /** * Positive test for getDynaPropertys(). Each property name * listed in <code>properties</code> should be returned exactly once. */ public void testGetDescriptors() { DynaProperty pd[] = bean.getDynaClass().getDynaProperties(); assertNotNull("Got descriptors", pd); int count[] = new int[properties.length]; for (int i = 0; i < pd.length; i++) { String name = pd[i].getName(); for (int j = 0; j < properties.length; j++) { if (name.equals(properties[j])) count[j]++; } } for (int j = 0; j < properties.length; j++) { if (count[j] < 0) fail("Missing property " + properties[j]); else if (count[j] > 1) fail("Duplicate property " + properties[j]); } } /** * Corner cases on getIndexedProperty invalid arguments. */ public void testGetIndexedArguments() { try { bean.get("intArray", -1); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IndexOutOfBoundsException"); } } /** * Positive and negative tests on getIndexedProperty valid arguments. */ public void testGetIndexedValues() { Object value = null; for (int i = 0; i < 5; i++) { try { value = bean.get("intArray", i); assertNotNull("intArray returned value " + i, value); assertTrue("intArray returned Integer " + i, value instanceof Integer); assertEquals("intArray returned correct " + i, i * 10, ((Integer) value).intValue()); } catch (Throwable t) { fail("intArray " + i + " threw " + t); } try { value = bean.get("intIndexed", i); assertNotNull("intIndexed returned value " + i, value); assertTrue("intIndexed returned Integer " + i, value instanceof Integer); assertEquals("intIndexed returned correct " + i, i * 10, ((Integer) value).intValue()); } catch (Throwable t) { fail("intIndexed " + i + " threw " + t); } try { value = bean.get("listIndexed", i); assertNotNull("listIndexed returned value " + i, value); assertTrue("list returned String " + i, value instanceof String); assertEquals("listIndexed returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("listIndexed " + i + " threw " + t); } try { value = bean.get("stringArray", i); assertNotNull("stringArray returned value " + i, value); assertTrue("stringArray returned String " + i, value instanceof String); assertEquals("stringArray returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("stringArray " + i + " threw " + t); } try { value = bean.get("stringIndexed", i); assertNotNull("stringIndexed returned value " + i, value); assertTrue("stringIndexed returned String " + i, value instanceof String); assertEquals("stringIndexed returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("stringIndexed " + i + " threw " + t); } } } /** * Corner cases on getMappedProperty invalid arguments. */ public void testGetMappedArguments() { try { Object value = bean.get("mappedProperty", "unknown"); assertNull("Should not return a value", value); } catch (Throwable t) { fail("Threw " + t + " instead of returning null"); } } /** * Positive and negative tests on getMappedProperty valid arguments. */ public void testGetMappedValues() { Object value = null; try { value = bean.get("mappedProperty", "First Key"); assertEquals("Can find first value", "First Value", value); } catch (Throwable t) { fail("Finding first value threw " + t); } try { value = bean.get("mappedProperty", "Second Key"); assertEquals("Can find second value", "Second Value", value); } catch (Throwable t) { fail("Finding second value threw " + t); } try { value = bean.get("mappedProperty", "Third Key"); assertNull("Can not find third value", value); } catch (Throwable t) { fail("Finding third value threw " + t); } } /** * Corner cases on getSimpleProperty invalid arguments. */ public void testGetSimpleArguments() { try { bean.get(null); fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException"); } } /** * Test getSimpleProperty on a boolean property. */ public void testGetSimpleBoolean() { try { Object value = bean.get("booleanProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Boolean)); assertTrue("Got correct value", ((Boolean) value).booleanValue() == true); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test getSimpleProperty on a double property. */ public void testGetSimpleDouble() { try { Object value = bean.get("doubleProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Double)); assertEquals("Got correct value", ((Double) value).doubleValue(), (double) 321.0, (double) 0.005); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test getSimpleProperty on a float property. */ public void testGetSimpleFloat() { try { Object value = bean.get("floatProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Float)); assertEquals("Got correct value", ((Float) value).floatValue(), (float) 123.0, (float) 0.005); } catch (Throwable t) { fail("Exception: " + t); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -