craigmcc 2002/06/29 18:49:21
Modified: . build-tests.xml
src/share/org/apache/struts/config FormPropertyConfig.java
src/test/org/apache/struts/action TestDynaActionForm.java
TestDynaActionFormClass.java
Log:
It is now possible to initialize the value of an indexed property in a
DynaActionForm bean, with syntax like this:
<form-property name="intArray" type="int[]"
initial="{ 0, 1, 2, 3}"/>
<form-property name="doubleArray" type="double[]"
initial="0.1 2.3 4.5"/>
<form-property name="stringArray", type="java.lang.String[]",
initial="'String 0', 'String 1', 'String 2'"/>
if you are using a version of commons-beanutils >= the 20020630 nightly build
(or any upcoming 1.4 release). In addition, you can register your own
String->ArrayType converters in ConvertUtils for arrays of non-primitive
types, and parse the initial value string any way you like.
The syntax for primitive and String indexed initialization was designed to be
similar to that a Java developer uses to initialize arrays directly:
int intArray[] = new int[] { 0, 1, 2, 3};
with the following adjustments:
* Only literal values are accepted -- no expression evaluation the way
a compiler would do it.
* Beginning and ending "{" and "}" are optional -- you need both or neither.
* Commas in between elements are considered whitespace, so they are
optional as well.
* You can use either single quotes or double quotes around strings, and
embed the opposite type inside the String. (Commas can, of course,
be embedded as well.
When the reset() method of a DynaActionFormBean is called (including when it
is created initially for you), the default functionality is to initialize
all form bean properties to the initial values again. You can override this
in your own DynaActionForm subclass if need something extra.
Revision Changes Path
1.7 +1 -0 jakarta-struts/build-tests.xml
Index: build-tests.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/build-tests.xml,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- build-tests.xml 29 Jun 2002 00:44:34 -0000 1.6
+++ build-tests.xml 30 Jun 2002 01:49:21 -0000 1.7
@@ -98,6 +98,7 @@
<pathelement location="${cactus.jar}"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${servlet.jar}"/>
+ <pathelement location="${struts.jar}"/>
</path>
<!-- ========== Executable Targets ======================================== -->
1.6 +22 -28
jakarta-struts/src/share/org/apache/struts/config/FormPropertyConfig.java
Index: FormPropertyConfig.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/config/FormPropertyConfig.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- FormPropertyConfig.java 29 Jun 2002 00:44:34 -0000 1.5
+++ FormPropertyConfig.java 30 Jun 2002 01:49:21 -0000 1.6
@@ -253,38 +253,32 @@
*/
public Object initial() {
+ // Compute our initial value the first time it is requested
// Don't bother synchronizing, a race is basically harmless
if (!initialized) {
try {
- if (initial == null) {
- if ("boolean".equals(type)) {
- initialValue = Boolean.FALSE;
- } else if ("byte".equals(type)) {
- initialValue = new Byte((byte) 0);
- } else if ("char".equals(type)) {
- initialValue = new Character((char) 0);
- } else if ("double".equals(type)) {
- initialValue = new Double((double) 0.0);
- } else if ("float".equals(type)) {
- initialValue = new Float((float) 0.0);
- } else if ("int".equals(type)) {
- initialValue = new Integer(0);
- } else if ("long".equals(type)) {
- initialValue = new Long((long) 0);
- } else if ("short".equals(type)) {
- initialValue = new Short((short) 0);
- } else {
- initialValue = null;
- }
- } else {
- Class clazz = getTypeClass();
- initialValue = ConvertUtils.convert(initial, clazz);
- }
+ Class clazz = getTypeClass();
+ initialValue = ConvertUtils.convert(initial, clazz);
} catch (Throwable t) {
initialValue = null;
}
initialized = true;
}
+
+ // Clone if the initial value is an array
+ if ((initialValue != null) &&
+ (initialValue.getClass().isArray())) {
+ int n = Array.getLength(initialValue);
+ Class componentType =
+ initialValue.getClass().getComponentType();
+ Object newValue = Array.newInstance(componentType, n);
+ for (int j = 0; j < n; j++) {
+ Array.set(newValue, j, Array.get(initialValue, j));
+ }
+ return (newValue);
+ }
+
+ // Return the calculated value
return (initialValue);
}
1.4 +138 -32
jakarta-struts/src/test/org/apache/struts/action/TestDynaActionForm.java
Index: TestDynaActionForm.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/test/org/apache/struts/action/TestDynaActionForm.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestDynaActionForm.java 29 Jun 2002 05:50:25 -0000 1.3
+++ TestDynaActionForm.java 30 Jun 2002 01:49:21 -0000 1.4
@@ -63,6 +63,9 @@
import junit.framework.*;
import org.apache.cactus.*;
import org.apache.commons.beanutils.DynaProperty;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.config.ApplicationConfig;
+import org.apache.struts.config.FormBeanConfig;
/**
@@ -110,12 +113,24 @@
/**
+ * Dummy ApplicationConfig for calls to reset() and validate().
+ */
+ protected ApplicationConfig appConfig = 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;
+
+
+ /**
* 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.
@@ -153,33 +168,10 @@
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage());
}
+ setupComplexProperties();
+ appConfig = new TestDynaActionFormConfig(beanConfig);
+ mapping = new TestDynaActionFormMapping(appConfig);
- // Temporarily, set up the complex properties manually
- int intArray[] = { 0, 10, 20, 30, 40 };
- dynaForm.set("intArray", intArray);
- int intIndexed[] = { 0, 10, 20, 30, 40 };
- dynaForm.set("intIndexed", intIndexed);
- List listIndexed = new ArrayList();
- listIndexed.add("String 0");
- listIndexed.add("String 1");
- listIndexed.add("String 2");
- listIndexed.add("String 3");
- listIndexed.add("String 4");
- dynaForm.set("listIndexed", listIndexed);
- Map mappedProperty = new HashMap();
- mappedProperty.put("First Key", "First Value");
- mappedProperty.put("Second Key", "Second Value");
- dynaForm.set("mappedProperty", mappedProperty);
- Map mappedIntProperty = new HashMap();
- mappedIntProperty.put("One", new Integer(1));
- mappedIntProperty.put("Two", new Integer(2));
- dynaForm.set("mappedIntProperty", mappedIntProperty);
- String stringArray[] =
- { "String 0", "String 1", "String 2", "String 3", "String 4" };
- dynaForm.set("stringArray", stringArray);
- String stringIndexed[] =
- { "String 0", "String 1", "String 2", "String 3", "String 4" };
- dynaForm.set("stringIndexed", stringIndexed);
}
@@ -187,8 +179,9 @@
public void tearDown() {
super.tearDown();
+ appConfig = null;
dynaForm = null;
-
+ mapping = null;
}
@@ -207,8 +200,8 @@
(Double) dynaForm.get("doubleProperty"));
assertEquals("floatProperty", new Float((float) 123.0),
(Float) dynaForm.get("floatProperty"));
- // FIXME - intArray
- // FIXME - intIndexed
+ assertEquals("intProperty", new Integer(123),
+ (Integer) dynaForm.get("intProperty"));
// FIXME - listIndexed
assertEquals("longProperty", new Long((long) 321),
(Long) dynaForm.get("longProperty"));
@@ -218,14 +211,64 @@
(String) dynaForm.get("nullProperty"));
assertEquals("shortProperty", new Short((short) 987),
(Short) dynaForm.get("shortProperty"));
- // FIXME - stringArray
- // FIXME - stringIndexed
assertEquals("stringProperty", "This is a string",
(String) dynaForm.get("stringProperty"));
}
+ // Test reset() method on indexed values to ensure that the
+ // result returned by FormPropertyConfig().initial() is never clobbered
+ public void testIndexedReset() {
+
+ // 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 reset and revalidate the original values
+ // while ensuring our initial values did not get corrupted
+ dynaForm.reset(mapping, (ServletRequest) null);
+ setupComplexProperties();
+ testGetIndexedValues();
+
+ }
+
+
+ // Test reset() method going back to initial values
+ public void testScalarReset() {
+
+ // 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"));
+
+ // Reset and revalidate the original values
+ dynaForm.reset(mapping, (ServletRequest) null);
+ setupComplexProperties();
+ testBeanCreate();
+
+ }
+
+
// --------------------------------------- Tests from BasicDynaBeanTestCase
@@ -405,7 +448,7 @@
assertNotNull("intIndexed returned value " + i, value);
assertTrue("intIndexed returned Integer " + i,
value instanceof Integer);
- assertEquals("intIndexed returned correct " + i, i * 10,
+ assertEquals("intIndexed returned correct " + i, i * 100,
((Integer) value).intValue());
} catch (Throwable t) {
fail("intIndexed " + i + " threw " + t);
@@ -962,6 +1005,34 @@
/**
+ * Set up the complex properties that cannot be configured from the
+ * initial value expression.
+ */
+ protected void setupComplexProperties() {
+
+ List listIndexed = new ArrayList();
+ listIndexed.add("String 0");
+ listIndexed.add("String 1");
+ listIndexed.add("String 2");
+ listIndexed.add("String 3");
+ listIndexed.add("String 4");
+ dynaForm.set("listIndexed", listIndexed);
+
+ Map mappedProperty = new HashMap();
+ mappedProperty.put("First Key", "First Value");
+ mappedProperty.put("Second Key", "Second Value");
+ dynaForm.set("mappedProperty", mappedProperty);
+
+ Map mappedIntProperty = new HashMap();
+ mappedIntProperty.put("One", new Integer(1));
+ mappedIntProperty.put("Two", new Integer(2));
+ dynaForm.set("mappedIntProperty", mappedIntProperty);
+
+ }
+
+
+
+ /**
* Base for testGetDescriptorXxxxx() series of tests.
*
* @param name Name of the property to be retrieved
@@ -980,5 +1051,40 @@
}
+
+}
+
+
+class TestDynaActionFormMapping extends ActionMapping {
+
+ public TestDynaActionFormMapping(ApplicationConfig appConfig) {
+ this.appConfig = appConfig;
+ }
+
+ private ApplicationConfig appConfig = null;
+
+ public ApplicationConfig getApplicationConfig() {
+ return (this.appConfig);
+ }
+
+ public String getName() {
+ return ("dynaForm");
+ }
+
+}
+
+
+class TestDynaActionFormConfig extends ApplicationConfig {
+
+ public TestDynaActionFormConfig(FormBeanConfig beanConfig) {
+ super("");
+ this.beanConfig = beanConfig;
+ }
+
+ private FormBeanConfig beanConfig = null;
+
+ public FormBeanConfig findFormBeanConfig(String name) {
+ return (this.beanConfig);
+ }
}
1.4 +8 -7
jakarta-struts/src/test/org/apache/struts/action/TestDynaActionFormClass.java
Index: TestDynaActionFormClass.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/test/org/apache/struts/action/TestDynaActionFormClass.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestDynaActionFormClass.java 29 Jun 2002 05:50:25 -0000 1.3
+++ TestDynaActionFormClass.java 30 Jun 2002 01:49:21 -0000 1.4
@@ -120,14 +120,15 @@
* The set of <code>FormPropertyConfig</code> objects to use when
* creating our <code>FormBeanConfig</code>.
*/
- // FIXME - initial values for arrays/lists/maps?
protected static final FormPropertyConfig[] dynaProperties = {
new FormPropertyConfig("booleanProperty", "boolean", "true"),
new FormPropertyConfig("booleanSecond", "boolean", "true"),
new FormPropertyConfig("doubleProperty", "double", "321.0"),
new FormPropertyConfig("floatProperty", "float", "123.0"),
- new FormPropertyConfig("intArray", "int[]", null),
- new FormPropertyConfig("intIndexed", "int[]", null),
+ new FormPropertyConfig("intArray", "int[]",
+ "{ 0, 10,20, \"30\" '40' }"),
+ new FormPropertyConfig("intIndexed", "int[]",
+ " 0 100, 200, 300, 400 "),
new FormPropertyConfig("intProperty", "int", "123"),
new FormPropertyConfig("listIndexed", "java.util.List", null),
new FormPropertyConfig("longProperty", "long", "321"),
@@ -135,8 +136,10 @@
new FormPropertyConfig("mappedIntProperty", "java.util.Map", null),
new FormPropertyConfig("nullProperty", "java.lang.String", null),
new FormPropertyConfig("shortProperty", "short", "987"),
- new FormPropertyConfig("stringArray", "java.lang.String[]", null),
- new FormPropertyConfig("stringIndexed", "java.lang.String[]", null),
+ new FormPropertyConfig("stringArray", "java.lang.String[]",
+ "{ 'String 0', 'String 1', 'String 2', 'String 3',
'String 4'}"),
+ new FormPropertyConfig("stringIndexed", "java.lang.String[]",
+ "{ 'String 0', 'String 1', 'String 2', 'String 3',
'String 4'}"),
new FormPropertyConfig("stringProperty", "java.lang.String",
"This is a string"),
};
@@ -250,8 +253,6 @@
assertEquals("floatProperty value",
new Float((float) 123.0),
beanConfig.findFormPropertyConfig("floatProperty").initial());
- // FIXME - intArray
- // FIXME - intIndexed
assertEquals("intProperty value",
new Integer(123),
beanConfig.findFormPropertyConfig("intProperty").initial());
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>