01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.components;
04:
05: import java.io.*;
06: import org.w3c.dom.*;
07: import javax.xml.parsers.*;
08: import fitnesse.testutil.AbstractRegex;
09:
10: public class XmlWriterTest extends AbstractRegex {
11: private ByteArrayOutputStream output;
12:
13: private Document doc;
14:
15: static String sampleXml = null;
16:
17: static {
18: ByteArrayOutputStream out = new ByteArrayOutputStream();
19: PrintWriter writer = new PrintWriter(out);
20: writer.println("<?xml version=\"1.0\"?>");
21: writer.println("<rootElement version=\"2.0\">");
22: writer
23: .println("\t<emptytElement dire=\"straights\" strawberry=\"alarmclock\"/>");
24: writer.println("\t<fullElement>");
25: writer.println("\t\t<childElement/>");
26: writer.println("\t</fullElement>");
27: writer.println("\t<text>some text</text>");
28: writer.println("\t<cdata><![CDATA[<>&;]]></cdata>");
29: writer.println("</rootElement>");
30: writer.flush();
31: sampleXml = new String(out.toByteArray());
32: }
33:
34: public void setUp() throws Exception {
35: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
36: .newDocumentBuilder();
37: doc = builder.parse(new ByteArrayInputStream(sampleXml
38: .getBytes()));
39: output = new ByteArrayOutputStream();
40: }
41:
42: public void tearDown() throws Exception {
43: }
44:
45: public void testAll() throws Exception {
46: String results = writeXml(doc);
47:
48: assertEquals(sampleXml, results);
49: }
50:
51: private String writeXml(Document doc) throws Exception {
52: XmlWriter writer = new XmlWriter(output);
53: writer.write(doc);
54: writer.flush();
55: writer.close();
56:
57: String results = new String(output.toByteArray());
58: return results;
59: }
60: }
|