1 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 34 35 public class TestRecursion extends TestCase { 36 37 public TestRecursion(String name) { 38 super(name); 39 } 40 41 42 public void setUp() {} 43 44 45 public static Test suite() { 46 47 return (new TestSuite(TestRecursion.class)); 48 49 } 50 51 52 public void tearDown() {} 53 54 56 public void testRecursiveRules() throws Exception { 57 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 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 } 100 | Popular Tags |