📄 basicdynabeantestcase.java
字号:
/*
* 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.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: 469743 $ $Date: 2006-11-01 01:27:40 +0000 (Wed, 01 Nov 2006) $
*/
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(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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -