KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestLocalRules.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 defining custom rules on the plugin class itself.
31  */

32
33 public class TestLocalRules extends TestCase {
34     /** Standard constructor */
35     public TestLocalRules(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(TestLocalRules.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 testLocalRules() throws Exception JavaDoc {
55         // * tests that the plugin class can define an addRules method,
56
// which gets detected and executed.
57
Digester digester = new Digester();
58         PluginRules rc = new PluginRules();
59         digester.setRules(rc);
60         
61         PluginDeclarationRule pdr = new PluginDeclarationRule();
62         digester.addRule("root/plugin", pdr);
63         
64         PluginCreateRule pcr2 = new PluginCreateRule(Widget.class);
65         digester.addRule("root/widget", pcr2);
66         digester.addSetNext("root/widget", "addChild");
67
68         Container root = new Container();
69         digester.push(root);
70         
71         try {
72             digester.parse(
73                 TestAll.getInputStream(this, "test4a.xml"));
74         }
75         catch(Exception JavaDoc e) {
76             throw e;
77         }
78         
79         Object JavaDoc child;
80         List JavaDoc children = root.getChildren();
81         assertTrue(children != null);
82         assertEquals(3, children.size());
83         
84         // min/max rules should be in effect
85
// setproperties should be in effect
86
child = children.get(0);
87         assertTrue(child != null);
88         assertEquals(Slider.class, child.getClass());
89         Slider slider1 = (Slider) child;
90         assertEquals("slider1", slider1.getLabel());
91         assertEquals(1, slider1.getMin());
92         assertEquals(2, slider1.getMax());
93         
94         // range rules should not be in effect
95
// setproperties should be in effect
96
child = children.get(1);
97         assertTrue(child != null);
98         assertEquals(Slider.class, child.getClass());
99         Slider slider2 = (Slider) child;
100         assertEquals("slider2", slider2.getLabel());
101         assertEquals(0, slider2.getMin());
102         assertEquals(0, slider2.getMax());
103         
104         // setproperties should be working on text label
105
child = children.get(2);
106         assertTrue(child != null);
107         assertEquals(TextLabel.class, child.getClass());
108         assertEquals("text1", ((TextLabel)child).getLabel());
109     }
110     
111     public void testNonStandardLocalRules() throws Exception JavaDoc {
112         // * tests that using PluginDeclarationRule to declare an alternate
113
// rule method name invokes that alternate method instead (the
114
// input xml specifies that a method other than addRules is to
115
// be used)
116
// * tests that if a rule method is defined, then a SetProperties
117
// rule is not automatically added.
118
// * tests that a SetProperties rule applying to one class doesn't
119
// apply to different plugin classes mounted at the same rule.
120
Digester digester = new Digester();
121         PluginRules rc = new PluginRules();
122         digester.setRules(rc);
123         
124         PluginDeclarationRule pdr = new PluginDeclarationRule();
125         digester.addRule("root/plugin", pdr);
126         
127         PluginCreateRule pcr2 = new PluginCreateRule(Widget.class);
128         digester.addRule("root/widget", pcr2);
129         digester.addSetNext("root/widget", "addChild");
130
131         Container root = new Container();
132         digester.push(root);
133         
134         try {
135             digester.parse(
136                 TestAll.getInputStream(this, "test4b.xml"));
137         }
138         catch(Exception JavaDoc e) {
139             throw e;
140         }
141         
142         Object JavaDoc child;
143         List JavaDoc children = root.getChildren();
144         assertTrue(children != null);
145         assertEquals(3, children.size());
146         
147         // min/max rules should not be in effect
148
// setproperties should not be in effect
149
child = children.get(0);
150         assertTrue(child != null);
151         assertEquals(Slider.class, child.getClass());
152         Slider slider1 = (Slider) child;
153         assertEquals("nolabel", slider1.getLabel());
154         assertEquals(0, slider1.getMin());
155         assertEquals(0, slider1.getMax());
156         
157         // range rules should be in effect
158
// setproperties should not be in effect
159
child = children.get(1);
160         assertTrue(child != null);
161         assertEquals(Slider.class, child.getClass());
162         Slider slider2 = (Slider) child;
163         assertEquals("nolabel", slider2.getLabel());
164         assertEquals(10, slider2.getMin());
165         assertEquals(20, slider2.getMax());
166         
167         // setproperties should be working on text label
168
child = children.get(2);
169         assertTrue(child != null);
170         assertEquals(TextLabel.class, child.getClass());
171         assertEquals("text1", ((TextLabel)child).getLabel());
172     }
173 }
174
Popular Tags