KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > TestXmlRuleInfo


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.plugins;
19
20 import java.io.StringReader JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.digester.Digester;
27
28 /**
29  * Test cases for the declaration of custom rules for a plugin using
30  * xmlrules format files.
31  */

32
33 public class TestXmlRuleInfo extends TestCase {
34     /** Standard constructor */
35     public TestXmlRuleInfo(String JavaDoc name) {
36         super(name);
37     }
38
39     /** Set up instance variables required by this test case. */
40     public void setUp() {}
41
42     /** Return the tests included in this test suite. */
43     public static Test suite() {
44
45         return (new TestSuite(TestXmlRuleInfo.class));
46
47     }
48
49     /** Tear down instance variables required by this test case.*/
50     public void tearDown() {}
51         
52     // --------------------------------------------------------------- Test cases
53

54     public void testXmlRuleInfoExplicitFile() throws Exception JavaDoc {
55         // * tests that custom rules can be declared on a
56
// separate class by explicitly declaring a file containing
57
// the rules, using a relative or absolute path name.
58

59         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
60         input.append("<root>");
61         input.append(" <plugin");
62         input.append(" id='testobject'");
63         input.append(" class='org.apache.commons.digester.plugins.TestObject'");
64         input.append(" file='src/test/org/apache/commons/digester/plugins/xmlrules1.xml'");
65         input.append(" />");
66         input.append(" <object plugin-id='testobject'/>");
67         input.append("</root>");
68
69         Digester digester = new Digester();
70         PluginRules rc = new PluginRules();
71         digester.setRules(rc);
72         
73         PluginDeclarationRule pdr = new PluginDeclarationRule();
74         digester.addRule("root/plugin", pdr);
75         
76         PluginCreateRule pcr = new PluginCreateRule(TestObject.class);
77         digester.addRule("root/object", pcr);
78         
79         try {
80             digester.parse(new StringReader JavaDoc(input.toString()));
81         }
82         catch(Exception JavaDoc e) {
83             throw e;
84         }
85
86         Object JavaDoc root = digester.getRoot();
87         assertEquals(TestObject.class, root.getClass());
88         TestObject testObject = (TestObject) root;
89         assertEquals("xmlrules1", testObject.getValue());
90     }
91
92     public void testXmlRuleInfoExplicitResource() throws Exception JavaDoc {
93         // * tests that custom rules can be declared on a
94
// separate class by explicitly declaring the rule class.
95
// and explicitly declaring a file which is somewhere in the
96
// classpath.
97

98         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
99         input.append("<root>");
100         input.append(" <plugin");
101         input.append(" id='testobject'");
102         input.append(" class='org.apache.commons.digester.plugins.TestObject'");
103         input.append(" resource='org/apache/commons/digester/plugins/xmlrules2.xml'");
104         input.append(" />");
105         input.append(" <object plugin-id='testobject'/>");
106         input.append("</root>");
107
108         Digester digester = new Digester();
109         PluginRules rc = new PluginRules();
110         digester.setRules(rc);
111         
112         PluginDeclarationRule pdr = new PluginDeclarationRule();
113         digester.addRule("root/plugin", pdr);
114         
115         PluginCreateRule pcr = new PluginCreateRule(TestObject.class);
116         digester.addRule("root/object", pcr);
117
118         try {
119             digester.parse(new StringReader JavaDoc(input.toString()));
120         }
121         catch(Exception JavaDoc e) {
122             throw e;
123         }
124
125         Object JavaDoc root = digester.getRoot();
126         assertEquals(TestObject.class, root.getClass());
127         TestObject testObject = (TestObject) root;
128         assertEquals("xmlrules2", testObject.getValue());
129     }
130     
131     public void testXmlRuleImplicitResource() throws Exception JavaDoc {
132         // * tests that custom rules can be declared on a
133
// separate class by explicitly declaring the rule class.
134
// and explicitly declaring a file which is somewhere in the
135
// classpath.
136

137         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
138         input.append("<root>");
139         input.append(" <plugin");
140         input.append(" id='testobject'");
141         input.append(" class='org.apache.commons.digester.plugins.TestObject'");
142         input.append(" />");
143         input.append(" <object plugin-id='testobject'/>");
144         input.append("</root>");
145
146         Digester digester = new Digester();
147         PluginRules rc = new PluginRules();
148         digester.setRules(rc);
149         
150         PluginDeclarationRule pdr = new PluginDeclarationRule();
151         digester.addRule("root/plugin", pdr);
152         
153         PluginCreateRule pcr = new PluginCreateRule(TestObject.class);
154         digester.addRule("root/object", pcr);
155
156         try {
157             digester.parse(new StringReader JavaDoc(input.toString()));
158         }
159         catch(Exception JavaDoc e) {
160             throw e;
161         }
162
163         Object JavaDoc root = digester.getRoot();
164         assertEquals(TestObject.class, root.getClass());
165         TestObject testObject = (TestObject) root;
166         assertEquals("xmlrules-ruleinfo", testObject.getValue());
167     }
168 }
169
Popular Tags