1 17 18 19 package org.apache.commons.digester.plugins; 20 21 import java.util.List ; 22 import java.util.LinkedList ; 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 TestConfigurablePluginAttributes extends TestCase { 36 37 public TestConfigurablePluginAttributes(String name) { 38 super(name); 39 } 40 41 42 public void setUp() {} 43 44 45 public static Test suite() { 46 47 return (new TestSuite(TestConfigurablePluginAttributes.class)); 48 49 } 50 51 52 public void tearDown() {} 53 54 56 public void testDefaultBehaviour() throws Exception { 57 60 Digester digester = new Digester(); 61 digester.setNamespaceAware(true); 62 PluginRules rc = new PluginRules(); 63 digester.setRules(rc); 64 65 PluginDeclarationRule pdr = new PluginDeclarationRule(); 66 digester.addRule("root/plugin", pdr); 67 68 PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class); 69 digester.addRule("root/widget", widgetPluginRule); 70 digester.addSetNext("root/widget", "addWidget"); 71 72 PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class); 73 digester.addRule("root/gadget", gadgetPluginRule); 74 digester.addSetNext("root/gadget", "addGadget"); 75 76 MultiContainer root = new MultiContainer(); 77 digester.push(root); 78 79 try { 80 digester.parse( 81 TestAll.getInputStream(this, "test7.xml")); 82 83 } catch(Exception e) { 84 throw e; 85 } 86 87 Object child; 88 89 List widgets = root.getWidgets(); 90 assertTrue(widgets != null); 91 assertEquals(4, widgets.size()); 92 93 assertEquals(TextLabel.class, widgets.get(0).getClass()); 94 assertEquals(TextLabel.class, widgets.get(1).getClass()); 95 assertEquals(TextLabel.class, widgets.get(2).getClass()); 96 assertEquals(TextLabel.class, widgets.get(3).getClass()); 97 98 List gadgets = root.getGadgets(); 99 assertTrue(gadgets != null); 100 assertEquals(4, gadgets.size()); 101 102 assertEquals(TextLabel.class, gadgets.get(0).getClass()); 103 assertEquals(TextLabel.class, gadgets.get(1).getClass()); 104 assertEquals(TextLabel.class, gadgets.get(2).getClass()); 105 assertEquals(TextLabel.class, gadgets.get(3).getClass()); 106 } 107 108 public void testGlobalOverride() throws Exception { 109 116 Digester digester = new Digester(); 117 digester.setNamespaceAware(true); 118 PluginRules rc = new PluginRules(); 119 digester.setRules(rc); 120 121 rc.setPluginIdAttribute(null, "id"); 122 rc.setPluginClassAttribute(null, "class"); 123 124 PluginDeclarationRule pdr = new PluginDeclarationRule(); 125 digester.addRule("root/plugin", pdr); 126 127 PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class); 128 digester.addRule("root/widget", widgetPluginRule); 129 digester.addSetNext("root/widget", "addWidget"); 130 131 PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class); 132 digester.addRule("root/gadget", gadgetPluginRule); 133 digester.addSetNext("root/gadget", "addGadget"); 134 135 MultiContainer root = new MultiContainer(); 136 digester.push(root); 137 138 try { 139 digester.parse( 140 TestAll.getInputStream(this, "test7.xml")); 141 142 } catch(Exception e) { 143 throw e; 144 } 145 146 Object child; 147 148 List widgets = root.getWidgets(); 149 assertTrue(widgets != null); 150 assertEquals(4, widgets.size()); 151 152 assertEquals(Slider.class, widgets.get(0).getClass()); 153 assertEquals(Slider.class, widgets.get(1).getClass()); 154 assertEquals(Slider.class, widgets.get(2).getClass()); 155 assertEquals(Slider.class, widgets.get(3).getClass()); 156 157 List gadgets = root.getGadgets(); 158 assertTrue(gadgets != null); 159 assertEquals(4, gadgets.size()); 160 161 assertEquals(Slider.class, gadgets.get(0).getClass()); 162 assertEquals(Slider.class, gadgets.get(1).getClass()); 163 assertEquals(Slider.class, gadgets.get(2).getClass()); 164 assertEquals(Slider.class, gadgets.get(3).getClass()); 165 } 166 167 public void testInstanceOverride() throws Exception { 168 172 Digester digester = new Digester(); 173 digester.setNamespaceAware(true); 174 PluginRules rc = new PluginRules(); 175 digester.setRules(rc); 176 177 PluginDeclarationRule pdr = new PluginDeclarationRule(); 178 digester.addRule("root/plugin", pdr); 179 180 PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class); 185 widgetPluginRule.setPluginIdAttribute( 186 "http://jakarta.apache.org/digester/plugins", "id"); 187 widgetPluginRule.setPluginClassAttribute( 188 "http://jakarta.apache.org/digester/plugins", "class"); 189 digester.addRule("root/widget", widgetPluginRule); 190 digester.addSetNext("root/widget", "addWidget"); 191 192 PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class); 193 digester.addRule("root/gadget", gadgetPluginRule); 194 digester.addSetNext("root/gadget", "addGadget"); 195 196 MultiContainer root = new MultiContainer(); 197 digester.push(root); 198 199 try { 200 digester.parse( 201 TestAll.getInputStream(this, "test7.xml")); 202 } catch(Exception e) { 203 throw e; 204 } 205 206 Object child; 207 208 List widgets = root.getWidgets(); 209 assertTrue(widgets != null); 210 assertEquals(4, widgets.size()); 211 212 assertEquals(TextLabel2.class, widgets.get(0).getClass()); 213 assertEquals(TextLabel2.class, widgets.get(1).getClass()); 214 assertEquals(TextLabel2.class, widgets.get(2).getClass()); 215 assertEquals(TextLabel2.class, widgets.get(3).getClass()); 216 217 List gadgets = root.getGadgets(); 218 assertTrue(gadgets != null); 219 assertEquals(4, gadgets.size()); 220 221 assertEquals(TextLabel.class, gadgets.get(0).getClass()); 222 assertEquals(TextLabel.class, gadgets.get(1).getClass()); 223 assertEquals(TextLabel.class, gadgets.get(2).getClass()); 224 assertEquals(TextLabel.class, gadgets.get(3).getClass()); 225 } 226 227 229 public static class MultiContainer { 230 private LinkedList widgets = new LinkedList (); 231 private LinkedList gadgets = new LinkedList (); 232 233 public MultiContainer() {} 234 235 public void addWidget(Widget child) { 236 widgets.add(child); 237 } 238 239 public List getWidgets() { 240 return widgets; 241 } 242 243 public void addGadget(Widget child) { 244 gadgets.add(child); 245 } 246 247 public List getGadgets() { 248 return gadgets; 249 } 250 } 251 } 252 | Popular Tags |