You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2005/04/15 08:43:30 UTC
svn commit: r161418 -
jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java
Author: skitching
Date: Thu Apr 14 23:43:30 2005
New Revision: 161418
URL: http://svn.apache.org/viewcvs?view=rev&rev=161418
Log:
Add additional tests. In particular, test for previous bug where
SetNestedPropertiesRule("foo", null) didn't cause element "foo"
to be ignored.
Modified:
jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java
Modified: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java?view=diff&r1=161417&r2=161418
==============================================================================
--- jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java (original)
+++ jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/SetNestedPropertiesRuleTestCase.java Thu Apr 14 23:43:30 2005
@@ -145,51 +145,211 @@
"GAMMA BODY",
bean.getGamma());
+ assertEquals(
+ "Property delta not set correctly",
+ "DELTA BODY",
+ bean.getDeltaValue());
+ }
+
+ /**
+ * Test that it is an error when a child element exists but no corresponding
+ * java property exists.
+ */
+ public void testMandatoryProperties()
+ throws SAXException, IOException {
+
+ String TEST_XML =
+ "<?xml version='1.0'?>" +
+ "<root>ROOT BODY" +
+ "<badprop>ALPHA BODY</badprop>" +
+ "</root>";
+ // going to be setting properties on a SimpleTestBean
+ digester.addObjectCreate("root",
+ "org.apache.commons.digester.SimpleTestBean");
+
+ // match all children of root with this rule
+ digester.addRule("root", new SetNestedPropertiesRule());
+
+ try {
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(
+ new StringReader(TEST_XML));
+
+ // we should never get here...
+ fail("No exception thrown by parse when unknown child element found.");
+ } catch(org.xml.sax.SAXParseException e) {
+ String msg = e.getMessage();
+ if (msg.contains("badprop")) {
+ // ok, this is expected; there is no "setBadprop" method on the
+ // SimpleTestBean class...
+ } else {
+ fail("Unexpected parse exception:" + e.getMessage());
+ }
+ }
}
/**
- * Test that you can customise the property mappings.
+ * Test that you can customise the property mappings using the
+ * constructor which takes arrays-of-strings.
*/
- public void testCustomisedProperties()
+ public void testCustomisedProperties1()
throws SAXException, IOException {
+ String TEST_XML =
+ "<?xml version='1.0'?>" +
+ "<root>ROOT BODY" +
+ "<alpha>ALPHA BODY</alpha>" +
+ "<beta>BETA BODY</beta>" +
+ "<gamma-alt>GAMMA BODY</gamma-alt>" +
+ "<delta>DELTA BODY</delta>" +
+ "</root>";
+
// going to be setting properties on a SimpleTestBean
digester.addObjectCreate("root",
"org.apache.commons.digester.SimpleTestBean");
- // ignorethe "alpha" element
- // map the "beta" element into the gamma property
- // map the gamma element into the delta property
- // ignore the delta element
+ // ignore the "alpha" element (target=null)
+ // don't remap the "beta" element
+ // map the gamma-alt element into the gamma property
+ // ignore the delta element (no matching element in array)
Rule rule = new SetNestedPropertiesRule(
- new String[]{"alpha", "beta", "gamma", "delta"},
- new String[]{null, "gamma", "delta"});
+ new String[]{"alpha", "gamma-alt", "delta"},
+ new String[]{null, "gamma"});
digester.addRule("root", rule);
- SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(
+ new StringReader(TEST_XML));
// check properties are set correctly
assertEquals(
- "Property alpha not set correctly",
+ "Property alpha was not ignored (it should be)",
null,
bean.getAlpha());
assertEquals(
"Property beta not set correctly",
- null,
+ "BETA BODY",
bean.getBeta());
assertEquals(
"Property gamma not set correctly",
+ "GAMMA BODY",
+ bean.getGamma());
+
+ assertEquals(
+ "Property delta was not ignored (it should be)",
+ null,
+ bean.getDeltaValue());
+
+ // check no bad rules object is left
+ assertEquals(
+ "Digester rules object not reset.",
+ RulesBase.class, digester.getRules().getClass());
+ }
+
+ /**
+ * Test that you can ignore a single input xml element using the
+ * constructor which takes a single remapping.
+ */
+ public void testCustomisedProperties2a()
+ throws SAXException, IOException {
+
+ String TEST_XML =
+ "<?xml version='1.0'?>" +
+ "<root>ROOT BODY" +
+ "<alpha>ALPHA BODY</alpha>" +
+ "<beta>BETA BODY</beta>" +
+ "<gamma>GAMMA BODY</gamma>" +
+ "<delta>DELTA BODY</delta>" +
+ "</root>";
+
+ // going to be setting properties on a SimpleTestBean
+ digester.addObjectCreate("root",
+ "org.apache.commons.digester.SimpleTestBean");
+
+ // ignore the "alpha" element (target=null)
+ Rule rule = new SetNestedPropertiesRule("alpha", null);
+ digester.addRule("root", rule);
+
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(
+ new StringReader(TEST_XML));
+
+ // check properties are set correctly
+ assertEquals(
+ "Property alpha was not ignored (it should be)",
+ null,
+ bean.getAlpha());
+
+ assertEquals(
+ "Property beta not set correctly",
"BETA BODY",
+ bean.getBeta());
+
+ assertEquals(
+ "Property gamma not set correctly",
+ "GAMMA BODY",
bean.getGamma());
assertEquals(
"Property delta not set correctly",
+ "DELTA BODY",
+ bean.getDeltaValue());
+
+ // check no bad rules object is left
+ assertEquals(
+ "Digester rules object not reset.",
+ RulesBase.class, digester.getRules().getClass());
+ }
+
+ /**
+ * Test that you can customise the property mappings using the
+ * constructor which takes a single remapping.
+ */
+ public void testCustomisedProperties2b()
+ throws SAXException, IOException {
+
+ String TEST_XML =
+ "<?xml version='1.0'?>" +
+ "<root>ROOT BODY" +
+ "<alpha-alt>ALPHA BODY</alpha-alt>" +
+ "<beta>BETA BODY</beta>" +
+ "<gamma>GAMMA BODY</gamma>" +
+ "<delta>DELTA BODY</delta>" +
+ "</root>";
+
+ // going to be setting properties on a SimpleTestBean
+ digester.addObjectCreate("root",
+ "org.apache.commons.digester.SimpleTestBean");
+
+ // map the contents of the alpha-alt xml child into the
+ // "alpha" java property.
+ Rule rule = new SetNestedPropertiesRule("alpha-alt", "alpha");
+ digester.addRule("root", rule);
+
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(
+ new StringReader(TEST_XML));
+
+ // check properties are set correctly
+ assertEquals(
+ "Property alpha not set correctly",
+ "ALPHA BODY",
+ bean.getAlpha());
+
+ assertEquals(
+ "Property beta not set correctly",
+ "BETA BODY",
+ bean.getBeta());
+
+ assertEquals(
+ "Property gamma not set correctly",
"GAMMA BODY",
+ bean.getGamma());
+
+ assertEquals(
+ "Property delta not set correctly",
+ "DELTA BODY",
bean.getDeltaValue());
// check no bad rules object is left
@@ -197,7 +357,6 @@
"Digester rules object not reset.",
RulesBase.class, digester.getRules().getClass());
}
-
/**
* Test that:
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org