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 27. June 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.acceptance;
012:
013: import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.converters.ConverterLookup;
017: import com.thoughtworks.xstream.converters.reflection.FieldDictionary;
018: import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
019: import com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter;
020: import com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy;
021: import com.thoughtworks.xstream.core.ReferenceByXPathUnmarshaller;
022: import com.thoughtworks.xstream.core.TreeUnmarshaller;
023: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
024: import com.thoughtworks.xstream.mapper.Mapper;
025:
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * Test XStream 1.2 compatibility.
033: *
034: * @author Jörg Schaible
035: */
036: public class XStream12CompatibilityTest extends AbstractAcceptanceTest {
037:
038: public static class ParentClass {
039: String name;
040: }
041:
042: public static class ChildClass extends ParentClass {
043: String name;
044:
045: ChildClass() {
046: this ("JDK", "1.3");
047: }
048:
049: ChildClass(final String parent, final String child) {
050: ((ParentClass) this ).name = parent;
051: name = child;
052: }
053:
054: public String toString() {
055: return ((ParentClass) this ).name + "/" + name;
056: }
057: }
058:
059: public void testCanDeserializeHiddenFieldsWithSameTypeWrittenWithXStream11() {
060: xstream.alias("parent", ParentClass.class);
061: xstream.alias("child", ChildClass.class);
062:
063: final String in = "" + "<child>\n" + " <name>CHILD</name>\n"
064: + " <name defined-in=\"parent\">PARENT</name>\n"
065: + "</child>";
066:
067: final ChildClass child = (ChildClass) xstream.fromXML(in);
068: assertEquals("PARENT/CHILD", child.toString());
069: }
070:
071: public static class ParentA extends StandardObject {
072: private List stuff = new ArrayList();
073:
074: public List getParentStuff() {
075: return stuff;
076: }
077: }
078:
079: public static class ChildA extends ParentA {
080: private Map stuff = new HashMap();
081:
082: public Map getChildStuff() {
083: return stuff;
084: }
085: }
086:
087: public void testCanDeserializeHiddenFieldsWithDifferentTypeWrittenWithXStream11() {
088: xstream.alias("child-a", ChildA.class);
089: xstream.alias("parent-a", ParentA.class);
090: String expected = "" + "<child-a>\n" + " <stuff>\n"
091: + " <entry>\n" + " <string>hello</string>\n"
092: + " <string>world</string>\n" + " </entry>\n"
093: + " </stuff>\n"
094: + " <stuff defined-in=\"parent-a\">\n"
095: + " <string>foo</string>\n" + " </stuff>\n"
096: + "</child-a>";
097:
098: ChildA childA = (ChildA) xstream.fromXML(expected);
099: assertEquals("world", childA.getChildStuff().get("hello"));
100: assertEquals("foo", childA.getParentStuff().iterator().next());
101: }
102:
103: public void testCanWriteInheritanceHierarchiesInOldOrder() {
104: xstream = new XStream(new PureJavaReflectionProvider(
105: new FieldDictionary(new XStream12FieldKeySorter())));
106: OpenSourceSoftware openSourceSoftware = new OpenSourceSoftware(
107: "apache", "geronimo", "license");
108: String xml = "<oss>\n" + " <license>license</license>\n"
109: + " <vendor>apache</vendor>\n"
110: + " <name>geronimo</name>\n" + "</oss>";
111:
112: xstream.alias("oss", OpenSourceSoftware.class);
113: assertEquals(xml, xstream.toXML(openSourceSoftware));
114: }
115:
116: private final class XStream12ReferenceByXPathUnmarshaller extends
117: ReferenceByXPathUnmarshaller {
118: private XStream12ReferenceByXPathUnmarshaller(Object root,
119: HierarchicalStreamReader reader,
120: ConverterLookup converterLookup, Mapper mapper) {
121: super (root, reader, converterLookup, mapper);
122: isXmlFriendly = false;
123: }
124: }
125:
126: public void testCanReadXmlUnfriendlyXPathReferences() {
127: xstream
128: .setMarshallingStrategy(new ReferenceByXPathMarshallingStrategy(
129: ReferenceByXPathMarshallingStrategy.RELATIVE) {
130:
131: protected TreeUnmarshaller createUnmarshallingContext(
132: Object root,
133: HierarchicalStreamReader reader,
134: ConverterLookup converterLookup,
135: Mapper mapper) {
136: return new XStream12ReferenceByXPathUnmarshaller(
137: root, reader, converterLookup, mapper);
138: }
139:
140: });
141: xstream.alias("foo$bar", StringBuffer.class);
142: xstream.alias("x_y", StringBuffer.class);
143: String xml = "<list>\n" + " <foo_-bar>foo</foo_-bar>\n"
144: + " <foo_-bar reference=\"../foo$bar\"/>\n"
145: + " <x__y>bar</x__y>\n"
146: + " <x__y reference=\"../x_y\"/>\n" + "</list>";
147:
148: List list = (List) xstream.fromXML(xml);
149: assertEquals(4, list.size());
150: assertSame(list.get(0), list.get(1));
151: assertEquals("foo", list.get(0).toString());
152: assertSame(list.get(2), list.get(3));
153: assertEquals("bar", list.get(2).toString());
154: }
155: }
|