1 16 17 18 package org.apache.commons.digester.plugins; 19 20 import java.io.StringReader ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.digester.Digester; 27 28 32 33 public class TestXmlRuleInfo extends TestCase { 34 35 public TestXmlRuleInfo(String name) { 36 super(name); 37 } 38 39 40 public void setUp() {} 41 42 43 public static Test suite() { 44 45 return (new TestSuite(TestXmlRuleInfo.class)); 46 47 } 48 49 50 public void tearDown() {} 51 52 54 public void testXmlRuleInfoExplicitFile() throws Exception { 55 59 StringBuffer input = new StringBuffer (); 60 input.append("<root>"); 61 input.append(" <plugin"); 62 input.append(" id='testobject'"); 63 input.append(" class='org.apache.commons.digester.plugins.TestObject'"); 64 input.append(" file='src/test/org/apache/commons/digester/plugins/xmlrules1.xml'"); 65 input.append(" />"); 66 input.append(" <object plugin-id='testobject'/>"); 67 input.append("</root>"); 68 69 Digester digester = new Digester(); 70 PluginRules rc = new PluginRules(); 71 digester.setRules(rc); 72 73 PluginDeclarationRule pdr = new PluginDeclarationRule(); 74 digester.addRule("root/plugin", pdr); 75 76 PluginCreateRule pcr = new PluginCreateRule(TestObject.class); 77 digester.addRule("root/object", pcr); 78 79 try { 80 digester.parse(new StringReader (input.toString())); 81 } 82 catch(Exception e) { 83 throw e; 84 } 85 86 Object root = digester.getRoot(); 87 assertEquals(TestObject.class, root.getClass()); 88 TestObject testObject = (TestObject) root; 89 assertEquals("xmlrules1", testObject.getValue()); 90 } 91 92 public void testXmlRuleInfoExplicitResource() throws Exception { 93 98 StringBuffer input = new StringBuffer (); 99 input.append("<root>"); 100 input.append(" <plugin"); 101 input.append(" id='testobject'"); 102 input.append(" class='org.apache.commons.digester.plugins.TestObject'"); 103 input.append(" resource='org/apache/commons/digester/plugins/xmlrules2.xml'"); 104 input.append(" />"); 105 input.append(" <object plugin-id='testobject'/>"); 106 input.append("</root>"); 107 108 Digester digester = new Digester(); 109 PluginRules rc = new PluginRules(); 110 digester.setRules(rc); 111 112 PluginDeclarationRule pdr = new PluginDeclarationRule(); 113 digester.addRule("root/plugin", pdr); 114 115 PluginCreateRule pcr = new PluginCreateRule(TestObject.class); 116 digester.addRule("root/object", pcr); 117 118 try { 119 digester.parse(new StringReader (input.toString())); 120 } 121 catch(Exception e) { 122 throw e; 123 } 124 125 Object root = digester.getRoot(); 126 assertEquals(TestObject.class, root.getClass()); 127 TestObject testObject = (TestObject) root; 128 assertEquals("xmlrules2", testObject.getValue()); 129 } 130 131 public void testXmlRuleImplicitResource() throws Exception { 132 137 StringBuffer input = new StringBuffer (); 138 input.append("<root>"); 139 input.append(" <plugin"); 140 input.append(" id='testobject'"); 141 input.append(" class='org.apache.commons.digester.plugins.TestObject'"); 142 input.append(" />"); 143 input.append(" <object plugin-id='testobject'/>"); 144 input.append("</root>"); 145 146 Digester digester = new Digester(); 147 PluginRules rc = new PluginRules(); 148 digester.setRules(rc); 149 150 PluginDeclarationRule pdr = new PluginDeclarationRule(); 151 digester.addRule("root/plugin", pdr); 152 153 PluginCreateRule pcr = new PluginCreateRule(TestObject.class); 154 digester.addRule("root/object", pcr); 155 156 try { 157 digester.parse(new StringReader (input.toString())); 158 } 159 catch(Exception e) { 160 throw e; 161 } 162 163 Object root = digester.getRoot(); 164 assertEquals(TestObject.class, root.getClass()); 165 TestObject testObject = (TestObject) root; 166 assertEquals("xmlrules-ruleinfo", testObject.getValue()); 167 } 168 } 169 | Popular Tags |