1 16 package org.apache.cocoon.xml.dom; 17 18 import org.custommonkey.xmlunit.XMLTestCase; 19 import org.custommonkey.xmlunit.XMLUnit; 20 21 import javax.xml.transform.TransformerException ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.dom.DOMSource ; 24 import javax.xml.transform.stream.StreamResult ; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 37 public class DOMBuilderStreamerTestCase extends XMLTestCase { 38 39 public DOMBuilderStreamerTestCase(String name) { 40 super(name); 41 } 42 43 public void testBuilderWithOneElement() throws Exception { 44 AttributesImpl atts = new AttributesImpl (); 45 46 DOMBuilder builder = new DOMBuilder(); 47 builder.startDocument(); 48 builder.startElement("", "root", "root", atts); 49 builder.endElement("", "root", "root"); 50 builder.endDocument(); 51 52 Document document = XMLUnit.buildControlDocument("<root/>"); 53 assertXMLEqual(document, builder.getDocument()); 54 } 55 56 public void testBuilderWithMoreElements() throws Exception { 57 AttributesImpl atts = new AttributesImpl (); 58 59 DOMBuilder builder = new DOMBuilder(); 60 builder.startDocument(); 61 builder.startElement("", "root", "root", atts); 62 builder.startElement("", "node", "node", atts); 63 builder.endElement("", "node", "node"); 64 builder.startElement("", "node", "node", atts); 65 builder.endElement("", "node", "node"); 66 builder.endElement("", "root", "root"); 67 builder.endDocument(); 68 69 Document document = XMLUnit.buildControlDocument("<root><node/><node/></root>"); 70 assertXMLEqual(document, builder.getDocument()); 71 } 72 73 public void testBuilderWithText() throws Exception { 74 AttributesImpl atts = new AttributesImpl (); 75 76 DOMBuilder builder = new DOMBuilder(); 77 builder.startDocument(); 78 builder.startElement("", "root", "root", atts); 79 builder.characters("abcd".toCharArray(), 0, 4); 80 builder.endElement("", "root", "node"); 81 builder.endDocument(); 82 83 Document document = XMLUnit.buildControlDocument("<root>abcd</root>"); 84 assertXMLEqual(document, builder.getDocument()); 85 } 86 87 101 102 116 117 136 137 public void testBuilderWithComments() throws Exception { 138 AttributesImpl atts = new AttributesImpl (); 139 140 DOMBuilder builder = new DOMBuilder(); 141 builder.startDocument(); 142 builder.startElement("", "root", "root", atts); 143 builder.comment("abcd".toCharArray(), 0, 4); 144 builder.endElement("", "root", "node"); 145 builder.endDocument(); 146 147 Document document = XMLUnit.buildControlDocument("<root><!--abcd--></root>"); 148 149 assertXMLEqual(document, builder.getDocument()); 150 } 151 152 public void testBuilderWithCommentWithinDocType() throws Exception { 153 AttributesImpl atts = new AttributesImpl (); 154 155 DOMBuilder builder = new DOMBuilder(); 156 builder.startDocument(); 157 builder.startDTD("skinconfig", null, null); 158 builder.comment("abcd".toCharArray(), 0, 4); 159 builder.endDTD(); 160 builder.startElement("", "root", "root", atts); 161 builder.endElement("", "root", "node"); 162 builder.endDocument(); 163 164 Document document = XMLUnit.buildControlDocument("<!DOCTYPE skinconfig [<!--abcd-->]><root></root>"); 165 166 print(document); 167 print(builder.getDocument()); 168 169 assertXMLEqual(document, builder.getDocument()); 170 } 171 172 public final void print(Document document) { 173 TransformerFactory factory = TransformerFactory.newInstance(); 174 try 175 { 176 javax.xml.transform.Transformer serializer = factory.newTransformer(); 177 serializer.transform(new DOMSource (document), new StreamResult (System.out)); 178 System.out.println(); 179 } 180 catch (TransformerException te) 181 { 182 te.printStackTrace(); 183 } 184 } 185 186 187 public void testTestFacility() throws Exception { 188 Document document = XMLUnit.getControlParser().newDocument(); 189 Element elemA = document.createElement("root"); 190 document.appendChild(elemA); 191 192 Document oneElementDocument = XMLUnit.buildControlDocument("<root/>"); 193 assertXMLEqual(oneElementDocument, document); 194 195 document = XMLUnit.getControlParser().newDocument(); 196 elemA = document.createElement("node"); 197 document.appendChild(elemA); 198 199 oneElementDocument = XMLUnit.buildControlDocument("<root/>"); 200 assertXMLNotEqual(oneElementDocument, document); 201 } 202 203 public void testStreamer() throws Exception { 204 205 Document document = XMLUnit.getControlParser().newDocument(); 206 Element elemA = document.createElement("root"); 207 document.appendChild(elemA); 208 209 Element elemB = document.createElement("node"); 210 elemA.appendChild(elemB); 211 212 elemB = document.createElement("node"); 213 elemA.appendChild(elemB); 214 215 DOMBuilder builder = new DOMBuilder(); 216 DOMStreamer streamer = new DOMStreamer(builder); 217 218 streamer.stream(document); 219 220 document = builder.getDocument(); 221 222 Document moreElementDocument = XMLUnit.buildControlDocument("<root><node/><node/></root>"); 223 assertXMLEqual(moreElementDocument, document); 224 } 225 226 248 } 249 | Popular Tags |