You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2002/09/04 20:01:16 UTC
cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester BeanPropertySetterRuleTestCase.java RuleTestCase.java
rdonkin 2002/09/04 11:01:16
Modified: digester/src/test/org/apache/commons/digester
BeanPropertySetterRuleTestCase.java
RuleTestCase.java
Log:
Patch improves exception handling in test cases by allowing exceptions to be handled directly by jUnit thus preseving the stack. Submitted by Christopher Lenz
Revision Changes Path
1.7 +17 -37 jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java
Index: BeanPropertySetterRuleTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- BeanPropertySetterRuleTestCase.java 17 Jul 2002 16:53:50 -0000 1.6
+++ BeanPropertySetterRuleTestCase.java 4 Sep 2002 18:01:16 -0000 1.7
@@ -66,6 +66,8 @@
import java.util.ArrayList;
import java.util.List;
+import org.xml.sax.SAXException;
+
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -85,7 +87,8 @@
* Simple test xml document used in the tests.
*/
protected final static String TEST_XML =
- "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha><beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>";
+ "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha>" +
+ "<beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>";
/**
@@ -150,7 +153,7 @@
* This is a general digester test but it fits into here pretty well.
* This tests that the rule calling order is properly enforced.
*/
- public void testDigesterRuleCallOrder() {
+ public void testDigesterRuleCallOrder() throws SAXException, IOException {
List callOrder = new ArrayList();
@@ -173,12 +176,7 @@
digester.addRule("root/alpha", thirdRule);
- try {
- digester.parse(xmlTestReader());
-
- } catch (Throwable t) {
- fail("Exception prevented test execution: " + t);
- }
+ digester.parse(xmlTestReader());
// we should have nine entries in our list of calls
@@ -243,7 +241,7 @@
* This is a general digester test but it fits into here pretty well.
* This tests that the body text stack is functioning correctly.
*/
- public void testDigesterBodyTextStack() {
+ public void testDigesterBodyTextStack() throws SAXException, IOException {
// use the standard rules
digester.setRules(new RulesBase());
@@ -264,12 +262,7 @@
TestRule gammaRule = new TestRule("root/gamma");
digester.addRule("root/gamma", gammaRule);
- try {
- digester.parse(xmlTestReader());
-
- } catch (Throwable t) {
- fail("Exception prevented test execution: " + t);
- }
+ digester.parse(xmlTestReader());
assertEquals(
"Root body text not set correct.",
@@ -297,13 +290,14 @@
/**
* Test that you can successfully set a given property
*/
- public void testSetGivenProperty() {
+ public void testSetGivenProperty() throws SAXException, IOException {
// use the standard rules
digester.setRules(new RulesBase());
// going to be setting properties on a SimpleTestBean
- digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean");
+ digester.addObjectCreate("root",
+ "org.apache.commons.digester.SimpleTestBean");
// we'll set property alpha with the body text of root
digester.addRule("root", new BeanPropertySetterRule("alpha"));
@@ -313,16 +307,7 @@
// we'll leave property gamma alone
-
- SimpleTestBean bean = null;
- try {
- bean = (SimpleTestBean) digester.parse(xmlTestReader());
-
- } catch (Throwable t) {
- fail("Exception prevented test execution: " + t);
- }
-
-
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
// check properties are set correctly
assertEquals(
@@ -344,25 +329,20 @@
/**
* Test that you can successfully automatically set properties.
*/
- public void testAutomaticallySetProperties() {
+ public void testAutomaticallySetProperties()
+ throws SAXException, IOException {
// need the extended rules
digester.setRules(new ExtendedBaseRules());
// going to be setting properties on a SimpleTestBean
- digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean");
+ digester.addObjectCreate("root",
+ "org.apache.commons.digester.SimpleTestBean");
// match all children of root with this rule
digester.addRule("root/?", new BeanPropertySetterRule());
- SimpleTestBean bean = null;
- try {
- bean = (SimpleTestBean) digester.parse(xmlTestReader());
-
- } catch (Throwable t) {
- fail("Exception prevented test execution: " + t);
- }
-
+ SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
// check properties are set correctly
assertEquals(
1.20 +89 -140 jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java
Index: RuleTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- RuleTestCase.java 31 Jul 2002 10:48:08 -0000 1.19
+++ RuleTestCase.java 4 Sep 2002 18:01:16 -0000 1.20
@@ -73,6 +73,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.xml.sax.SAXException;
/**
* <p>Test Case for the Digester class. These tests perform parsing of
@@ -152,7 +153,7 @@
* the stack, which should cause an appropriate Employee object to be
* returned.
*/
- public void testObjectCreate1() {
+ public void testObjectCreate1() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee",
@@ -161,11 +162,8 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
+
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
root instanceof Employee);
@@ -187,7 +185,7 @@
* returned. The processing rules will process the nested Address elements
* as well, but will not attempt to add them to the Employee.
*/
- public void testObjectCreate2() {
+ public void testObjectCreate2() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -198,11 +196,8 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
+
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
root instanceof Employee);
@@ -224,7 +219,7 @@
* returned. The processing rules will process the nested Address elements
* as well, and will add them to the owning Employee.
*/
- public void testObjectCreate3() {
+ public void testObjectCreate3() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -237,11 +232,8 @@
// Parse our test input once
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
+
validateObjectCreate3(root);
// Parse the same input again
@@ -259,7 +251,7 @@
* Same as testObjectCreate1(), except use individual call method rules
* to set the properties of the Employee.
*/
- public void testObjectCreate4() {
+ public void testObjectCreate4() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -273,11 +265,8 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
+
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
root instanceof Employee);
@@ -299,7 +288,7 @@
* a paramCount=0 (ie the body of the element is the argument of the
* method).
*/
- public void testObjectCreate5() {
+ public void testObjectCreate5() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -309,11 +298,8 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test5.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test5.xml"));
+
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
root instanceof Employee);
@@ -332,7 +318,7 @@
* It should be possible to parse the same input twice, and get trees
* of objects that are isomorphic but not be identical object instances.
*/
- public void testRepeatedParse() {
+ public void testRepeatedParse() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -345,20 +331,14 @@
// Parse our test input the first time
Object root1 = null;
- try {
- root1 = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester #1 threw Exception: " + t);
- }
+ root1 = digester.parse(getInputStream("Test1.xml"));
+
validateObjectCreate3(root1);
// Parse our test input the second time
Object root2 = null;
- try {
- root2 = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester #2 threw Exception: " + t);
- }
+ root2 = digester.parse(getInputStream("Test1.xml"));
+
validateObjectCreate3(root2);
// Make sure that it was a different root
@@ -374,7 +354,7 @@
* returned. The processing rules will process the nested Address elements
* as well, but will not attempt to add them to the Employee.
*/
- public void testRuleSet1() {
+ public void testRuleSet1() throws SAXException, IOException {
// Configure the digester as required
RuleSet rs = new TestRuleSet();
@@ -382,11 +362,7 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
@@ -409,7 +385,7 @@
/**
* Same as <code>testRuleSet1</code> except using a single namespace.
*/
- public void testRuleSet2() {
+ public void testRuleSet2() throws SAXException, IOException {
// Configure the digester as required
digester.setNamespaceAware(true);
@@ -419,11 +395,7 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test2.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test2.xml"));
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
@@ -448,7 +420,7 @@
* for employee that we should recognize, and a namespace for
* address that we should skip.
*/
- public void testRuleSet3() {
+ public void testRuleSet3() throws SAXException, IOException {
// Configure the digester as required
digester.setNamespaceAware(true);
@@ -458,11 +430,7 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test3.xml"));
- } catch (Throwable t) {
- fail("Digester threw IOException: " + t);
- }
+ root = digester.parse(getInputStream("Test3.xml"));
assertNotNull("Digester returned an object", root);
assertTrue("Digester returned an Employee",
@@ -492,7 +460,7 @@
* with the top-1 (parent) object as an argument. The three argument
* form is tested in <code>testSetTopRule2</code>.
*/
- public void testSetTopRule1() {
+ public void testSetTopRule1() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee",
@@ -505,11 +473,7 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Exception t) {
- fail("Digester threw Exception: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
validateObjectCreate3(root);
}
@@ -519,7 +483,7 @@
* Same as <code>testSetTopRule1</code> except using the three argument
* form of the SetTopRule rule.
*/
- public void testSetTopRule2() {
+ public void testSetTopRule2() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee",
@@ -533,11 +497,8 @@
// Parse our test input.
Object root = null;
- try {
- root = digester.parse(getInputStream("Test1.xml"));
- } catch (Exception t) {
- fail("Digester threw Exception: " + t);
- }
+ root = digester.parse(getInputStream("Test1.xml"));
+
validateObjectCreate3(root);
}
@@ -551,12 +512,13 @@
TestRule rule = new TestRule("Test");
digester.addRule("/root", rule);
- assertEquals("Digester is not properly on rule addition.", digester, rule.getDigester());
+ assertEquals("Digester is not properly on rule addition.",
+ digester, rule.getDigester());
}
- public void testSetNext() throws Exception {
+ public void testSetNext() throws SAXException, IOException {
Digester digester = new Digester();
digester.setRules(new ExtendedBaseRules());
digester.setValidating(false);
@@ -569,7 +531,8 @@
digester.addSetNext("!*/b/?", "setChild");
digester.addSetNext("!*/a/?", "setChild");
digester.addSetNext("!root/?", "add");
- ArrayList root = (ArrayList) digester.parse(getInputStream("Test4.xml"));
+ ArrayList root =
+ (ArrayList) digester.parse(getInputStream("Test4.xml"));
assertEquals("Wrong array size", 2, root.size());
AlphaBean one = (AlphaBean) root.get(0);
@@ -588,7 +551,7 @@
}
- public void testSetTop() throws Exception {
+ public void testSetTop() throws SAXException, IOException {
Digester digester = new Digester();
digester.setRules(new ExtendedBaseRules());
digester.setValidating(false);
@@ -602,7 +565,8 @@
digester.addSetTop("!*/a/?", "setParent");
digester.addSetRoot("!*/a", "add");
digester.addSetRoot("!*/b", "add");
- ArrayList root = (ArrayList) digester.parse(getInputStream("Test4.xml"));
+ ArrayList root =
+ (ArrayList) digester.parse(getInputStream("Test4.xml"));
assertEquals("Wrong array size", 5, root.size());
@@ -637,7 +601,7 @@
* to call any accessible method of the object on the top of the stack,
* even methods with no arguments.
*/
- public void testCallMethod() throws Exception {
+ public void testCallMethod() throws SAXException, IOException {
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
@@ -650,14 +614,8 @@
// Parse our test input
Object root1 = null;
- try {
- // an exception will be thrown if the method can't be found
- root1 = digester.parse(getInputStream("Test5.xml"));
-
- } catch (Throwable t) {
- // this means that the method can't be found and so the test fails
- fail("Digester threw Exception: " + t);
- }
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
}
@@ -666,7 +624,7 @@
* to call any accessible method of the object on the top of the stack,
* even methods with no arguments.
*/
- public void testCallMethod2() throws Exception {
+ public void testCallMethod2() throws SAXException, IOException {
//I was preparing this test case to fix another bug
// i'll uncomment it once i've fixed it
@@ -674,85 +632,71 @@
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
// try all syntax permutations
- digester.addCallMethod("employee", "setLastName", 1, new String[] {"java.lang.String"});
+ digester.addCallMethod("employee", "setLastName", 1,
+ new String[] {"java.lang.String"});
digester.addCallParam("employee/lastName", 0);
// Parse our test input
Object root1 = null;
- try {
- // an exception will be thrown if the method can't be found
- root1 = digester.parse(getInputStream("Test5.xml"));
- Employee employee = (Employee) root1;
- assertEquals("Failed to call Employee.setLastName", "Last Name", employee.getLastName());
-
- } catch (Throwable t) {
- // this means that the method can't be found and so the test fails
- fail("Digester threw Exception: " + t);
- }
+
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ Employee employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setLastName",
+ "Last Name", employee.getLastName());
digester = new Digester();
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
// try out primitive convertion
- digester.addCallMethod("employee", "setAge", 1, new Class[] {int.class});
+ digester.addCallMethod("employee", "setAge", 1,
+ new Class[] {int.class});
digester.addCallParam("employee/age", 0);
// Parse our test input
root1 = null;
- try {
- // an exception will be thrown if the method can't be found
- root1 = digester.parse(getInputStream("Test5.xml"));
- Employee employee = (Employee) root1;
- assertEquals("Failed to call Employee.setAge", 21, employee.getAge());
-
- } catch (Throwable t) {
- // this means that the method can't be found and so the test fails
- fail("Digester threw Exception: " + t);
- }
+
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setAge", 21, employee.getAge());
digester = new Digester();
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
- digester.addCallMethod("employee", "setActive", 1, new Class[] {boolean.class});
+ digester.addCallMethod("employee", "setActive", 1,
+ new Class[] {boolean.class});
digester.addCallParam("employee/active", 0);
// Parse our test input
root1 = null;
- try {
- // an exception will be thrown if the method can't be found
- root1 = digester.parse(getInputStream("Test5.xml"));
- Employee employee = (Employee) root1;
- assertEquals("Failed to call Employee.setActive", true, employee.isActive());
-
- } catch (Throwable t) {
- // this means that the method can't be found and so the test fails
- fail("Digester threw Exception: " + t);
- }
+
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setActive",
+ true, employee.isActive());
digester = new Digester();
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
- digester.addCallMethod("employee", "setSalary", 1, new Class[] {float.class});
+ digester.addCallMethod("employee", "setSalary", 1,
+ new Class[] {float.class});
digester.addCallParam("employee/salary", 0);
// Parse our test input
root1 = null;
- try {
- // an exception will be thrown if the method can't be found
- root1 = digester.parse(getInputStream("Test5.xml"));
- Employee employee = (Employee) root1;
- assertEquals("Failed to call Employee.setSalary", 1000000.0f, employee.getSalary(), 0.1f);
-
- } catch (Throwable t) {
- // this means that the method can't be found and so the test fails
- fail("Digester threw Exception: " + t);
- }
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setSalary",
+ 1000000.0f, employee.getSalary(), 0.1f);
}
/**
*/
- public void testSetCustomProperties() throws Exception {
+ public void testSetCustomProperties() throws SAXException, IOException {
Digester digester = new Digester();
@@ -779,7 +723,8 @@
digester.addSetProperties("toplevel/four", "alt-city", "city");
- ArrayList root = (ArrayList) digester.parse(getInputStream("Test7.xml"));
+ ArrayList root =
+ (ArrayList) digester.parse(getInputStream("Test7.xml"));
assertEquals("Wrong array size", 4, root.size());
@@ -789,28 +734,32 @@
obj = root.get(0);
assertTrue("(1) Should be an Address ", obj instanceof Address);
Address addressOne = (Address) obj;
- assertEquals("(1) Street attribute", "New Street", addressOne.getStreet());
+ assertEquals("(1) Street attribute", "New Street",
+ addressOne.getStreet());
assertEquals("(1) City attribute", "Las Vegas", addressOne.getCity());
assertEquals("(1) State attribute", "Nevada", addressOne.getState());
obj = root.get(1);
assertTrue("(2) Should be an Address ", obj instanceof Address);
Address addressTwo = (Address) obj;
- assertEquals("(2) Street attribute", "Old Street", addressTwo.getStreet());
+ assertEquals("(2) Street attribute", "Old Street",
+ addressTwo.getStreet());
assertEquals("(2) City attribute", "Portland", addressTwo.getCity());
assertEquals("(2) State attribute", "Oregon", addressTwo.getState());
obj = root.get(2);
assertTrue("(3) Should be an Address ", obj instanceof Address);
Address addressThree = (Address) obj;
- assertEquals("(3) Street attribute", "4th Street", addressThree.getStreet());
+ assertEquals("(3) Street attribute", "4th Street",
+ addressThree.getStreet());
assertEquals("(3) City attribute", "Dayton", addressThree.getCity());
assertEquals("(3) State attribute", "US" , addressThree.getState());
obj = root.get(3);
assertTrue("(4) Should be an Address ", obj instanceof Address);
Address addressFour = (Address) obj;
- assertEquals("(4) Street attribute", "6th Street", addressFour.getStreet());
+ assertEquals("(4) Street attribute", "6th Street",
+ addressFour.getStreet());
assertEquals("(4) City attribute", "Cleveland", addressFour.getCity());
assertEquals("(4) State attribute", "Ohio", addressFour.getState());
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>