KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestConfigurablePluginAttributes.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 import java.util.LinkedList JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.apache.commons.digester.Digester;
29
30 /**
31  * Test cases for functionality which sets what xml attributes specify
32  * the plugin class or plugin declaration id.
33  */

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

56     public void testDefaultBehaviour() throws Exception JavaDoc {
57         // tests that by default the attributes used are
58
// named "plugin-class" and "plugin-id"
59

60         Digester digester = new Digester();
61         digester.setNamespaceAware(true);
62         PluginRules rc = new PluginRules();
63         digester.setRules(rc);
64         
65         PluginDeclarationRule pdr = new PluginDeclarationRule();
66         digester.addRule("root/plugin", pdr);
67         
68         PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class);
69         digester.addRule("root/widget", widgetPluginRule);
70         digester.addSetNext("root/widget", "addWidget");
71
72         PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class);
73         digester.addRule("root/gadget", gadgetPluginRule);
74         digester.addSetNext("root/gadget", "addGadget");
75
76         MultiContainer root = new MultiContainer();
77         digester.push(root);
78         
79         try {
80             digester.parse(
81                 TestAll.getInputStream(this, "test7.xml"));
82
83         } catch(Exception JavaDoc e) {
84             throw e;
85         }
86
87         Object JavaDoc child;
88         
89         List JavaDoc widgets = root.getWidgets();
90         assertTrue(widgets != null);
91         assertEquals(4, widgets.size());
92
93         assertEquals(TextLabel.class, widgets.get(0).getClass());
94         assertEquals(TextLabel.class, widgets.get(1).getClass());
95         assertEquals(TextLabel.class, widgets.get(2).getClass());
96         assertEquals(TextLabel.class, widgets.get(3).getClass());
97         
98         List JavaDoc gadgets = root.getGadgets();
99         assertTrue(gadgets != null);
100         assertEquals(4, gadgets.size());
101
102         assertEquals(TextLabel.class, gadgets.get(0).getClass());
103         assertEquals(TextLabel.class, gadgets.get(1).getClass());
104         assertEquals(TextLabel.class, gadgets.get(2).getClass());
105         assertEquals(TextLabel.class, gadgets.get(3).getClass());
106     }
107     
108     public void testGlobalOverride() throws Exception JavaDoc {
109         // Tests that using setDefaultPluginXXXX overrides behaviour for all
110
// PluginCreateRule instances. Also tests specifying attributes
111
// with "null" for namespace (ie attributes not in any namespace).
112
//
113
// note that in order not to screw up all other tests, we need
114
// to reset the global names after we finish here!
115

116         Digester digester = new Digester();
117         digester.setNamespaceAware(true);
118         PluginRules rc = new PluginRules();
119         digester.setRules(rc);
120
121         rc.setPluginIdAttribute(null, "id");
122         rc.setPluginClassAttribute(null, "class");
123      
124         PluginDeclarationRule pdr = new PluginDeclarationRule();
125         digester.addRule("root/plugin", pdr);
126         
127         PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class);
128         digester.addRule("root/widget", widgetPluginRule);
129         digester.addSetNext("root/widget", "addWidget");
130
131         PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class);
132         digester.addRule("root/gadget", gadgetPluginRule);
133         digester.addSetNext("root/gadget", "addGadget");
134
135         MultiContainer root = new MultiContainer();
136         digester.push(root);
137         
138         try {
139             digester.parse(
140                 TestAll.getInputStream(this, "test7.xml"));
141                 
142         } catch(Exception JavaDoc e) {
143             throw e;
144         }
145
146         Object JavaDoc child;
147         
148         List JavaDoc widgets = root.getWidgets();
149         assertTrue(widgets != null);
150         assertEquals(4, widgets.size());
151
152         assertEquals(Slider.class, widgets.get(0).getClass());
153         assertEquals(Slider.class, widgets.get(1).getClass());
154         assertEquals(Slider.class, widgets.get(2).getClass());
155         assertEquals(Slider.class, widgets.get(3).getClass());
156         
157         List JavaDoc gadgets = root.getGadgets();
158         assertTrue(gadgets != null);
159         assertEquals(4, gadgets.size());
160
161         assertEquals(Slider.class, gadgets.get(0).getClass());
162         assertEquals(Slider.class, gadgets.get(1).getClass());
163         assertEquals(Slider.class, gadgets.get(2).getClass());
164         assertEquals(Slider.class, gadgets.get(3).getClass());
165     }
166     
167     public void testInstanceOverride() throws Exception JavaDoc {
168         // Tests that using setPluginXXXX overrides behaviour for only
169
// that particular PluginCreateRule instance. Also tests that
170
// attributes can be in namespaces.
171

172         Digester digester = new Digester();
173         digester.setNamespaceAware(true);
174         PluginRules rc = new PluginRules();
175         digester.setRules(rc);
176
177         PluginDeclarationRule pdr = new PluginDeclarationRule();
178         digester.addRule("root/plugin", pdr);
179
180         // for plugins at pattern "root/widget", use xml attributes "id" and
181
// "class" in the custom namespace as the values for plugin id and
182
// class, not the default (and non-namespaced) values of
183
// "plugin-id" and "plugin-class".
184
PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class);
185         widgetPluginRule.setPluginIdAttribute(
186             "http://jakarta.apache.org/digester/plugins", "id");
187         widgetPluginRule.setPluginClassAttribute(
188             "http://jakarta.apache.org/digester/plugins", "class");
189         digester.addRule("root/widget", widgetPluginRule);
190         digester.addSetNext("root/widget", "addWidget");
191
192         PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class);
193         digester.addRule("root/gadget", gadgetPluginRule);
194         digester.addSetNext("root/gadget", "addGadget");
195
196         MultiContainer root = new MultiContainer();
197         digester.push(root);
198         
199         try {
200             digester.parse(
201                 TestAll.getInputStream(this, "test7.xml"));
202         } catch(Exception JavaDoc e) {
203             throw e;
204         }
205
206         Object JavaDoc child;
207         
208         List JavaDoc widgets = root.getWidgets();
209         assertTrue(widgets != null);
210         assertEquals(4, widgets.size());
211
212         assertEquals(TextLabel2.class, widgets.get(0).getClass());
213         assertEquals(TextLabel2.class, widgets.get(1).getClass());
214         assertEquals(TextLabel2.class, widgets.get(2).getClass());
215         assertEquals(TextLabel2.class, widgets.get(3).getClass());
216         
217         List JavaDoc gadgets = root.getGadgets();
218         assertTrue(gadgets != null);
219         assertEquals(4, gadgets.size());
220
221         assertEquals(TextLabel.class, gadgets.get(0).getClass());
222         assertEquals(TextLabel.class, gadgets.get(1).getClass());
223         assertEquals(TextLabel.class, gadgets.get(2).getClass());
224         assertEquals(TextLabel.class, gadgets.get(3).getClass());
225     }
226     
227     // inner classes used for testing
228

229     public static class MultiContainer {
230         private LinkedList JavaDoc widgets = new LinkedList JavaDoc();
231         private LinkedList JavaDoc gadgets = new LinkedList JavaDoc();
232     
233         public MultiContainer() {}
234         
235         public void addWidget(Widget child) {
236             widgets.add(child);
237         }
238     
239         public List JavaDoc getWidgets() {
240             return widgets;
241         }
242
243         public void addGadget(Widget child) {
244             gadgets.add(child);
245         }
246     
247         public List JavaDoc getGadgets() {
248             return gadgets;
249         }
250     }
251 }
252
Popular Tags