1 17 18 19 package org.apache.commons.digester.plugins; 20 21 import java.util.List ; 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 33 34 public class TestInline extends TestCase { 35 36 public TestInline(String name) { 37 super(name); 38 } 39 40 41 public void setUp() {} 42 43 44 public static Test suite() { 45 46 return (new TestSuite(TestInline.class)); 47 48 } 49 50 51 public void tearDown() {} 52 53 55 public void testInlineDeclaration() throws Exception { 56 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 e) { 77 throw e; 78 } 79 80 Object child; 81 List 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 { 101 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 e) { 123 throw e; 124 } 125 126 Object child; 127 List 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 |