/* * Copyright (C) 2005, 2006 Joe Walnes. * Copyright (C) 2006, 2007 XStream Committers. * All rights reserved. * * The software in this package is published under the terms of the BSD * style license a copy of which has been included with this distribution in * the LICENSE.txt file. * * Created on 16. September 2005 by Mauro Talevi */ package com.thoughtworks.acceptance.annotations; import com.thoughtworks.acceptance.AbstractAcceptanceTest; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamAlias; import java.util.ArrayList; import java.util.List; /** * Tests for annotation detection. * * @author Chung-Onn Cheong * @author Mauro Talevi * @author Guilherme Silveira * @author Jörg Schaible */ public class AnnotationsTest extends AbstractAcceptanceTest { @Override protected XStream createXStream() { XStream xstream = super.createXStream(); xstream.autodetectAnnotations(true); return xstream; } @XStreamAlias("param") public static class ParameterizedContainer { private ParameterizedType type; public ParameterizedContainer() { type = new ParameterizedType(new InternalType()); } } @XStreamAlias("param") public static class DoubleParameterizedContainer { private ArrayList> list; public DoubleParameterizedContainer() { list = new ArrayList>(); list.add(new ArrayList()); list.get(0).add(new InternalType()); } } @XStreamAlias("second") public static class InternalType { @XStreamAlias("aliased") private String original = "value"; @Override public boolean equals(Object obj) { return obj instanceof InternalType ? original.equals(((InternalType)obj).original) : false; } } @XStreamAlias("typeAlias") public static class ParameterizedType { @XStreamAlias("fieldAlias") private T object; public ParameterizedType(T object) { this.object = object; } @Override public boolean equals(Object obj) { return obj instanceof ParameterizedType ? object .equals(((ParameterizedType)obj).object) : false; } } public void testAreDetectedInParameterizedTypes() { String xml = "" + "\n" + " \n" + " \n" + " value\n" + " \n" + " \n" + ""; assertBothWays(new ParameterizedContainer(), xml); } public void testAreDetectedInNestedParameterizedTypes() { String xml = "" + "\n" + " \n" + " \n" + " \n" + " value\n" + " \n" + " \n" + " \n" + ""; assertBothWays(new DoubleParameterizedContainer(), xml); } public void testAreDetectedInArrays() { InternalType[] internalTypes = new InternalType[]{ new InternalType(), new InternalType()}; String xml = "" + "\n" + " \n" + " value\n" + " \n" + " \n" + " value\n" + " \n" + ""; assertBothWays(internalTypes, xml); } public void testAreDetectedInParametrizedArrays() { ParameterizedType[] types = new ParameterizedType[]{ new ParameterizedType("foo"), new ParameterizedType("bar")}; String xml = "" + "\n" + " \n" + " foo\n" + " \n" + " \n" + " bar\n" + " \n" + ""; assertBothWays(types, xml); } public void testAreDetectedInJDKCollection() { List list = new ArrayList(); list.add(new InternalType()); String xml = "" + "\n" + " \n" + " value\n" + " \n" + ""; assertBothWays(list, xml); } public void testForClassIsDetectedAtDeserialization() { // must preprocess annotations here xstream.processAnnotations(InternalType.class); InternalType internalType = new InternalType(); String xml = "" // + "\n" // + " value\n" // + ""; assertEquals(internalType, xstream.fromXML(xml)); } }