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 ; 9 import javax.xml.stream.XMLOutputFactory; 10 import javax.xml.stream.XMLStreamException; 11 import javax.xml.stream.XMLStreamWriter; 12 import java.io.StringWriter ; 13 import java.io.Writer ; 14 15 18 19 public class StaxWriter2Test extends AbstractXMLWriterTest { 20 21 public static final String XML_HEADER = QNameMappedConcreteClassesTest.XML_HEADER; 22 23 private StringWriter buffer; 24 private XMLOutputFactory outputFactory; 25 private X testInput; 26 27 28 protected void setUp() throws Exception { 29 super.setUp(); 30 outputFactory = XMLOutputFactory.newInstance(); 31 buffer = new StringWriter (); 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 expected) { 42 expected = "<?xml version='1.0' encoding='utf-8'?>" + expected; assertEquals(expected, buffer.toString()); 44 } 45 46 public void testEscapesWhitespaceCharacters() { 47 } 49 50 public void testSupportsEmptyTags() { 51 writer.startNode("empty"); 53 writer.endNode(); 54 55 assertXmlProducedIs("<empty></empty>"); 56 } 57 58 public void testNamespacedXmlWithPrefix() throws Exception { 59 QNameMap qnameMap = new QNameMap(); 60 QName qname = new QName ("http://foo.com", "alias", "foo"); 61 qnameMap.registerMapping(qname, X.class); 62 63 String 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 { 69 QNameMap qnameMap = new QNameMap(); 70 QName qname = new QName ("http://foo.com", "bar"); 71 qnameMap.registerMapping(qname, X.class); 72 73 String 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 expected) throws XMLStreamException { 80 String 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 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 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 |