| Method from org.apache.commons.digester.SetNestedPropertiesRuleTestCase Detail: |
public void setUp() {
digester = new Digester();
}
Set up instance variables required by this test case. |
public static Test suite() {
return (new TestSuite(SetNestedPropertiesRuleTestCase.class));
}
Return the tests included in this test suite. |
public void tearDown() {
digester = null;
}
Tear down instance variables required by this test case. |
public void testAutomaticallySetProperties() throws IOException, SAXException {
// 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());
SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
// 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());
}
Test that you can successfully automatically set properties. |
public void testCustomisedProperties1() throws IOException, SAXException {
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");
// 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", "gamma-alt", "delta"},
new String[]{null, "gamma"});
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 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 customise the property mappings using the
constructor which takes arrays-of-strings. |
public void testCustomisedProperties2a() throws IOException, SAXException {
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 ignore a single input xml element using the
constructor which takes a single remapping. |
public void testCustomisedProperties2b() throws IOException, SAXException {
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
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 testMandatoryProperties() throws IOException, SAXException {
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.indexOf("badprop") >= 0) {
// ok, this is expected; there is no "setBadprop" method on the
// SimpleTestBean class...
} else {
fail("Unexpected parse exception:" + e.getMessage());
}
}
}
Test that it is an error when a child element exists but no corresponding
java property exists. |
public void testMultiRuleMatch() throws IOException, SAXException {
String testXml =
"< ?xml version='1.0'? >" +
"< root >" +
"< testbean alpha='alpha-attr' >ROOT BODY" +
"< beta >BETA BODY< /beta >" +
"< gamma >GAMMA " +
"< prop name='delta' value='delta-prop'/ >" +
"BODY" +
"< /gamma >" +
"< /testbean >" +
"< /root >";
Reader reader = new StringReader(testXml);
// going to be setting properties on a SimpleTestBean
digester.addObjectCreate("root/testbean",
"org.apache.commons.digester.SimpleTestBean");
digester.addRule("root/testbean", new SetNestedPropertiesRule());
digester.addSetProperties("root/testbean");
digester.addSetProperty("root/testbean/gamma/prop", "name", "value");
SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
assertNotNull("No object created", bean);
// check properties are set correctly
assertEquals(
"Property alpha not set correctly",
"alpha-attr",
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-prop",
bean.getDeltaValue());
// check no bad rules object is left
assertEquals(
"Digester rules object not reset.",
RulesBase.class, digester.getRules().getClass());
}
Test that:
- you can have rules matching the same pattern as the
SetNestedPropertiesRule,
- you can have rules matching child elements of the rule,
- the Rules object is reset nicely.
|
public void testRecursiveNestedProperties() throws IOException, SAXException {
String testXml =
"< ?xml version='1.0'? >" +
"< testbean >" +
"< beta >BETA BODY< /beta >" +
"< testbean >" +
"< beta >BETA BODY< /beta >" +
"< /testbean >" +
"< /testbean >";
Reader reader = new StringReader(testXml);
// going to be setting properties on a SimpleTestBean
digester.addObjectCreate("*/testbean",
"org.apache.commons.digester.SimpleTestBean");
SetNestedPropertiesRule rule = new SetNestedPropertiesRule();
rule.setAllowUnknownChildElements(true);
digester.addRule("*/testbean", rule);
SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
assertNotNull(bean);
}
|
public void testUnknownChildrenCausesException() throws IOException, SAXException {
String testXml =
"< ?xml version='1.0'? >" +
"< root >" +
"< testbean >" +
"< beta >BETA BODY< /beta >" +
"< foo >GAMMA< /foo >" +
"< /testbean >" +
"< /root >";
Reader reader = new StringReader(testXml);
// going to be setting properties on a SimpleTestBean
digester.addObjectCreate("root",
"org.apache.commons.digester.SimpleTestBean");
Rule rule = new SetNestedPropertiesRule();
digester.addRule("root", rule);
try {
SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
fail("Expected to generate an exception.");
} catch(SAXException e) {
Exception nested = e.getException();
if ((nested==null) || !(nested instanceof NoSuchMethodException)) {
// nope, not the sort of exception we expected
throw e;
}
}
}
Test that unknown child elements trigger an exception. |
public void testUnknownChildrenExceptionOverride() throws IOException, SAXException {
String testXml =
"< ?xml version='1.0'? >" +
"< root >" +
"< testbean >" +
"< beta >BETA BODY< /beta >" +
"< foo >GAMMA< /foo >" +
"< /testbean >" +
"< /root >";
Reader reader = new StringReader(testXml);
// going to be setting properties on a SimpleTestBean
digester.addObjectCreate("root",
"org.apache.commons.digester.SimpleTestBean");
SetNestedPropertiesRule rule = new SetNestedPropertiesRule();
rule.setAllowUnknownChildElements(true);
digester.addRule("root", rule);
SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
assertNotNull(bean);
}
Test that unknown child elements are allowed if the appropriate
flag is set. |