001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml.dom;
018:
019: import org.custommonkey.xmlunit.XMLTestCase;
020: import org.custommonkey.xmlunit.XMLUnit;
021:
022: import javax.xml.transform.TransformerException;
023: import javax.xml.transform.TransformerFactory;
024: import javax.xml.transform.dom.DOMSource;
025: import javax.xml.transform.stream.StreamResult;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import org.xml.sax.helpers.AttributesImpl;
031:
032: /**
033: * Testcase for DOMStreamer and DOMBuilder.
034: *
035: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
036: * @version CVS $Id: DOMBuilderStreamerTestCase.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class DOMBuilderStreamerTestCase extends XMLTestCase {
039:
040: public DOMBuilderStreamerTestCase(String name) {
041: super (name);
042: }
043:
044: public void testBuilderWithOneElement() throws Exception {
045: AttributesImpl atts = new AttributesImpl();
046:
047: DOMBuilder builder = new DOMBuilder();
048: builder.startDocument();
049: builder.startElement("", "root", "root", atts);
050: builder.endElement("", "root", "root");
051: builder.endDocument();
052:
053: Document document = XMLUnit.buildControlDocument("<root/>");
054: assertXMLEqual(document, builder.getDocument());
055: }
056:
057: public void testBuilderWithMoreElements() throws Exception {
058: AttributesImpl atts = new AttributesImpl();
059:
060: DOMBuilder builder = new DOMBuilder();
061: builder.startDocument();
062: builder.startElement("", "root", "root", atts);
063: builder.startElement("", "node", "node", atts);
064: builder.endElement("", "node", "node");
065: builder.startElement("", "node", "node", atts);
066: builder.endElement("", "node", "node");
067: builder.endElement("", "root", "root");
068: builder.endDocument();
069:
070: Document document = XMLUnit
071: .buildControlDocument("<root><node/><node/></root>");
072: assertXMLEqual(document, builder.getDocument());
073: }
074:
075: public void testBuilderWithText() throws Exception {
076: AttributesImpl atts = new AttributesImpl();
077:
078: DOMBuilder builder = new DOMBuilder();
079: builder.startDocument();
080: builder.startElement("", "root", "root", atts);
081: builder.characters("abcd".toCharArray(), 0, 4);
082: builder.endElement("", "root", "node");
083: builder.endDocument();
084:
085: Document document = XMLUnit
086: .buildControlDocument("<root>abcd</root>");
087: assertXMLEqual(document, builder.getDocument());
088: }
089:
090: /*public void testBuilderWithNS() throws Exception {
091: AttributesImpl atts = new AttributesImpl();
092:
093: DOMBuilder builder = new DOMBuilder();
094: builder.startDocument();
095: builder.startPrefixMapping("", "http://xml.apache.org");
096: builder.startElement("", "root", "root", atts);
097: builder.endElement("", "node", "node");
098: builder.endPrefixMapping("");
099: builder.endDocument();
100:
101: Document document = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"/>");
102: assertXMLEqual(document, builder.getDocument());
103: }*/
104:
105: /*public void testBuilderWithPrefix() throws Exception {
106: AttributesImpl atts = new AttributesImpl();
107:
108: DOMBuilder builder = new DOMBuilder();
109: builder.startDocument();
110: builder.startPrefixMapping("bla", "http://xml.apache.org");
111: builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
112: builder.endElement("http://xml.apache.org", "root", "bla:root");
113: builder.endPrefixMapping("bla");
114: builder.endDocument();
115:
116: Document document = XMLUnit.buildControlDocument("<bla:root xmlns:bla=\"http://xml.apache.org\"/>");
117: assertXMLEqual(document, builder.getDocument());
118: }*/
119:
120: /*public void testBuilderWithNSError() throws Exception {
121: AttributesImpl atts = new AttributesImpl();
122:
123: DOMBuilder builder = new DOMBuilder();
124:
125: try {
126: builder.startDocument();
127: builder.startPrefixMapping("bla", "http://xml.apache.org");
128: atts.addAttribute( "", "bla", "xmlns:bla", "CDATA", "http://xml.apache.org");
129: builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
130: builder.endElement("http://xml.apache.org", "root", "bla:root");
131: builder.endPrefixMapping("bla");
132: builder.endDocument();
133:
134: fail("DOMBuilder should throw exception because of permitted attribute");
135: } catch (Exception e) {
136: // nothing
137: }
138: }*/
139:
140: public void testBuilderWithComments() throws Exception {
141: AttributesImpl atts = new AttributesImpl();
142:
143: DOMBuilder builder = new DOMBuilder();
144: builder.startDocument();
145: builder.startElement("", "root", "root", atts);
146: builder.comment("abcd".toCharArray(), 0, 4);
147: builder.endElement("", "root", "node");
148: builder.endDocument();
149:
150: Document document = XMLUnit
151: .buildControlDocument("<root><!--abcd--></root>");
152:
153: assertXMLEqual(document, builder.getDocument());
154: }
155:
156: public void testBuilderWithCommentWithinDocType() throws Exception {
157: AttributesImpl atts = new AttributesImpl();
158:
159: DOMBuilder builder = new DOMBuilder();
160: builder.startDocument();
161: builder.startDTD("skinconfig", null, null);
162: builder.comment("abcd".toCharArray(), 0, 4);
163: builder.endDTD();
164: builder.startElement("", "root", "root", atts);
165: builder.endElement("", "root", "node");
166: builder.endDocument();
167:
168: Document document = XMLUnit
169: .buildControlDocument("<!DOCTYPE skinconfig [<!--abcd-->]><root></root>");
170:
171: print(document);
172: print(builder.getDocument());
173:
174: assertXMLEqual(document, builder.getDocument());
175: }
176:
177: public final void print(Document document) {
178: TransformerFactory factory = TransformerFactory.newInstance();
179: try {
180: javax.xml.transform.Transformer serializer = factory
181: .newTransformer();
182: serializer.transform(new DOMSource(document),
183: new StreamResult(System.out));
184: System.out.println();
185: } catch (TransformerException te) {
186: te.printStackTrace();
187: }
188: }
189:
190: public void testTestFacility() throws Exception {
191: Document document = XMLUnit.getControlParser().newDocument();
192: Element elemA = document.createElement("root");
193: document.appendChild(elemA);
194:
195: Document oneElementDocument = XMLUnit
196: .buildControlDocument("<root/>");
197: assertXMLEqual(oneElementDocument, document);
198:
199: document = XMLUnit.getControlParser().newDocument();
200: elemA = document.createElement("node");
201: document.appendChild(elemA);
202:
203: oneElementDocument = XMLUnit.buildControlDocument("<root/>");
204: assertXMLNotEqual(oneElementDocument, document);
205: }
206:
207: public void testStreamer() throws Exception {
208:
209: Document document = XMLUnit.getControlParser().newDocument();
210: Element elemA = document.createElement("root");
211: document.appendChild(elemA);
212:
213: Element elemB = document.createElement("node");
214: elemA.appendChild(elemB);
215:
216: elemB = document.createElement("node");
217: elemA.appendChild(elemB);
218:
219: DOMBuilder builder = new DOMBuilder();
220: DOMStreamer streamer = new DOMStreamer(builder);
221:
222: streamer.stream(document);
223:
224: document = builder.getDocument();
225:
226: Document moreElementDocument = XMLUnit
227: .buildControlDocument("<root><node/><node/></root>");
228: assertXMLEqual(moreElementDocument, document);
229: }
230:
231: /*public void testStreamerWithNS() throws Exception {
232:
233: Document document = XMLUnit.getControlParser().newDocument();
234: Element elemA = document.createElementNS("http://xml.apache.org", "root");
235: document.appendChild(elemA);
236:
237: Element elemB = document.createElementNS("http://xml.apache.org", "node");
238: elemA.appendChild(elemB);
239:
240: elemB = document.createElementNS("http://xml.apache.org", "node");
241: elemA.appendChild(elemB);
242:
243: DOMBuilder builder = new DOMBuilder();
244: DOMStreamer streamer = new DOMStreamer(builder);
245:
246: streamer.stream(document);
247:
248: document = builder.getDocument();
249:
250: Document moreElementDocument = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"><node/><node/></root>");
251: assertXMLEqual(moreElementDocument, document);
252: }*/
253: }
|