001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 03. November 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.io.xml;
012:
013: import com.thoughtworks.acceptance.someobjects.X;
014: import com.thoughtworks.acceptance.someobjects.Y;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestResult;
021: import junit.framework.TestSuite;
022:
023: import org.apache.oro.text.perl.Perl5Util;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLOutputFactory;
027: import javax.xml.stream.XMLStreamException;
028: import javax.xml.stream.XMLStreamWriter;
029:
030: import java.io.StringWriter;
031:
032: public abstract class AbstractStaxWriterTest extends
033: AbstractXMLWriterTest {
034:
035: protected StringWriter buffer;
036: protected Perl5Util perlUtil;
037: protected XMLOutputFactory outputFactory;
038: private X testInput;
039:
040: protected static Test createSuite(Class test, String staxClassName) {
041: try {
042: Class.forName(staxClassName);
043: return new TestSuite(test);
044: } catch (ClassNotFoundException e) {
045: return new TestCase(test.getName() + ": not available") {
046:
047: public int countTestCases() {
048: return 1;
049: }
050:
051: public void run(TestResult result) {
052: }
053: };
054: }
055: }
056:
057: protected abstract String getXMLHeader();
058:
059: protected abstract XMLOutputFactory getOutputFactory();
060:
061: protected void setUp() throws Exception {
062: super .setUp();
063: outputFactory = getOutputFactory();
064: outputFactory
065: .setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
066: Boolean.FALSE);
067: buffer = new StringWriter();
068: writer = new StaxWriter(new QNameMap(), outputFactory
069: .createXMLStreamWriter(buffer));
070: perlUtil = new Perl5Util();
071:
072: testInput = new X();
073: testInput.anInt = 9;
074: testInput.aStr = "zzz";
075: testInput.innerObj = new Y();
076: testInput.innerObj.yField = "ooo";
077: }
078:
079: public void testNamespacedXmlWithPrefix() throws Exception {
080: QNameMap qnameMap = new QNameMap();
081: QName qname = new QName("http://foo.com", "alias", "foo");
082: qnameMap.registerMapping(qname, X.class);
083:
084: String expected = "<foo:alias xmlns:foo=\"http://foo.com\"><aStr xmlns=\"\">zzz</aStr><anInt xmlns=\"\">9</anInt><innerObj xmlns=\"\"><yField>ooo</yField></innerObj></foo:alias>";
085: marshalWithBothRepairingModes(qnameMap, expected);
086: }
087:
088: public void testNamespacedXmlWithoutPrefix() throws Exception {
089: QNameMap qnameMap = new QNameMap();
090: QName qname = new QName("http://foo.com", "bar");
091: qnameMap.registerMapping(qname, X.class);
092:
093: String expected = "<bar xmlns=\"http://foo.com\"><aStr xmlns=\"\">zzz</aStr><anInt xmlns=\"\">9</anInt><innerObj xmlns=\"\"><yField>ooo</yField></innerObj></bar>";
094: marshalWithBothRepairingModes(qnameMap, expected);
095: }
096:
097: protected void marshalWithBothRepairingModes(QNameMap qnameMap,
098: String expected) throws XMLStreamException {
099: marshall(qnameMap, true);
100: assertXmlProducedIs(expected);
101:
102: marshall(qnameMap, false);
103: assertXmlProducedIs(expected);
104: }
105:
106: protected void marshall(QNameMap qnameMap,
107: boolean repairNamespaceMode) throws XMLStreamException {
108: outputFactory.setProperty(
109: XMLOutputFactory.IS_REPAIRING_NAMESPACES,
110: repairNamespaceMode ? Boolean.TRUE : Boolean.FALSE);
111: XStream xstream = new XStream((HierarchicalStreamDriver) null);
112: buffer = new StringWriter();
113: XMLStreamWriter xmlStreamWriter = outputFactory
114: .createXMLStreamWriter(buffer);
115: xstream.marshal(testInput, new StaxWriter(qnameMap,
116: xmlStreamWriter));
117: }
118:
119: }
|