KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > StaxWriter2Test


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.acceptance.QNameMappedConcreteClassesTest;
4 import com.thoughtworks.acceptance.someobjects.X;
5 import com.thoughtworks.acceptance.someobjects.Y;
6 import com.thoughtworks.xstream.XStream;
7
8 import javax.xml.namespace.QName JavaDoc;
9 import javax.xml.stream.XMLOutputFactory;
10 import javax.xml.stream.XMLStreamException;
11 import javax.xml.stream.XMLStreamWriter;
12 import java.io.StringWriter JavaDoc;
13 import java.io.Writer JavaDoc;
14
15 /*
16  * @author James Strachan
17  */

18
19 public class StaxWriter2Test extends AbstractXMLWriterTest {
20
21     public static final String JavaDoc XML_HEADER = QNameMappedConcreteClassesTest.XML_HEADER;
22
23     private StringWriter JavaDoc buffer;
24     private XMLOutputFactory outputFactory;
25     private X testInput;
26
27
28     protected void setUp() throws Exception JavaDoc {
29         super.setUp();
30         outputFactory = XMLOutputFactory.newInstance();
31         buffer = new StringWriter JavaDoc();
32         writer = new StaxWriter(new QNameMap(), outputFactory.createXMLStreamWriter(buffer));
33
34         testInput = new X();
35         testInput.anInt = 9;
36         testInput.aStr = "zzz";
37         testInput.innerObj = new Y();
38         testInput.innerObj.yField = "ooo";
39     }
40
41     protected void assertXmlProducedIs(String JavaDoc expected) {
42         expected = "<?xml version='1.0' encoding='utf-8'?>" + expected; // include header
43
assertEquals(expected, buffer.toString());
44     }
45
46     public void testEscapesWhitespaceCharacters() {
47         // overriding test in superclass... this doesn't seem to work with StaxWriter.
48
}
49
50     public void testSupportsEmptyTags() {
51         // overriding test in superclass.... StaxWriter writes <blah></blah> instead of <blah/>
52
writer.startNode("empty");
53         writer.endNode();
54
55         assertXmlProducedIs("<empty></empty>");
56     }
57
58     public void testNamespacedXmlWithPrefix() throws Exception JavaDoc {
59         QNameMap qnameMap = new QNameMap();
60         QName JavaDoc qname = new QName JavaDoc("http://foo.com", "alias", "foo");
61         qnameMap.registerMapping(qname, X.class);
62
63         String JavaDoc expected = XML_HEADER + "<foo:alias xmlns:foo=\"http://foo.com\"><aStr>zzz</aStr><anInt>9</anInt><innerObj><yField>ooo</yField></innerObj></foo:alias>";
64
65         marshalWithBothRepairingModes(qnameMap, expected);
66     }
67
68     public void testNamespacedXmlWithoutPrefix() throws Exception JavaDoc {
69         QNameMap qnameMap = new QNameMap();
70         QName JavaDoc qname = new QName JavaDoc("http://foo.com", "bar");
71         qnameMap.registerMapping(qname, X.class);
72
73         String JavaDoc expected = XML_HEADER + "<bar xmlns=\"http://foo.com\"><aStr>zzz</aStr><anInt>9</anInt><innerObj><yField>ooo</yField></innerObj></bar>";
74
75         marshalWithBothRepairingModes(qnameMap, expected);
76
77     }
78
79     protected void marshalWithBothRepairingModes(QNameMap qnameMap, String JavaDoc expected) throws XMLStreamException {
80         String JavaDoc text = marshall(qnameMap, true);
81         assertEquals("Generated XML with repairing mode: true", expected, text);
82
83         text = marshall(qnameMap, false);
84         assertEquals("Generated XML with repairing mode: false", expected, text);
85     }
86
87     protected String JavaDoc marshall(QNameMap qnameMap, boolean repairNamespaceMode) throws XMLStreamException {
88         StaxDriver staxDriver = new StaxDriver(qnameMap, repairNamespaceMode);
89         XStream xstream = new XStream(staxDriver);
90         return xstream.toXML(testInput);
91     }
92
93     public void testSupportsAttributes() {
94         // overriding test in superclass.... StaxWriter writes <blah></blah> instead of <blah/>
95
writer.startNode("person");
96         writer.addAttribute("firstname", "Joe");
97         writer.addAttribute("lastname", "Walnes");
98         writer.endNode();
99
100         assertXmlProducedIs("<person firstname=\"Joe\" lastname=\"Walnes\"></person>");
101     }
102
103 }
104
105
Popular Tags