001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 01. October 2004 by James Strachan
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.bea.xml.stream.MXParserFactory;
015: import com.bea.xml.stream.XMLOutputFactoryBase;
016: import com.thoughtworks.acceptance.someobjects.Handler;
017: import com.thoughtworks.acceptance.someobjects.Protocol;
018: import com.thoughtworks.acceptance.someobjects.WithList;
019: import com.thoughtworks.acceptance.someobjects.X;
020: import com.thoughtworks.acceptance.someobjects.Y;
021: import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
022: import com.thoughtworks.xstream.io.xml.QNameMap;
023: import com.thoughtworks.xstream.io.xml.StaxDriver;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLInputFactory;
027: import javax.xml.stream.XMLOutputFactory;
028:
029: import java.util.ArrayList;
030:
031: public class QNameMappedConcreteClassesTest extends
032: AbstractAcceptanceTest {
033:
034: public static final String XML_HEADER = "<?xml version='1.0' encoding='utf-8'?>";
035:
036: protected QNameMap qnameMap;
037: protected String namespace = getDefaultNS(WithList.class);
038:
039: public void testUsingNamespace() {
040: // lets register some qnames
041: QName qname = new QName(namespace, "withList", "w");
042: qnameMap.registerMapping(qname, WithList.class);
043:
044: WithList withList = new WithList();
045: withList.things = new ArrayList();
046:
047: String expected = ""
048: + XML_HEADER
049: + "<w:withList xmlns:w=\"java://com.thoughtworks.acceptance.someobjects\">"
050: + "<things></things>" + "</w:withList>";
051:
052: assertBothWays(withList, expected);
053: }
054:
055: public void testUsingDefaultNamespace() {
056: qnameMap.setDefaultNamespace(namespace);
057: xstream.alias("withList", WithList.class);
058:
059: WithList withList = new WithList();
060: withList.things = new ArrayList();
061:
062: String expected = XML_HEADER
063: + "<withList xmlns=\"java://com.thoughtworks.acceptance.someobjects\">"
064: + "<things></things>" + "</withList>";
065:
066: assertBothWays(withList, expected);
067: }
068:
069: public void testUsingDefaultNamespaceAndPrefix() {
070: qnameMap.setDefaultNamespace(namespace);
071: qnameMap.setDefaultPrefix("x");
072: QName qname = new QName(namespace, "withList", "x");
073: qnameMap.registerMapping(qname, WithList.class);
074:
075: WithList withList = new WithList();
076: withList.things = new ArrayList();
077:
078: String expected = XML_HEADER
079: + "<x:withList xmlns:x=\"java://com.thoughtworks.acceptance.someobjects\">"
080: + "<x:things></x:things>" + "</x:withList>";
081:
082: assertBothWays(withList, expected);
083: }
084:
085: public void testUsingDifferentNamespaces() {
086: // lets register some qnames
087: qnameMap.registerMapping(new QName(namespace, "withList", "w"),
088: WithList.class);
089: qnameMap.registerMapping(new QName("urn:foo", "things", "f"),
090: "things");
091:
092: WithList withList = new WithList();
093: withList.things = new ArrayList();
094:
095: String expected = XML_HEADER
096: + "<w:withList xmlns:w=\"java://com.thoughtworks.acceptance.someobjects\">"
097: + "<f:things xmlns:f=\"urn:foo\"></f:things>"
098: + "</w:withList>";
099:
100: assertBothWays(withList, expected);
101: }
102:
103: public void testUsingDifferentNamespacesWithAliases() {
104: xstream.alias("handler", X.class);
105: xstream.alias("protocol", Y.class);
106:
107: qnameMap.registerMapping(new QName(
108: getDefaultNS(Handler.class) + 1, "handler", "h"),
109: "handler");
110: qnameMap.registerMapping(new QName(
111: getDefaultNS(Protocol.class) + 2, "protocol", "p"),
112: "innerObj");
113:
114: X x = new X();
115: x.aStr = "foo";
116: x.anInt = 42;
117: x.innerObj = new Y();
118: x.innerObj.yField = "YField";
119:
120: String expected = XML_HEADER
121: + "<h:handler xmlns:h=\"java://com.thoughtworks.acceptance.someobjects1\">"
122: + "<aStr>foo</aStr>"
123: + "<anInt>42</anInt>"
124: + "<p:protocol xmlns:p=\"java://com.thoughtworks.acceptance.someobjects2\">"
125: + "<yField>YField</yField>" + "</p:protocol>"
126: + "</h:handler>";
127:
128: assertBothWays(x, expected);
129: }
130:
131: protected HierarchicalStreamDriver createDriver() {
132: System.setProperty(XMLInputFactory.class.getName(),
133: MXParserFactory.class.getName());
134: System.setProperty(XMLOutputFactory.class.getName(),
135: XMLOutputFactoryBase.class.getName());
136: // careful, called from inside base class constructor
137: qnameMap = new QNameMap();
138: return new StaxDriver(qnameMap);
139: }
140:
141: protected String getDefaultNS(Class type) {
142: return "java://" + type.getPackage().getName();
143: }
144: }
|