1 16 17 18 package org.apache.commons.digester; 19 20 21 import java.io.IOException ; 22 import java.io.StringReader ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.xml.sax.SAXException ; 29 30 34 public class OverlappingCallMethodRuleTestCase extends TestCase { 35 36 38 39 44 public OverlappingCallMethodRuleTestCase(String name) { 45 46 super(name); 47 48 } 49 50 51 53 54 57 public void setUp() { 58 } 59 60 61 64 public static Test suite() { 65 return (new TestSuite(OverlappingCallMethodRuleTestCase.class)); 66 } 67 68 69 72 public void tearDown() { 73 } 74 75 76 77 String itemId; 78 String itemName; 79 80 public void setItemId(String id) { itemId = id; } 81 public void setItemName(String name) { itemName = name; } 82 83 85 86 public void testItem1() throws SAXException , IOException { 87 StringBuffer input = new StringBuffer (); 88 input.append("<root>"); 89 input.append(" <item id='1'>anitem</item>"); 90 input.append("</root>"); 91 92 Digester digester = new Digester(); 93 94 digester.addCallMethod("root/item", "setItemId", 1); 95 digester.addCallParam("root/item", 0, "id"); 96 digester.addCallMethod("root/item", "setItemName", 1); 97 digester.addCallParam("root/item", 0); 98 99 this.itemId = null; 100 this.itemName = null; 101 digester.push(this); 102 digester.parse(new StringReader (input.toString())); 103 104 assertEquals("1", this.itemId); 105 assertEquals("anitem", this.itemName); 106 } 107 108 public void testItem2() throws SAXException , IOException { 109 StringBuffer input = new StringBuffer (); 110 input.append("<root>"); 111 input.append(" <item id='1'>anitem</item>"); 112 input.append("</root>"); 113 114 Digester digester = new Digester(); 115 116 digester.addCallMethod("root/item", "setItemName", 1); 117 digester.addCallParam("root/item", 0); 118 digester.addCallMethod("root/item", "setItemId", 1); 119 digester.addCallParam("root/item", 0, "id"); 120 121 this.itemId = null; 122 this.itemName = null; 123 digester.push(this); 124 digester.parse(new StringReader (input.toString())); 125 126 assertEquals("1", this.itemId); 127 assertEquals("anitem", this.itemName); 128 } 129 130 public void testItem3() throws SAXException , IOException { 131 StringBuffer input = new StringBuffer (); 132 input.append("<root>"); 133 input.append(" <item>1</item>"); 134 input.append("</root>"); 135 136 Digester digester = new Digester(); 137 138 digester.addCallMethod("root/item", "setItemId", 1); 139 digester.addCallParam("root/item", 0); 140 digester.addCallMethod("root/item", "setItemName", 1); 141 digester.addCallParam("root/item", 0); 142 143 this.itemId = null; 144 this.itemName = null; 145 digester.push(this); 146 digester.parse(new StringReader (input.toString())); 147 148 assertEquals("1", this.itemId); 149 assertEquals("1", this.itemName); 150 } 151 152 173 174 public void testItem4() throws SAXException , IOException { 175 StringBuffer input = new StringBuffer (); 176 input.append("<root>"); 177 input.append(" <item>"); 178 input.append(" <id value='1'/>"); 179 input.append(" <name value='name'/>"); 180 input.append(" </item>"); 181 input.append("</root>"); 182 183 Digester digester = new Digester(); 184 185 digester.addCallMethod("root/item", "setItemId", 1); 186 digester.addCallParam("root/item/id", 0, "value"); 187 digester.addCallMethod("root/item", "setItemName", 1); 188 digester.addCallParam("root/item/name", 0, "value"); 189 190 this.itemId = null; 191 this.itemName = null; 192 digester.push(this); 193 digester.parse(new StringReader (input.toString())); 194 195 199 assertEquals(null, this.itemId); 201 assertEquals("name", this.itemName); 202 } 203 204 210 public void testWildcard1() throws SAXException , IOException { 211 StringBuffer input = new StringBuffer (); 212 input.append("<box id='A1'>"); 213 input.append(" <box id='B1'>"); 214 input.append(" <box id='C1'/>"); 215 input.append(" <box id='C2'/>"); 216 input.append(" </box>"); 217 input.append("</box>"); 218 219 Digester digester = new Digester(); 220 221 digester.addObjectCreate("*/box", Box.class); 222 digester.addCallMethod("*/box", "setId", 1); 223 digester.addCallParam("*/box", 0, "id"); 224 digester.addSetNext("*/box", "addChild"); 225 226 Box root = new Box(); 227 root.setId("root"); 228 digester.push(root); 229 digester.parse(new StringReader (input.toString())); 230 231 String ids = root.getIds(); 233 assertEquals("root A1 B1 C1 C2", ids); 234 } 235 236 242 public void testWildcard2() throws SAXException , IOException { 243 StringBuffer input = new StringBuffer (); 244 input.append("<box>A1"); 245 input.append(" <box>B1"); 246 input.append(" <box>C1</box>"); 247 input.append(" <box>C2</box>"); 248 input.append(" </box>"); 249 input.append("</box>"); 250 251 Digester digester = new Digester(); 252 253 digester.addObjectCreate("*/box", Box.class); 254 digester.addCallMethod("*/box", "setId", 1); 255 digester.addCallParam("*/box", 0); 256 digester.addSetNext("*/box", "addChild"); 257 258 Box root = new Box(); 259 root.setId("root"); 260 digester.push(root); 261 digester.parse(new StringReader (input.toString())); 262 263 String ids = root.getIds(); 265 assertEquals("root A1 B1 C1 C2", ids); 266 } 267 } 268 269 | Popular Tags |