KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestDefaultPlugin.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 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.impl.NoOpLog;
31
32 /**
33  * Test cases for the use of default plugin classes.
34  */

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

59     public void testDefaultPlugins1() throws Exception JavaDoc {
60         // * tests that when a PluginCreateRule is defined with a default
61
// class, that the default class is instantiated when no class
62
// or id is specified in the xml file.
63
Digester digester = new Digester();
64         PluginRules rc = new PluginRules();
65         digester.setRules(rc);
66         
67         PluginCreateRule pcr = new PluginCreateRule(Widget.class, TextLabel.class);
68         digester.addRule("root/widget", pcr);
69         digester.addSetNext("root/widget", "addChild");
70
71         Container root = new Container();
72         digester.push(root);
73         
74         try {
75             digester.parse(
76                 TestAll.getInputStream(this, "test2.xml"));
77         }
78         catch(Exception JavaDoc e) {
79             throw e;
80         }
81         
82         Object JavaDoc child;
83         List JavaDoc children = root.getChildren();
84         assertTrue(children != null);
85         assertEquals(3, children.size());
86         
87         child = children.get(0);
88         assertTrue(child != null);
89         assertEquals(TextLabel.class, child.getClass());
90         TextLabel label1 = (TextLabel) child;
91         assertEquals("label1", label1.getLabel());
92         
93         child = children.get(1);
94         assertTrue(child != null);
95         assertEquals(TextLabel.class, child.getClass());
96         TextLabel label2 = (TextLabel) child;
97         assertEquals("label2", label2.getLabel());
98         
99         child = children.get(2);
100         assertTrue(child != null);
101         assertEquals(Slider.class, child.getClass());
102         Slider slider1 = (Slider) child;
103         assertEquals("slider1", slider1.getLabel());
104     }
105
106     public void testDefaultPlugins2() throws Exception JavaDoc {
107         // * tests that when there is no default plugin, it is an error
108
// not to have one of plugin-class or plugin-id specified
109
Digester digester = new Digester();
110         PluginRules rc = new PluginRules();
111         digester.setRules(rc);
112         
113         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
114         digester.addRule("root/widget", pcr);
115         digester.addSetNext("root/widget", "addChild");
116
117         Container root = new Container();
118         digester.push(root);
119         
120         Exception JavaDoc exception = null;
121         Log oldLog = digester.getLogger();
122         try {
123             digester.setLogger(new NoOpLog());
124             digester.parse(
125                 TestAll.getInputStream(this, "test2.xml"));
126         }
127         catch(Exception JavaDoc e) {
128             exception = e;
129         }
130         finally {
131             digester.setLogger(oldLog);
132         }
133         
134         assertTrue(exception != null);
135         assertEquals(SAXParseException JavaDoc.class, exception.getClass());
136         assertEquals(
137             PluginInvalidInputException.class,
138             ((SAXParseException JavaDoc)exception).getException().getClass());
139     }
140
141     public void testDefaultPlugins3() throws Exception JavaDoc {
142         // * tests that the default plugin must implement or extend the
143
// plugin base class.
144
Digester digester = new Digester();
145         PluginRules rc = new PluginRules();
146         digester.setRules(rc);
147
148         PluginCreateRule pcr = new PluginCreateRule(Widget.class, Object JavaDoc.class);
149         digester.addRule("root/widget", pcr);
150         digester.addSetNext("root/widget", "addChild");
151
152         Container root = new Container();
153         digester.push(root);
154         
155         Exception JavaDoc exception = null;
156         Log oldLog = digester.getLogger();
157         try {
158             digester.setLogger(new NoOpLog());
159             digester.parse(
160                 TestAll.getInputStream(this, "test2.xml"));
161         }
162         catch(Exception JavaDoc e) {
163             exception = e;
164         }
165         finally {
166             digester.setLogger(oldLog);
167         }
168         
169         assertTrue(exception != null);
170         assertEquals(SAXParseException JavaDoc.class, exception.getClass());
171         assertEquals(
172             PluginConfigurationException.class,
173             ((SAXParseException JavaDoc)exception).getException().getClass());
174     }
175 }
176
Popular Tags