001: /*
002: * Copyright (C) 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 16. September 2005 by Mauro Talevi
011: */
012: package com.thoughtworks.acceptance.annotations;
013:
014: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.annotations.XStreamAlias;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: /**
022: * Tests for annotation detection.
023: *
024: * @author Chung-Onn Cheong
025: * @author Mauro Talevi
026: * @author Guilherme Silveira
027: * @author Jörg Schaible
028: */
029: public class AnnotationsTest extends AbstractAcceptanceTest {
030:
031: @Override
032: protected XStream createXStream() {
033: XStream xstream = super .createXStream();
034: xstream.autodetectAnnotations(true);
035: return xstream;
036: }
037:
038: @XStreamAlias("param")
039: public static class ParameterizedContainer {
040:
041: private ParameterizedType<InternalType> type;
042:
043: public ParameterizedContainer() {
044: type = new ParameterizedType<InternalType>(
045: new InternalType());
046: }
047:
048: }
049:
050: @XStreamAlias("param")
051: public static class DoubleParameterizedContainer {
052:
053: private ArrayList<ArrayList<InternalType>> list;
054:
055: public DoubleParameterizedContainer() {
056: list = new ArrayList<ArrayList<InternalType>>();
057: list.add(new ArrayList<InternalType>());
058: list.get(0).add(new InternalType());
059: }
060:
061: }
062:
063: @XStreamAlias("second")
064: public static class InternalType {
065: @XStreamAlias("aliased")
066: private String original = "value";
067:
068: @Override
069: public boolean equals(Object obj) {
070: return obj instanceof InternalType ? original
071: .equals(((InternalType) obj).original) : false;
072: }
073:
074: }
075:
076: @XStreamAlias("typeAlias")
077: public static class ParameterizedType<T> {
078: @XStreamAlias("fieldAlias")
079: private T object;
080:
081: public ParameterizedType(T object) {
082: this .object = object;
083: }
084:
085: @Override
086: public boolean equals(Object obj) {
087: return obj instanceof ParameterizedType ? object
088: .equals(((ParameterizedType) obj).object) : false;
089: }
090: }
091:
092: public void testAreDetectedInParameterizedTypes() {
093: String xml = "" + "<param>\n" + " <type>\n"
094: + " <fieldAlias class=\"second\">\n"
095: + " <aliased>value</aliased>\n"
096: + " </fieldAlias>\n" + " </type>\n" + "</param>";
097: assertBothWays(new ParameterizedContainer(), xml);
098: }
099:
100: public void testAreDetectedInNestedParameterizedTypes() {
101: String xml = "" + "<param>\n" + " <list>\n" + " <list>\n"
102: + " <second>\n"
103: + " <aliased>value</aliased>\n"
104: + " </second>\n" + " </list>\n" + " </list>\n"
105: + "</param>";
106: assertBothWays(new DoubleParameterizedContainer(), xml);
107: }
108:
109: public void testAreDetectedInArrays() {
110: InternalType[] internalTypes = new InternalType[] {
111: new InternalType(), new InternalType() };
112: String xml = "" + "<second-array>\n" + " <second>\n"
113: + " <aliased>value</aliased>\n" + " </second>\n"
114: + " <second>\n" + " <aliased>value</aliased>\n"
115: + " </second>\n" + "</second-array>";
116: assertBothWays(internalTypes, xml);
117: }
118:
119: public void testAreDetectedInParametrizedArrays() {
120: ParameterizedType<String>[] types = new ParameterizedType[] {
121: new ParameterizedType<String>("foo"),
122: new ParameterizedType<String>("bar") };
123: String xml = "" + "<typeAlias-array>\n" + " <typeAlias>\n"
124: + " <fieldAlias class=\"string\">foo</fieldAlias>\n"
125: + " </typeAlias>\n" + " <typeAlias>\n"
126: + " <fieldAlias class=\"string\">bar</fieldAlias>\n"
127: + " </typeAlias>\n" + "</typeAlias-array>";
128: assertBothWays(types, xml);
129: }
130:
131: public void testAreDetectedInJDKCollection() {
132: List<InternalType> list = new ArrayList<InternalType>();
133: list.add(new InternalType());
134: String xml = "" + "<list>\n" + " <second>\n"
135: + " <aliased>value</aliased>\n" + " </second>\n"
136: + "</list>";
137: assertBothWays(list, xml);
138: }
139:
140: public void testForClassIsDetectedAtDeserialization() {
141: // must preprocess annotations here
142: xstream.processAnnotations(InternalType.class);
143: InternalType internalType = new InternalType();
144: String xml = "" //
145: + "<second>\n" //
146: + " <aliased>value</aliased>\n" //
147: + "</second>";
148: assertEquals(internalType, xstream.fromXML(xml));
149: }
150: }
|