KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18
19 package org.apache.commons.digester.plugins;
20
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.digester.Digester;
28
29 /**
30  * Test cases for the declaration of custom rules for a plugin using
31  * a separate class to define the rules.
32  */

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

55     public void testRuleInfoExplicitClass() throws Exception JavaDoc {
56         // * tests that custom rules can be declared on a
57
// separate class by explicitly declaring the rule class.
58

59         Digester digester = new Digester();
60         PluginRules rc = new PluginRules();
61         digester.setRules(rc);
62         
63         PluginDeclarationRule pdr = new PluginDeclarationRule();
64         digester.addRule("root/plugin", pdr);
65         
66         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
67         digester.addRule("root/widget", pcr);
68         digester.addSetNext("root/widget", "addChild");
69
70         Container root = new Container();
71         digester.push(root);
72         
73         try {
74             digester.parse(
75                 TestAll.getInputStream(this, "test5a.xml"));
76         }
77         catch(Exception JavaDoc e) {
78             throw e;
79         }
80
81         Object JavaDoc child;
82         List JavaDoc children = root.getChildren();
83         assertTrue(children != null);
84         assertEquals(1, children.size());
85         
86         child = children.get(0);
87         assertTrue(child != null);
88         assertEquals(TextLabel2.class, child.getClass());
89         TextLabel2 label = (TextLabel2) child;
90         
91         // id should not be mapped, label should
92
assertEquals("anonymous", label.getId());
93         assertEquals("std label", label.getLabel());
94     }
95     
96     public void testRuleInfoExplicitMethod() throws Exception JavaDoc {
97         // * tests that custom rules can be declared on a
98
// separate class by explicitly declaring the rule class.
99
// and explicitly declaring the rule method name.
100

101         Digester digester = new Digester();
102         PluginRules rc = new PluginRules();
103         digester.setRules(rc);
104         
105         PluginDeclarationRule pdr = new PluginDeclarationRule();
106         digester.addRule("root/plugin", pdr);
107         
108         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
109         digester.addRule("root/widget", pcr);
110         digester.addSetNext("root/widget", "addChild");
111
112         Container root = new Container();
113         digester.push(root);
114         
115         try {
116             digester.parse(
117                 TestAll.getInputStream(this, "test5b.xml"));
118         }
119         catch(Exception JavaDoc e) {
120             throw e;
121         }
122
123         Object JavaDoc child;
124         List JavaDoc children = root.getChildren();
125         assertTrue(children != null);
126         assertEquals(1, children.size());
127         
128         child = children.get(0);
129         assertTrue(child != null);
130         assertEquals(TextLabel2.class, child.getClass());
131         TextLabel2 label = (TextLabel2) child;
132         
133         // id should not be mapped, altlabel should
134
assertEquals("anonymous", label.getId());
135         assertEquals("alt label", label.getLabel());
136     }
137     
138     public void testRuleInfoAutoDetect() throws Exception JavaDoc {
139         // * tests that custom rules can be declared on a
140
// separate class with name {plugin-class}RuleInfo,
141
// and they are automatically detected and loaded.
142

143         Digester digester = new Digester();
144         PluginRules rc = new PluginRules();
145         digester.setRules(rc);
146         
147         PluginDeclarationRule pdr = new PluginDeclarationRule();
148         digester.addRule("root/plugin", pdr);
149         
150         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
151         digester.addRule("root/widget", pcr);
152         digester.addSetNext("root/widget", "addChild");
153
154         Container root = new Container();
155         digester.push(root);
156         
157         try {
158             digester.parse(
159                 TestAll.getInputStream(this, "test5c.xml"));
160         }
161         catch(Exception JavaDoc e) {
162             throw e;
163         }
164
165         Object JavaDoc child;
166         List JavaDoc children = root.getChildren();
167         assertTrue(children != null);
168         assertEquals(1, children.size());
169         
170         child = children.get(0);
171         assertTrue(child != null);
172         assertEquals(TextLabel2.class, child.getClass());
173         TextLabel2 label = (TextLabel2) child;
174         
175         // id should not be mapped, label should
176
assertEquals("anonymous", label.getId());
177         assertEquals("std label", label.getLabel());
178     }
179 }
180
Popular Tags