KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestInline.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 declaration of plugin classes "inline" (ie by specifying
31  * plugin-class).
32  */

33
34 public class TestInline extends TestCase {
35     /** Standard constructor */
36     public TestInline(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(TestInline.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 testInlineDeclaration() throws Exception JavaDoc {
56         // * tests that plugins can be specified by class, and that the
57
// correct class gets loaded.
58
// * tests that autosetproperties works
59
// * tests that multiple different classes can be loaded via the
60
// same plugin rule (ie at the same pattern).
61
Digester digester = new Digester();
62         PluginRules rc = new PluginRules();
63         digester.setRules(rc);
64         
65         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
66         digester.addRule("root/widget", pcr);
67         digester.addSetNext("root/widget", "addChild");
68
69         Container root = new Container();
70         digester.push(root);
71         
72         try {
73             digester.parse(
74                 TestAll.getInputStream(this, "test1.xml"));
75         }
76         catch(Exception JavaDoc e) {
77             throw e;
78         }
79         
80         Object JavaDoc child;
81         List JavaDoc children = root.getChildren();
82         assertTrue(children != null);
83         assertEquals(2, children.size());
84         
85         child = children.get(0);
86         assertTrue(child != null);
87         assertEquals(TextLabel.class, child.getClass());
88         TextLabel label1 = (TextLabel) child;
89         assertEquals("anonymous", label1.getId());
90         assertEquals("1", label1.getLabel());
91         
92         child = children.get(1);
93         assertTrue(child != null);
94         assertEquals(TextLabel.class, child.getClass());
95         TextLabel label2 = (TextLabel) child;
96         assertEquals("L1", label2.getId());
97         assertEquals("2", label2.getLabel());
98     }
99     
100     public void testLeadingSlash() throws Exception JavaDoc {
101         // Tests that PluginRules handles patterns with a leading slash.
102
//
103
// This test doesn't really belong in this class. If a separate test
104
// case class is created for PluginRules, then this method should be
105
// moved there.
106

107         Digester digester = new Digester();
108         PluginRules rc = new PluginRules();
109         digester.setRules(rc);
110         
111         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
112         digester.addRule("/root/widget", pcr);
113         digester.addSetNext("/root/widget", "addChild");
114
115         Container root = new Container();
116         digester.push(root);
117         
118         try {
119             digester.parse(
120                 TestAll.getInputStream(this, "test1.xml"));
121         }
122         catch(Exception JavaDoc e) {
123             throw e;
124         }
125         
126         Object JavaDoc child;
127         List JavaDoc children = root.getChildren();
128         assertTrue(children != null);
129         assertEquals(2, children.size());
130         
131         child = children.get(0);
132         assertTrue(child != null);
133         assertEquals(TextLabel.class, child.getClass());
134         TextLabel label1 = (TextLabel) child;
135         assertEquals("anonymous", label1.getId());
136         assertEquals("1", label1.getLabel());
137         
138         child = children.get(1);
139         assertTrue(child != null);
140         assertEquals(TextLabel.class, child.getClass());
141         TextLabel label2 = (TextLabel) child;
142         assertEquals("L1", label2.getId());
143         assertEquals("2", label2.getLabel());
144     }
145     
146 }
147
Popular Tags