KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestRecursion.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.Iterator 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 plugins with custom rules which include PluginCreateRule
32  * instances, allowing recursive datastructures to be processed.
33  */

34
35 public class TestRecursion extends TestCase {
36     /** Standard constructor */
37     public TestRecursion(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(TestRecursion.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 testRecursiveRules() throws Exception JavaDoc {
57         // * tests that a rule can declare custom PluginCreateRules
58
// that allow it to plug in instances of itself below
59
// itself.
60

61         Digester digester = new Digester();
62         PluginRules rc = new PluginRules();
63         digester.setRules(rc);
64         
65         PluginDeclarationRule pdr = new PluginDeclarationRule();
66         digester.addRule("*/plugin", pdr);
67         
68         PluginCreateRule pcr = new PluginCreateRule(Widget.class);
69         digester.addRule("root/widget", pcr);
70         digester.addSetNext("root/widget", "addChild");
71
72         Container root = new Container();
73         digester.push(root);
74         
75         try {
76             digester.parse(
77                 TestAll.getInputStream(this, "test6.xml"));
78         }
79         catch(Exception JavaDoc e) {
80             throw e;
81         }
82         
83         int nDescendants = countWidgets(root);
84         assertEquals(10, nDescendants);
85     }
86
87     private int countWidgets(Container c) {
88         List JavaDoc l = c.getChildren();
89         int sum = 0;
90         for(Iterator JavaDoc i = l.iterator(); i.hasNext(); ) {
91             Widget w = (Widget) i.next();
92             ++sum;
93             if (w instanceof Container) {
94                 sum += countWidgets((Container) w);
95             }
96         }
97         return sum;
98     }
99 }
100
Popular Tags