public void testPropagateException() throws Exception {
// only used with this method
class ThrowExceptionCreateRule extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) throws Exception {
throw new RuntimeException();
}
}
// now for the tests
String xml = "< ?xml version='1.0' ? >< root >< element/ >< /root >";
// test default - which is to propagate the exception
Digester digester = new Digester();
digester.addFactoryCreate("root", new ThrowExceptionCreateRule());
try {
digester.parse(new StringReader(xml));
fail("Exception not propagated from create rule (1)");
} catch (Exception e) {
/* This is what's expected */
}
// test propagate exception
digester = new Digester();
digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), false);
try {
digester.parse(new StringReader(xml));
fail("Exception not propagated from create rule (1)");
} catch (Exception e) {
/* This is what's expected */
}
// test don't propagate exception
digester = new Digester();
digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), true);
try {
digester.parse(new StringReader(xml));
} catch (Exception e) {
// this shouldn't happen
fail("Exception should not be propagated");
}
}
|