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 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.impl.NoOpLog; 31 32 35 36 import org.xml.sax.SAXParseException ; 37 38 public class TestDefaultPlugin extends TestCase { 39 40 public TestDefaultPlugin(String name) { 41 super(name); 42 } 43 44 45 public void setUp() {} 46 47 48 public static Test suite() { 49 50 return (new TestSuite(TestDefaultPlugin.class)); 51 52 } 53 54 55 public void tearDown() {} 56 57 59 public void testDefaultPlugins1() throws Exception { 60 Digester digester = new Digester(); 64 PluginRules rc = new PluginRules(); 65 digester.setRules(rc); 66 67 PluginCreateRule pcr = new PluginCreateRule(Widget.class, TextLabel.class); 68 digester.addRule("root/widget", pcr); 69 digester.addSetNext("root/widget", "addChild"); 70 71 Container root = new Container(); 72 digester.push(root); 73 74 try { 75 digester.parse( 76 TestAll.getInputStream(this, "test2.xml")); 77 } 78 catch(Exception e) { 79 throw e; 80 } 81 82 Object child; 83 List children = root.getChildren(); 84 assertTrue(children != null); 85 assertEquals(3, children.size()); 86 87 child = children.get(0); 88 assertTrue(child != null); 89 assertEquals(TextLabel.class, child.getClass()); 90 TextLabel label1 = (TextLabel) child; 91 assertEquals("label1", label1.getLabel()); 92 93 child = children.get(1); 94 assertTrue(child != null); 95 assertEquals(TextLabel.class, child.getClass()); 96 TextLabel label2 = (TextLabel) child; 97 assertEquals("label2", label2.getLabel()); 98 99 child = children.get(2); 100 assertTrue(child != null); 101 assertEquals(Slider.class, child.getClass()); 102 Slider slider1 = (Slider) child; 103 assertEquals("slider1", slider1.getLabel()); 104 } 105 106 public void testDefaultPlugins2() throws Exception { 107 Digester digester = new Digester(); 110 PluginRules rc = new PluginRules(); 111 digester.setRules(rc); 112 113 PluginCreateRule pcr = new PluginCreateRule(Widget.class); 114 digester.addRule("root/widget", pcr); 115 digester.addSetNext("root/widget", "addChild"); 116 117 Container root = new Container(); 118 digester.push(root); 119 120 Exception exception = null; 121 Log oldLog = digester.getLogger(); 122 try { 123 digester.setLogger(new NoOpLog()); 124 digester.parse( 125 TestAll.getInputStream(this, "test2.xml")); 126 } 127 catch(Exception e) { 128 exception = e; 129 } 130 finally { 131 digester.setLogger(oldLog); 132 } 133 134 assertTrue(exception != null); 135 assertEquals(SAXParseException .class, exception.getClass()); 136 assertEquals( 137 PluginInvalidInputException.class, 138 ((SAXParseException )exception).getException().getClass()); 139 } 140 141 public void testDefaultPlugins3() throws Exception { 142 Digester digester = new Digester(); 145 PluginRules rc = new PluginRules(); 146 digester.setRules(rc); 147 148 PluginCreateRule pcr = new PluginCreateRule(Widget.class, Object .class); 149 digester.addRule("root/widget", pcr); 150 digester.addSetNext("root/widget", "addChild"); 151 152 Container root = new Container(); 153 digester.push(root); 154 155 Exception exception = null; 156 Log oldLog = digester.getLogger(); 157 try { 158 digester.setLogger(new NoOpLog()); 159 digester.parse( 160 TestAll.getInputStream(this, "test2.xml")); 161 } 162 catch(Exception e) { 163 exception = e; 164 } 165 finally { 166 digester.setLogger(oldLog); 167 } 168 169 assertTrue(exception != null); 170 assertEquals(SAXParseException .class, exception.getClass()); 171 assertEquals( 172 PluginConfigurationException.class, 173 ((SAXParseException )exception).getException().getClass()); 174 } 175 } 176 | Popular Tags |