001: /*
002: * Copyright (C) 2004, 2005, 2006 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 06. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.Software;
015: import com.thoughtworks.acceptance.someobjects.X;
016: import com.thoughtworks.xstream.XStream;
017: import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
018: import com.thoughtworks.xstream.io.xml.XppDriver;
019: import com.thoughtworks.xstream.mapper.CannotResolveClassException;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * @author Paul Hammant
026: */
027: public class AliasTest extends AbstractAcceptanceTest {
028:
029: public void testBarfsIfItDoesNotExist() {
030:
031: String xml = "" + "<X-array>\n" + " <X>\n"
032: + " <anInt>0</anInt>\n" + " </X>\n" + "</X-array>";
033:
034: // now change the alias
035: xstream.alias("Xxxxxxxx", X.class);
036: try {
037: xstream.fromXML(xml);
038: fail("ShouldCannotResolveClassException expected");
039: } catch (CannotResolveClassException expectedException) {
040: // expected
041: }
042: }
043:
044: public void testWithUnderscore() {
045: xstream = new XStream(new XppDriver(new XmlFriendlyReplacer(
046: "_-", "_")));
047: String xml = "" + "<X_alias>\n" + " <anInt>0</anInt>\n"
048: + "</X_alias>";
049:
050: // now change the alias
051: xstream.alias("X_alias", X.class);
052: X x = new X(0);
053: assertBothWays(x, xml);
054: }
055:
056: public void testForFieldAsAttribute() {
057: Software software = new Software("walness", "xstream");
058:
059: xstream.alias("software", Software.class);
060: xstream.useAttributeFor(String.class);
061: xstream.aliasAttribute("id", "name");
062:
063: String xml = "<software vendor=\"walness\" id=\"xstream\"/>";
064:
065: assertBothWays(software, xml);
066: }
067:
068: public void testForReferenceAttribute() {
069: List list = new ArrayList();
070: Software software = new Software("walness", "xstream");
071: list.add(software);
072: list.add(software);
073:
074: xstream.alias("software", Software.class);
075: xstream.useAttributeFor(String.class);
076: xstream.aliasAttribute("refid", "reference");
077:
078: String xml = "" + "<list>\n"
079: + " <software vendor=\"walness\" name=\"xstream\"/>\n"
080: + " <software refid=\"../software\"/>\n" + "</list>";
081:
082: assertBothWays(list, xml);
083: }
084:
085: public void testIdentityForFields() {
086: Software software = new Software("walness", "xstream");
087:
088: xstream.alias("software", Software.class);
089: xstream.aliasField("name", Software.class, "name");
090: xstream.aliasField("vendor", Software.class, "vendor");
091:
092: String xml = "" + "<software>\n"
093: + " <vendor>walness</vendor>\n"
094: + " <name>xstream</name>\n" + "</software>";
095:
096: assertBothWays(software, xml);
097: }
098:
099: private static class FieldsWithInternalNames {
100: String clazz;
101: String ref;
102: }
103:
104: public void testCanUseInternalNameAsFieldAlias() {
105: FieldsWithInternalNames object = new FieldsWithInternalNames();
106: object.clazz = "TestIt";
107: object.ref = "MyReference";
108:
109: xstream.alias("internalNames", FieldsWithInternalNames.class);
110: xstream.aliasField("class", FieldsWithInternalNames.class,
111: "clazz");
112: xstream.aliasField("reference", FieldsWithInternalNames.class,
113: "ref");
114:
115: String xml = "" + "<internalNames>\n"
116: + " <class>TestIt</class>\n"
117: + " <reference>MyReference</reference>\n"
118: + "</internalNames>";
119:
120: assertBothWays(object, xml);
121: }
122:
123: public void testCanAliasPrimitiveTypes() {
124: Object object = new boolean[] { true, false };
125: xstream.alias("bo", Boolean.TYPE);
126: String xml = "" + "<bo-array>\n" + " <bo>true</bo>\n"
127: + " <bo>false</bo>\n" + "</bo-array>";
128: assertBothWays(object, xml);
129: }
130:
131: public static class TypeA {
132: private String attrA = "testA";
133: }
134:
135: public static class TypeB extends TypeA {
136: private String attrB = "testB";
137: }
138:
139: public static class TypeC extends TypeB {
140: private String attrC = "testC";
141: }
142:
143: public void testCanAliasInheritedFields() {
144: xstream.alias("test", TypeC.class);
145: xstream.aliasField("a", TypeA.class, "attrA");
146: xstream.aliasField("b", TypeB.class, "attrB");
147: xstream.aliasField("c", TypeC.class, "attrC");
148: TypeC object = new TypeC();
149: String xml = "" + "<test>\n" + " <a>testA</a>\n"
150: + " <b>testB</b>\n" + " <c>testC</c>\n" + "</test>";
151: assertBothWays(object, xml);
152: }
153:
154: public void testCanOverwriteInheritedAlias() {
155: xstream.alias("test", TypeC.class);
156: xstream.aliasField("a", TypeA.class, "attrA");
157: xstream.aliasField("b", TypeB.class, "attrB");
158: xstream.aliasField("c", TypeC.class, "attrC");
159: xstream.aliasField("y", TypeC.class, "attrA");
160: TypeC object = new TypeC();
161: String xml = "" + "<test>\n" + " <y>testA</y>\n"
162: + " <b>testB</b>\n" + " <c>testC</c>\n" + "</test>";
163: assertBothWays(object, xml);
164: }
165:
166: public void testCanAliasArrayElements() {
167: Object[] software = new Object[] { new Software("walness",
168: "xstream") };
169:
170: xstream.alias("software", Software.class);
171: xstream.aliasField("Name", Software.class, "name");
172: xstream.aliasField("Vendor", Software.class, "vendor");
173:
174: String xml = "" + "<object-array>\n" + " <software>\n"
175: + " <Vendor>walness</Vendor>\n"
176: + " <Name>xstream</Name>\n" + " </software>\n"
177: + "</object-array>";
178:
179: assertBothWays(software, xml);
180: }
181: }
|