KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > dom > DOMBuilderStreamerTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.dom.DOMSource JavaDoc;
24 import javax.xml.transform.stream.StreamResult JavaDoc;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30
31 /**
32  * Testcase for DOMStreamer and DOMBuilder.
33  *
34  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
35  * @version CVS $Id: DOMBuilderStreamerTestCase.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class DOMBuilderStreamerTestCase extends XMLTestCase {
38
39     public DOMBuilderStreamerTestCase(String JavaDoc name) {
40         super(name);
41     }
42
43     public void testBuilderWithOneElement() throws Exception JavaDoc {
44         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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 JavaDoc document = XMLUnit.buildControlDocument("<root/>");
53         assertXMLEqual(document, builder.getDocument());
54     }
55
56     public void testBuilderWithMoreElements() throws Exception JavaDoc {
57         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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 JavaDoc document = XMLUnit.buildControlDocument("<root><node/><node/></root>");
70         assertXMLEqual(document, builder.getDocument());
71     }
72
73     public void testBuilderWithText() throws Exception JavaDoc {
74         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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 JavaDoc document = XMLUnit.buildControlDocument("<root>abcd</root>");
84         assertXMLEqual(document, builder.getDocument());
85     }
86
87     /*public void testBuilderWithNS() throws Exception {
88         AttributesImpl atts = new AttributesImpl();
89
90         DOMBuilder builder = new DOMBuilder();
91         builder.startDocument();
92         builder.startPrefixMapping("", "http://xml.apache.org");
93         builder.startElement("", "root", "root", atts);
94         builder.endElement("", "node", "node");
95         builder.endPrefixMapping("");
96         builder.endDocument();
97
98         Document document = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"/>");
99         assertXMLEqual(document, builder.getDocument());
100     }*/

101
102     /*public void testBuilderWithPrefix() throws Exception {
103         AttributesImpl atts = new AttributesImpl();
104
105         DOMBuilder builder = new DOMBuilder();
106         builder.startDocument();
107         builder.startPrefixMapping("bla", "http://xml.apache.org");
108         builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
109         builder.endElement("http://xml.apache.org", "root", "bla:root");
110         builder.endPrefixMapping("bla");
111         builder.endDocument();
112
113         Document document = XMLUnit.buildControlDocument("<bla:root xmlns:bla=\"http://xml.apache.org\"/>");
114         assertXMLEqual(document, builder.getDocument());
115     }*/

116
117     /*public void testBuilderWithNSError() throws Exception {
118         AttributesImpl atts = new AttributesImpl();
119
120         DOMBuilder builder = new DOMBuilder();
121
122         try {
123             builder.startDocument();
124             builder.startPrefixMapping("bla", "http://xml.apache.org");
125             atts.addAttribute( "", "bla", "xmlns:bla", "CDATA", "http://xml.apache.org");
126             builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
127             builder.endElement("http://xml.apache.org", "root", "bla:root");
128             builder.endPrefixMapping("bla");
129             builder.endDocument();
130
131             fail("DOMBuilder should throw exception because of permitted attribute");
132         } catch (Exception e) {
133             // nothing
134         }
135     }*/

136
137     public void testBuilderWithComments() throws Exception JavaDoc {
138         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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 JavaDoc document = XMLUnit.buildControlDocument("<root><!--abcd--></root>");
148
149         assertXMLEqual(document, builder.getDocument());
150     }
151
152     public void testBuilderWithCommentWithinDocType() throws Exception JavaDoc {
153         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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 JavaDoc 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 JavaDoc document) {
173         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
174         try
175         {
176           javax.xml.transform.Transformer JavaDoc serializer = factory.newTransformer();
177           serializer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(System.out));
178           System.out.println();
179         }
180         catch (TransformerException JavaDoc te)
181         {
182           te.printStackTrace();
183         }
184     }
185
186
187     public void testTestFacility() throws Exception JavaDoc {
188         Document JavaDoc document = XMLUnit.getControlParser().newDocument();
189         Element JavaDoc elemA = document.createElement("root");
190         document.appendChild(elemA);
191
192         Document JavaDoc 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 JavaDoc {
204
205         Document JavaDoc document = XMLUnit.getControlParser().newDocument();
206         Element JavaDoc elemA = document.createElement("root");
207         document.appendChild(elemA);
208
209         Element JavaDoc 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 JavaDoc moreElementDocument = XMLUnit.buildControlDocument("<root><node/><node/></root>");
223         assertXMLEqual(moreElementDocument, document);
224     }
225
226     /*public void testStreamerWithNS() throws Exception {
227
228         Document document = XMLUnit.getControlParser().newDocument();
229         Element elemA = document.createElementNS("http://xml.apache.org", "root");
230         document.appendChild(elemA);
231
232         Element elemB = document.createElementNS("http://xml.apache.org", "node");
233         elemA.appendChild(elemB);
234
235         elemB = document.createElementNS("http://xml.apache.org", "node");
236         elemA.appendChild(elemB);
237
238         DOMBuilder builder = new DOMBuilder();
239         DOMStreamer streamer = new DOMStreamer(builder);
240
241         streamer.stream(document);
242     
243         document = builder.getDocument();
244     
245         Document moreElementDocument = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"><node/><node/></root>");
246         assertXMLEqual(moreElementDocument, document);
247     }*/

248 }
249
Popular Tags