01: /* $Id: TestRecursion.java 471661 2006-11-06 08:09:25Z skitching $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.apache.commons.digester.plugins;
20:
21: import java.util.List;
22: import java.util.Iterator;
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 name) {
38: super (name);
39: }
40:
41: /** Set up instance variables required by this test case. */
42: public void setUp() {
43: }
44:
45: /** Return the tests included in this test suite. */
46: public static Test suite() {
47:
48: return (new TestSuite(TestRecursion.class));
49:
50: }
51:
52: /** Tear down instance variables required by this test case.*/
53: public void tearDown() {
54: }
55:
56: // --------------------------------------------------------------- Test cases
57:
58: public void testRecursiveRules() throws Exception {
59: // * tests that a rule can declare custom PluginCreateRules
60: // that allow it to plug in instances of itself below
61: // itself.
62:
63: Digester digester = new Digester();
64: PluginRules rc = new PluginRules();
65: digester.setRules(rc);
66:
67: PluginDeclarationRule pdr = new PluginDeclarationRule();
68: digester.addRule("*/plugin", pdr);
69:
70: PluginCreateRule pcr = new PluginCreateRule(Widget.class);
71: digester.addRule("root/widget", pcr);
72: digester.addSetNext("root/widget", "addChild");
73:
74: Container root = new Container();
75: digester.push(root);
76:
77: try {
78: digester.parse(TestAll.getInputStream(this , "test6.xml"));
79: } catch (Exception 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 l = c.getChildren();
89: int sum = 0;
90: for (Iterator 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: }
|