📄 testdynaactionform.java
字号:
/*
* $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/action/TestDynaActionForm.java,v 1.15 2004/03/14 06:23:51 sraeburn Exp $
* $Revision: 1.15 $
* $Date: 2004/03/14 06:23:51 $
*
* Copyright 1999-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.struts.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.impl.ModuleConfigImpl;
/**
* Suite of unit tests for the
* <code>org.apache.struts.action.DynaActionForm</code> class.
*/
public class TestDynaActionForm extends TestDynaActionFormClass {
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestDynaActionForm(String theName)
{
super(theName);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs)
{
junit.awtui.TestRunner.main
(new String[] {TestDynaActionForm.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestDynaActionForm.class);
}
// ----------------------------------------------------- Instance Variables
/**
* Dummy ModuleConfig for calls to reset() and validate().
*/
protected ModuleConfig moduleConfig = null;
/**
* The basic <code>DynaActionForm</code> to use for testing.
*/
protected DynaActionForm dynaForm = null;
/**
* Dummy ActionMapping for calls to reset() and validate().
*/
protected ActionMapping mapping = null;
protected Log log = 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",
};
// ----------------------------------------------------- Setup and Teardown
public void setUp() {
super.setUp();
try {
dynaForm = (DynaActionForm) dynaClass.newInstance();
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage());
}
setupComplexProperties();
moduleConfig = new DynaActionFormConfig(beanConfig);
mapping = new DynaActionFormMapping(moduleConfig);
log = LogFactory.getLog(this.getClass().getName() + "." + this.getName());
}
public void tearDown() {
super.tearDown();
moduleConfig = null;
dynaForm = null;
mapping = null;
}
// --------------------------------------------- Create New DynaActionForms
// Test basic form bean properties on creation
public void testBeanCreate() {
assertEquals("booleanProperty", Boolean.TRUE,
(Boolean) dynaForm.get("booleanProperty"));
assertEquals("booleanSecond", Boolean.TRUE,
(Boolean) dynaForm.get("booleanSecond"));
assertEquals("doubleProperty", new Double(321.0),
(Double) dynaForm.get("doubleProperty"));
assertEquals("floatProperty", new Float((float) 123.0),
(Float) dynaForm.get("floatProperty"));
assertEquals("intProperty", new Integer(123),
(Integer) dynaForm.get("intProperty"));
// FIXME - listIndexed
assertEquals("longProperty", new Long((long) 321),
(Long) dynaForm.get("longProperty"));
// FIXME - mappedProperty
// FIXME - mappedIntProperty
// assertEquals("nullProperty", (String) null,
// (String) dynaForm.get("nullProperty"));
assertEquals("shortProperty", new Short((short) 987),
(Short) dynaForm.get("shortProperty"));
assertEquals("stringProperty", "This is a string",
(String) dynaForm.get("stringProperty"));
}
// Test initialize() method on indexed values to ensure that the
// result returned by FormPropertyConfig().initial() is never clobbered
public void testIndexedInitialize() {
// Update some values in the indexed properties
dynaForm.set("intArray", 1, new Integer(111));
assertEquals("intArray[1]", new Integer(111),
(Integer) dynaForm.get("intArray", 1));
dynaForm.set("intIndexed", 2, new Integer(222));
assertEquals("intIndexed[2]", new Integer(222),
(Integer) dynaForm.get("intIndexed", 2));
dynaForm.set("stringArray", 3, "New String 3");
assertEquals("stringArray[3]", "New String 3",
(String) dynaForm.get("stringArray", 3));
dynaForm.set("stringIndexed", 4, "New String 4");
assertEquals("stringIndexed[4]", "New String 4",
(String) dynaForm.get("stringIndexed", 4));
// Perform initialize() and revalidate the original values
// while ensuring our initial values did not get corrupted
dynaForm.initialize(mapping);
setupComplexProperties();
testGetIndexedValues();
}
// Test initialize() method going back to initial values
public void testScalarInitialize() {
// Update a bunch of scalar properties to new values
dynaForm.set("booleanProperty", Boolean.FALSE);
assertEquals("booleanProperty", Boolean.FALSE,
(Boolean) dynaForm.get("booleanProperty"));
dynaForm.set("booleanSecond", Boolean.FALSE);
dynaForm.set("doubleProperty", new Double(654.0));
dynaForm.set("floatProperty", new Float((float) 543.0));
dynaForm.set("intProperty", new Integer(555));
dynaForm.set("longProperty", new Long((long) 777));
dynaForm.set("shortProperty", new Short((short) 222));
dynaForm.set("stringProperty", "New String Value");
assertEquals("stringProperty", "New String Value",
(String) dynaForm.get("stringProperty"));
// Perform initialize() and revalidate the original values
dynaForm.initialize(mapping);
setupComplexProperties();
testBeanCreate();
}
// --------------------------------------- Tests from BasicDynaBeanTestCase
/**
* Corner cases on getDynaProperty invalid arguments.
*/
public void testGetDescriptorArguments() {
DynaProperty descriptor =
dynaForm.getDynaClass().getDynaProperty("unknown");
assertNull("Unknown property descriptor should be null",
descriptor);
try {
dynaForm.getDynaClass().getDynaProperty(null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
; // Expected response
}
}
/**
* 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[] = dynaForm.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 {
dynaForm.get("intArray", -1);
fail("Should throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
; // Expected response
}
}
/**
* Positive and negative tests on getIndexedProperty valid arguments.
*/
public void testGetIndexedValues() {
Object value = null;
for (int i = 0; i < 5; i++) {
value = dynaForm.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());
value = dynaForm.get("intIndexed", i);
assertNotNull("intIndexed returned value " + i, value);
assertTrue("intIndexed returned Integer " + i,
value instanceof Integer);
assertEquals("intIndexed returned correct " + i, i * 100,
((Integer) value).intValue());
value = dynaForm.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);
value = dynaForm.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);
value = dynaForm.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);
}
}
/**
* Corner cases on getMappedProperty invalid arguments.
*/
public void testGetMappedArguments() {
Object value = dynaForm.get("mappedProperty", "unknown");
assertNull("Should not return a value", value);
}
/**
* Positive and negative tests on getMappedProperty valid arguments.
*/
public void testGetMappedValues() {
Object value = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -