1 17 package org.apache.commons.digester.xmlrules; 18 19 import java.io.StringReader ; 20 21 import junit.framework.TestCase; 22 23 import org.xml.sax.InputSource ; 24 25 import org.apache.commons.digester.Digester; 26 27 32 33 public class DigesterLoaderRulesTest extends TestCase { 34 35 public DigesterLoaderRulesTest(java.lang.String testName) { 36 super(testName); 37 } 38 39 40 public void testObjectParamRule() throws Exception { 41 String xmlRules = 42 "<?xml version='1.0'?>" + 43 "<digester-rules>" + 44 " <pattern value='root'>" + 45 " <pattern value='foo'>" + 46 " <call-method-rule " + 47 " methodname='duo' " + 48 " paramcount='2' " + 49 " paramtypes='java.lang.String,java.lang.String'/>" + 50 " <pattern value='bar'>" + 51 " <object-param-rule paramnumber='0' type='java.lang.String' />" + 52 " </pattern>" + 53 " <pattern value='rab'>" + 54 " <object-param-rule paramnumber='1' type='java.lang.String' value='tester.test' />" + 55 " </pattern>" + 56 " </pattern>" + 57 " </pattern>" + 58 "</digester-rules>"; 59 60 String xml = 61 "<?xml version='1.0'?>" + 62 "<root>" + 63 " <foo>" + 64 " <bar/>" + 65 " <rab/>" + 66 " </foo>" + 67 "</root>"; 68 69 CallParamTestObject testObject = new CallParamTestObject(); 70 Digester digester = DigesterLoader.createDigester(new InputSource (new StringReader (xmlRules))); 71 digester.push(testObject); 72 digester.parse(new InputSource (new StringReader (xml))); 73 assertEquals("First param", "", testObject.getLeft()); 74 assertEquals("Param with default set", "tester.test", testObject.getRight()); 75 } 76 77 78 public void testCallMethodRuleTargetOffset() throws Exception { 79 String xmlRules = 80 "<?xml version='1.0'?>" + 81 "<digester-rules>" + 82 " <pattern value='root'>" + 83 " <pattern value='call-top-object'>" + 84 " <call-method-rule " + 85 " methodname='setMiddle' " + 86 " targetoffset='0' " + 87 " paramcount='1' " + 88 " paramtypes='java.lang.String' />" + 89 " <call-param-rule paramnumber='0' />" + 90 " </pattern>" + 91 " <pattern value='call-parent-object'>" + 92 " <call-method-rule " + 93 " methodname='setLeft' " + 94 " targetoffset='1' " + 95 " paramcount='1' " + 96 " paramtypes='java.lang.String' />" + 97 " <call-param-rule paramnumber='0' />" + 98 " </pattern>" + 99 " <pattern value='call-root-object'>" + 100 " <call-method-rule " + 101 " methodname='setRight' " + 102 " targetoffset='-1' " + 103 " paramcount='1' " + 104 " paramtypes='java.lang.String' />" + 105 " <call-param-rule paramnumber='0' />" + 106 " </pattern>" + 107 " </pattern>" + 108 "</digester-rules>"; 109 110 String xml = 111 "<?xml version='1.0'?>" + 112 "<root>" + 113 " <call-top-object>DataForTheTopObject</call-top-object>" + 114 " <call-parent-object>DataForTheParentObject</call-parent-object>" + 115 " <call-root-object>DataForTheRootObject</call-root-object>" + 116 "</root>"; 117 118 CallParamTestObject testObjectA = new CallParamTestObject(); 119 CallParamTestObject testObjectB = new CallParamTestObject(); 120 CallParamTestObject testObjectC = new CallParamTestObject(); 121 Digester digester = DigesterLoader.createDigester(new InputSource (new StringReader (xmlRules))); 122 digester.push( testObjectA ); 123 digester.push( testObjectB ); 124 digester.push( testObjectC ); 125 digester.parse(new InputSource (new StringReader (xml))); 126 127 assertEquals("Top object invoked", "DataForTheTopObject", testObjectC.getMiddle()); 128 assertEquals("Parent object invoked", "DataForTheParentObject", testObjectB.getLeft()); 129 assertEquals("Root object invoked", "DataForTheRootObject", testObjectA.getRight()); 130 131 } 132 } 133 | Popular Tags |