KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > xmlrules > FromXmlRuleSetTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.digester.xmlrules;
19
20
21 import java.io.StringReader JavaDoc;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.digester.Digester;
27
28 import org.xml.sax.InputSource JavaDoc;
29
30 /**
31  * Tests loading Digester rules from an XML file.
32  */

33
34 public class FromXmlRuleSetTest extends TestCase {
35
36     public FromXmlRuleSetTest(java.lang.String JavaDoc testName) {
37         super(testName);
38     }
39
40     public static void main(java.lang.String JavaDoc[] args) {
41         junit.textui.TestRunner.run(suite());
42     }
43
44     public static junit.framework.Test suite() {
45         TestSuite suite = new TestSuite(FromXmlRuleSetTest.class);
46
47         return suite;
48     }
49
50     /**
51      * Test the FromXmlRules.addRuleInstances(digester, path) method, ie
52      * test loading rules at a base position other than the root.
53      */

54      
55     public void testBasePath() throws Exception JavaDoc {
56         String JavaDoc xmlRules =
57             "<?xml version='1.0'?>" +
58             "<digester-rules>" +
59             " <pattern value='foo'>" +
60             " <call-method-rule " +
61             " methodname='setProperty' " +
62             " paramcount='0' />" +
63             " </pattern>" +
64             "</digester-rules>";
65         
66         String JavaDoc xml =
67             "<?xml version='1.0'?>" +
68             "<root>" +
69             " <foo>success</foo>" +
70             "</root>";
71             
72         // First try with no base path. The rule shouldn't match, because
73
// foo is not the root element.
74
{
75         TestObject testObject = new TestObject();
76         FromXmlRuleSet ruleset =
77             new FromXmlRuleSet(
78                 new InputSource JavaDoc(new StringReader JavaDoc(xmlRules)));
79         Digester digester = new Digester();
80         ruleset.addRuleInstances(digester);
81         
82         digester.push(testObject);
83         digester.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
84         
85         assertEquals("", testObject.getProperty());
86         }
87         
88         // Now try with a base path. The rule should now match.
89
{
90         TestObject testObject = new TestObject();
91         FromXmlRuleSet ruleset =
92             new FromXmlRuleSet(
93                 new InputSource JavaDoc(new StringReader JavaDoc(xmlRules)));
94         Digester digester = new Digester();
95         ruleset.addRuleInstances(digester, "root");
96
97         digester.push(testObject);
98         digester.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
99         
100         assertEquals("success", testObject.getProperty());
101         }
102     }
103 }
104
Popular Tags