1 package com.thoughtworks.xstream.xml.dom; 2 3 import com.thoughtworks.xstream.xml.AbstractXMLReaderTest; 4 import com.thoughtworks.xstream.xml.XMLReader; 5 import org.w3c.dom.Document; 6 import org.w3c.dom.Element; 7 8 import javax.xml.parsers.DocumentBuilder; 9 import javax.xml.parsers.DocumentBuilderFactory; 10 import java.io.ByteArrayInputStream; 11 12 public class DomXMLReaderTest extends AbstractXMLReaderTest { 13 14 // factory method 15 protected XMLReader createReader(String xml) throws Exception { 16 return new DomXMLReader(buildDocument(xml)); 17 } 18 19 private Document buildDocument(String xml) throws Exception { 20 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 21 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 22 ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes()); 23 Document document = documentBuilder.parse(inputStream); 24 return document; 25 } 26 27 public void testCanReadFromElementOfLargerDocument() throws Exception { 28 Document document = buildDocument("" + 29 "<big>" + 30 " <small>" + 31 " <tiny/>" + 32 " </small>" + 33 " <small-two>" + 34 " </small-two>" + 35 "</big>"); 36 Element small = (Element) document.getDocumentElement().getElementsByTagName("small").item(0); 37 38 XMLReader xmlReader = new DomXMLReader(small); 39 assertEquals("small", xmlReader.name()); 40 xmlReader.child(0); 41 assertEquals("tiny", xmlReader.name()); 42 } 43 44 }

This page was automatically generated by Maven