1 17 18 19 package org.apache.commons.digester.xmlrules; 20 21 22 import java.io.InputStream ; 23 import java.io.StringReader ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.apache.commons.digester.Address; 31 import org.apache.commons.digester.Digester; 32 import org.apache.commons.digester.TestObjectCreationFactory; 33 34 import org.xml.sax.InputSource ; 35 36 42 43 public class DigesterLoaderTest extends TestCase { 44 45 public DigesterLoaderTest(java.lang.String testName) { 46 super(testName); 47 } 48 49 public static void main(java.lang.String [] args) { 50 junit.textui.TestRunner.run(suite()); 51 } 52 53 public static junit.framework.Test suite() { 54 TestSuite suite = new TestSuite(DigesterLoaderTest.class); 55 56 return suite; 57 } 58 59 64 public void testCreateDigester() throws Exception { 65 URL rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testrules.xml"); 66 URL input = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/test.xml"); 67 assertNotNull("The test could not locate testrules.xml", rules); 68 assertNotNull("The test could not locate test.xml", input); 69 Digester digester = DigesterLoader.createDigester(rules); 70 digester.push(new ArrayList ()); 71 Object root = digester.parse(input.openStream()); 72 assertEquals("[foo1 baz1 foo2, foo3 foo4]",root.toString()); 73 } 74 75 80 public void testLoad1() throws Exception { 81 ClassLoader classLoader = getClass().getClassLoader(); 82 URL rules = classLoader.getResource("org/apache/commons/digester/xmlrules/testrules.xml"); 83 URL input = classLoader.getResource("org/apache/commons/digester/xmlrules/test.xml"); 84 assertNotNull("The test could not locate testrules.xml", rules); 85 assertNotNull("The test could not locate test.xml", input); 86 Object root = DigesterLoader.load(rules, classLoader, input, new ArrayList ()); 87 if (!(root instanceof ArrayList )) { 88 fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got " + root.getClass().getName()); 89 } 90 assertEquals( "[foo1 baz1 foo2, foo3 foo4]",root.toString()); 91 92 ArrayList al = (ArrayList )root; 93 Object obj = al.get(0); 94 if (! (obj instanceof TestObject)) { 95 fail("Unexpected object returned from DigesterLoader. Expected TestObject; got " + obj.getClass().getName()); 96 } 97 TestObject to = (TestObject)obj; 98 assertEquals(new Long (555),to.getLongValue()); 99 assertEquals( "foo", to.getMapValue( "test1" ) ); 100 assertEquals( "bar", to.getMapValue( "test2" ) ); 101 } 102 103 107 public void testLoad2() throws Exception { 108 URL rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testrules.xml"); 109 InputStream input = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/test.xml").openStream(); 110 Object root = DigesterLoader.load(rules, getClass().getClassLoader(), input, new ArrayList ()); 111 if (!(root instanceof ArrayList )) { 112 fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got " + root.getClass().getName()); 113 } 114 ArrayList list = (ArrayList ) root; 115 assertEquals(root.toString(), "[foo1 baz1 foo2, foo3 foo4]"); 116 assertEquals("Wrong number of classes created", 2 , list.size()); 117 assertEquals("Pushed first", true , ((TestObject)list.get(0)).isPushed()); 118 assertEquals("Didn't push second", false , ((TestObject)list.get(1)).isPushed()); 119 assertTrue("Property was set properly", ((TestObject)list.get(0)).getProperty().equals("I am a property!") ); 120 } 121 122 123 126 public void testCircularInclude1() { 127 URL rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testCircularRules.xml"); 128 try { 129 Digester digester = DigesterLoader.createDigester(rules); 130 } catch (Exception ex) { 131 return; 132 } 133 fail("Creating a digester with circular rules should have thrown CircularIncludeException."); 134 } 135 136 137 139 public void testSetCustomProperties() throws Exception { 140 URL rules = ClassLoader.getSystemResource 141 ("org/apache/commons/digester/xmlrules/testPropertyAliasRules.xml"); 142 InputStream input = ClassLoader.getSystemResource 143 ("org/apache/commons/digester/Test7.xml").openStream(); 144 145 Object obj = DigesterLoader.load( 146 rules, 147 getClass().getClassLoader(), 148 input, 149 new ArrayList ()); 150 151 if (!(obj instanceof ArrayList )) { 152 fail( 153 "Unexpected object returned from DigesterLoader. Expected ArrayList; got " 154 + obj.getClass().getName()); 155 } 156 157 ArrayList root = (ArrayList ) obj; 158 159 assertEquals("Wrong array size", 4, root.size()); 160 161 163 obj = root.get(0); 164 assertTrue("(1) Should be an Address ", obj instanceof Address); 165 Address addressOne = (Address) obj; 166 assertEquals("(1) Street attribute", "New Street", addressOne.getStreet()); 167 assertEquals("(1) City attribute", "Las Vegas", addressOne.getCity()); 168 assertEquals("(1) State attribute", "Nevada", addressOne.getState()); 169 170 obj = root.get(1); 171 assertTrue("(2) Should be an Address ", obj instanceof Address); 172 Address addressTwo = (Address) obj; 173 assertEquals("(2) Street attribute", "Old Street", addressTwo.getStreet()); 174 assertEquals("(2) City attribute", "Portland", addressTwo.getCity()); 175 assertEquals("(2) State attribute", "Oregon", addressTwo.getState()); 176 177 obj = root.get(2); 178 assertTrue("(3) Should be an Address ", obj instanceof Address); 179 Address addressThree = (Address) obj; 180 assertEquals("(3) Street attribute", "4th Street", addressThree.getStreet()); 181 assertEquals("(3) City attribute", "Dayton", addressThree.getCity()); 182 assertEquals("(3) State attribute", "US" , addressThree.getState()); 183 184 obj = root.get(3); 185 assertTrue("(4) Should be an Address ", obj instanceof Address); 186 Address addressFour = (Address) obj; 187 assertEquals("(4) Street attribute", "6th Street", addressFour.getStreet()); 188 assertEquals("(4) City attribute", "Cleveland", addressFour.getCity()); 189 assertEquals("(4) State attribute", "Ohio", addressFour.getState()); 190 191 } 192 193 public void testFactoryCreateRule() throws Exception { 194 URL rules = ClassLoader.getSystemResource 195 ("org/apache/commons/digester/xmlrules/testfactory.xml"); 196 197 String xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>"; 198 Object obj = DigesterLoader.load( 199 rules, 200 getClass().getClassLoader(), 201 new StringReader (xml), 202 new ArrayList ()); 203 204 if (!(obj instanceof ArrayList )) { 205 fail( 206 "Unexpected object returned from DigesterLoader. Expected ArrayList; got " 207 + obj.getClass().getName()); 208 } 209 210 ArrayList list = (ArrayList ) obj; 211 212 assertEquals("List should contain only the factory object", list.size() , 1); 213 TestObjectCreationFactory factory = (TestObjectCreationFactory) list.get(0); 214 assertEquals("Object create not called(1)", factory.called , true); 215 assertEquals( 216 "Attribute not passed (1)", 217 factory.attributes.getValue("one"), 218 "good"); 219 assertEquals( 220 "Attribute not passed (2)", 221 factory.attributes.getValue("two"), 222 "bad"); 223 assertEquals( 224 "Attribute not passed (3)", 225 factory.attributes.getValue("three"), 226 "ugly"); 227 228 229 rules = ClassLoader.getSystemResource 230 ("org/apache/commons/digester/xmlrules/testfactoryignore.xml"); 231 232 xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>"; 233 try { 234 DigesterLoader.load( 235 rules, 236 getClass().getClassLoader(), 237 new StringReader (xml)); 238 } catch (Exception e) { 239 fail("This exception should have been ignored: " + e.getClass().getName()); 240 } 241 242 rules = ClassLoader.getSystemResource 243 ("org/apache/commons/digester/xmlrules/testfactorynoignore.xml"); 244 245 xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>"; 246 try { 247 DigesterLoader.load( 248 rules, 249 getClass().getClassLoader(), 250 new StringReader (xml)); 251 fail("Exception should have been propagated from create method."); 252 } catch (Exception e) { 253 254 assertEquals(org.xml.sax.SAXParseException .class, e.getClass()); 255 } 256 } 257 258 public void testCallParamRule() throws Exception { 259 260 URL rules = ClassLoader.getSystemResource 261 ("org/apache/commons/digester/xmlrules/test-call-param-rules.xml"); 262 263 String xml = "<?xml version='1.0' ?>" 264 + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>"; 265 266 CallParamTestObject testObject = new CallParamTestObject(); 267 268 DigesterLoader.load( 269 rules, 270 getClass().getClassLoader(), 271 new StringReader (xml), 272 testObject); 273 274 assertEquals("Incorrect left value", "long", testObject.getLeft()); 275 assertEquals("Incorrect middle value", "short", testObject.getMiddle()); 276 assertEquals("Incorrect right value", "", testObject.getRight()); 277 } 278 279 public void testInputSourceLoader() throws Exception { 280 String rulesXml = "<?xml version='1.0'?>" 281 + "<digester-rules>" 282 + " <pattern value='root'>" 283 + " <pattern value='foo'>" 284 + " <call-method-rule methodname='triple' paramcount='3'" 285 + " paramtypes='java.lang.String,java.lang.String,java.lang.String'/>" 286 + " <call-param-rule paramnumber='0' attrname='attr'/>" 287 + " <pattern value='bar'>" 288 + " <call-param-rule paramnumber='1' from-stack='false'/>" 289 + " </pattern>" 290 + " <pattern value='foobar'>" 291 + " <object-create-rule classname='java.lang.String'/>" 292 + " <pattern value='ping'>" 293 + " <call-param-rule paramnumber='2' from-stack='true'/>" 294 + " </pattern>" 295 + " </pattern>" 296 + " </pattern>" 297 + " </pattern>" 298 + "</digester-rules>"; 299 300 String xml = "<?xml version='1.0' ?>" 301 + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>"; 302 303 CallParamTestObject testObject = new CallParamTestObject(); 304 305 Digester digester = DigesterLoader.createDigester(new InputSource (new StringReader (rulesXml))); 306 digester.push(testObject); 307 digester.parse(new StringReader (xml)); 308 309 assertEquals("Incorrect left value", "long", testObject.getLeft()); 310 assertEquals("Incorrect middle value", "short", testObject.getMiddle()); 311 assertEquals("Incorrect right value", "", testObject.getRight()); 312 } 313 } 314 | Popular Tags |