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 32 33 public class TestLocalRules extends TestCase { 34 35 public TestLocalRules(String name) { 36 super(name); 37 } 38 39 40 public void setUp() {} 41 42 43 public static Test suite() { 44 45 return (new TestSuite(TestLocalRules.class)); 46 47 } 48 49 50 public void tearDown() {} 51 52 54 public void testLocalRules() throws Exception { 55 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 e) { 76 throw e; 77 } 78 79 Object child; 80 List children = root.getChildren(); 81 assertTrue(children != null); 82 assertEquals(3, children.size()); 83 84 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 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 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 { 112 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 e) { 139 throw e; 140 } 141 142 Object child; 143 List children = root.getChildren(); 144 assertTrue(children != null); 145 assertEquals(3, children.size()); 146 147 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 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 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 |