1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.StringReader ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.DocumentFragment ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.xml.sax.SAXException ; 41 42 43 49 50 public class NodeCreateRuleTestCase extends TestCase { 51 52 53 55 58 protected final static String TEST_XML = 59 "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha>" + 60 "<beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>"; 61 62 63 66 protected Digester digester = null; 67 68 69 71 72 77 public NodeCreateRuleTestCase(String name) { 78 79 super(name); 80 81 } 82 83 84 86 87 90 public void setUp() { 91 92 digester = new Digester(); 93 94 } 95 96 97 100 public static Test suite() { 101 102 return (new TestSuite(NodeCreateRuleTestCase.class)); 103 104 } 105 106 107 110 public void tearDown() { 111 112 digester = null; 113 114 } 115 116 117 118 120 121 125 public void testInvalidNodeTypes() 126 throws SAXException , ParserConfigurationException , IOException { 127 128 try { 129 Rule rule = new NodeCreateRule(Node.ATTRIBUTE_NODE); 130 fail("IllegalArgumentException expected for type ATTRIBUTE_NODE"); 131 } catch (IllegalArgumentException iae) { 132 } 134 try { 135 Rule rule = new NodeCreateRule(Node.CDATA_SECTION_NODE); 136 fail("IllegalArgumentException expected for type " + 137 "CDATA_SECTION_NODE"); 138 } catch (IllegalArgumentException iae) { 139 } 141 try { 142 Rule rule = new NodeCreateRule(Node.COMMENT_NODE); 143 fail("IllegalArgumentException expected for type COMMENT_NODE"); 144 } catch (IllegalArgumentException iae) { 145 } 147 try { 148 Rule rule = new NodeCreateRule(Node.DOCUMENT_NODE); 149 fail("IllegalArgumentException expected for type DOCUMENT_NODE"); 150 } catch (IllegalArgumentException iae) { 151 } 153 try { 154 Rule rule = new NodeCreateRule(Node.DOCUMENT_TYPE_NODE); 155 fail("IllegalArgumentException expected for type " + 156 "DOCUMENT_TYPE_NODE"); 157 } catch (IllegalArgumentException iae) { 158 } 160 try { 161 Rule rule = new NodeCreateRule(Node.ENTITY_NODE); 162 fail("IllegalArgumentException expected for type ENTITY_NODE"); 163 } catch (IllegalArgumentException iae) { 164 } 166 try { 167 Rule rule = new NodeCreateRule(Node.ENTITY_REFERENCE_NODE); 168 fail("IllegalArgumentException expected for type " + 169 "ENTITY_REFERENCE_NODE"); 170 } catch (IllegalArgumentException iae) { 171 } 173 try { 174 Rule rule = new NodeCreateRule(Node.NOTATION_NODE); 175 fail("IllegalArgumentException expected for type NOTATION_NODE"); 176 } catch (IllegalArgumentException iae) { 177 } 179 try { 180 Rule rule = new NodeCreateRule(Node.PROCESSING_INSTRUCTION_NODE); 181 fail("IllegalArgumentException expected for type " + 182 "PROCESSING_INSTRUCTION_NODE"); 183 } catch (IllegalArgumentException iae) { 184 } 186 try { 187 Rule rule = new NodeCreateRule(Node.TEXT_NODE); 188 fail("IllegalArgumentException expected for type TEXT_NODE"); 189 } catch (IllegalArgumentException iae) { 190 } 192 193 } 194 195 199 public void testElement() 200 throws SAXException , ParserConfigurationException , IOException { 201 202 digester.addRule("root/alpha", new NodeCreateRule()); 203 Object result = digester.parse(new StringReader (TEST_XML)); 204 205 assertNotNull(result); 206 assertTrue(result instanceof Element ); 207 Element element = (Element )result; 208 assertEquals("alpha", element.getNodeName()); 209 assertNull(((Element )element).getLocalName()); 210 assertNull(((Element )element).getNamespaceURI()); 211 assertEquals(1, element.getChildNodes().getLength()); 212 assertEquals("ALPHA BODY", element.getFirstChild().getNodeValue()); 213 214 } 215 216 217 221 public void testDocumentFragment() 222 throws SAXException , ParserConfigurationException , IOException { 223 224 digester.addRule("root", 225 new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE)); 226 Object result = digester.parse(new StringReader (TEST_XML)); 227 228 assertNotNull(result); 229 assertTrue(result instanceof DocumentFragment ); 230 DocumentFragment fragment = (DocumentFragment )result; 231 assertEquals(4, fragment.getChildNodes().getLength()); 232 233 Node rootBody = fragment.getFirstChild(); 234 assertEquals(Node.TEXT_NODE, rootBody.getNodeType()); 235 assertEquals("ROOT BODY", rootBody.getNodeValue()); 236 237 Node alpha = fragment.getChildNodes().item(1); 238 assertEquals(Node.ELEMENT_NODE, alpha.getNodeType()); 239 assertEquals("alpha", alpha.getNodeName()); 240 assertNull(((Element )alpha).getLocalName()); 241 assertNull(((Element )alpha).getNamespaceURI()); 242 assertEquals(1, alpha.getChildNodes().getLength()); 243 assertEquals("ALPHA BODY", alpha.getFirstChild().getNodeValue()); 244 245 Node beta = fragment.getChildNodes().item(2); 246 assertEquals(Node.ELEMENT_NODE, beta.getNodeType()); 247 assertEquals("beta", beta.getNodeName()); 248 assertNull(((Element )beta).getLocalName()); 249 assertNull(((Element )beta).getNamespaceURI()); 250 assertEquals(1, beta.getChildNodes().getLength()); 251 assertEquals("BETA BODY", beta.getFirstChild().getNodeValue()); 252 253 Node gamma = fragment.getChildNodes().item(3); 254 assertEquals(Node.ELEMENT_NODE, gamma.getNodeType()); 255 assertEquals("gamma", gamma.getNodeName()); 256 assertNull(((Element )gamma).getLocalName()); 257 assertNull(((Element )gamma).getNamespaceURI()); 258 assertEquals(1, gamma.getChildNodes().getLength()); 259 assertEquals("GAMMA BODY", gamma.getFirstChild().getNodeValue()); 260 261 } 262 263 264 268 public void testNested() 269 throws SAXException , ParserConfigurationException , IOException { 270 271 digester.addObjectCreate("root", ArrayList .class); 272 digester.addRule("root/a/b", 273 new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE)); 274 digester.addSetRoot("root/a/b", "add"); 275 digester.addObjectCreate("root/b", String .class); 276 digester.addSetRoot("root/b", "add"); 277 Object result = digester.parse(getInputStream("Test4.xml")); 278 279 assertNotNull(result); 280 assertTrue(result instanceof List ); 281 List list = (List )result; 282 assertEquals(2, list.size()); 283 284 assertTrue(list.get(0) instanceof DocumentFragment ); 285 DocumentFragment fragment = (DocumentFragment )list.get(0); 286 287 assertEquals(Node.ELEMENT_NODE, 288 fragment.getFirstChild().getNodeType()); 289 Element a = (Element )fragment.getFirstChild(); 290 assertEquals("a", a.getNodeName()); 291 assertEquals(1, a.getAttributes().getLength()); 292 assertEquals("THREE", a.getAttribute("name")); 293 294 assertTrue(list.get(1) instanceof String ); 295 296 } 297 298 299 303 public void testAttributes() 304 throws SAXException , ParserConfigurationException , IOException { 305 306 digester.addRule("employee", 307 new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE)); 308 Object result = digester.parse(getInputStream("Test1.xml")); 309 310 assertNotNull(result); 311 assertTrue(result instanceof DocumentFragment ); 312 DocumentFragment fragment = (DocumentFragment )result; 313 assertEquals(2, fragment.getChildNodes().getLength()); 314 315 assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType()); 316 Element address1 = (Element )fragment.getFirstChild(); 317 assertEquals("address", address1.getNodeName()); 318 assertEquals(5, address1.getAttributes().getLength()); 319 assertEquals("home", address1.getAttribute("type")); 320 assertEquals("Home Street", address1.getAttribute("street")); 321 assertEquals("Home City", address1.getAttribute("city")); 322 assertEquals("HS", address1.getAttribute("state")); 323 assertEquals("HmZip", address1.getAttribute("zipCode")); 324 325 assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType()); 326 Element address2 = (Element )fragment.getLastChild(); 327 assertEquals("address", address2.getNodeName()); 328 assertEquals(5, address2.getAttributes().getLength()); 329 assertEquals("office", address2.getAttribute("type")); 330 assertEquals("Office Street", address2.getAttribute("street")); 331 assertEquals("Office City", address2.getAttribute("city")); 332 assertEquals("OS", address2.getAttribute("state")); 333 assertEquals("OfZip", address2.getAttribute("zipCode")); 334 335 } 336 337 338 342 public void testNamespaces() 343 throws SAXException , ParserConfigurationException , IOException { 344 345 digester.setNamespaceAware(true); 346 digester.setRuleNamespaceURI(null); 347 digester.addRule("employee", 348 new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE)); 349 Object result = digester.parse(getInputStream("Test3.xml")); 350 351 assertNotNull(result); 352 assertTrue(result instanceof DocumentFragment ); 353 DocumentFragment fragment = (DocumentFragment )result; 354 assertEquals(2, fragment.getChildNodes().getLength()); 355 356 assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType()); 357 Element address1 = (Element )fragment.getFirstChild(); 358 assertEquals("address", address1.getNodeName()); 359 assertEquals("http://jakarta.apache.org/digester/Bar", 360 address1.getNamespaceURI()); 361 assertEquals("address", address1.getLocalName()); 362 assertEquals(5, address1.getAttributes().getLength()); 363 assertEquals("home", address1.getAttribute("type")); 364 assertEquals("Home Street", address1.getAttribute("street")); 365 assertEquals("Home City", address1.getAttribute("city")); 366 assertEquals("HS", address1.getAttribute("state")); 367 assertEquals("HmZip", address1.getAttribute("zipCode")); 368 369 assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType()); 370 Element address2 = (Element )fragment.getLastChild(); 371 assertEquals("address", address2.getNodeName()); 372 assertEquals("http://jakarta.apache.org/digester/Bar", 373 address2.getNamespaceURI()); 374 assertEquals("address", address2.getLocalName()); 375 assertEquals(5, address2.getAttributes().getLength()); 376 assertEquals("office", address2.getAttribute("type")); 377 assertEquals("Office Street", address2.getAttribute("street")); 378 assertEquals("Office City", address2.getAttribute("city")); 379 assertEquals("OS", address2.getAttribute("state")); 380 assertEquals("OfZip", address2.getAttribute("zipCode")); 381 382 } 383 384 385 389 public void testImport() 390 throws SAXException , ParserConfigurationException , IOException { 391 392 digester.addRule("root", 393 new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE)); 394 Object result = digester.parse(new StringReader (TEST_XML)); 395 DocumentFragment fragment = (DocumentFragment )result; 396 397 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 398 DocumentBuilder builder = factory.newDocumentBuilder(); 399 Document doc = builder.newDocument(); 400 Node importedFragment = doc.importNode(fragment, true); 401 doc.appendChild(doc.createElement("root")); 402 doc.getFirstChild().appendChild(importedFragment); 403 404 } 405 406 407 409 410 418 protected InputStream getInputStream(String name) throws IOException { 419 420 return (this.getClass().getResourceAsStream 421 ("/org/apache/commons/digester/" + name)); 422 423 } 424 425 426 } 427 | Popular Tags |