/* * Copyright (C) 2003, 2004 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 26. September 2003 by Joe Walnes */ package com.thoughtworks.acceptance; import com.thoughtworks.acceptance.objects.StandardObject; import com.thoughtworks.acceptance.someobjects.WithList; import java.util.ArrayList; import java.util.LinkedList; public class ConcreteClassesTest extends AbstractAcceptanceTest { public void testDefaultImplementationOfInterface() { xstream.alias("with-list", WithList.class); WithList withList = new WithList(); withList.things = new ArrayList(); String expected = "\n" + " \n" + ""; assertBothWays(withList, expected); } public void testAlternativeImplementationOfInterface() { xstream.alias("with-list", WithList.class); xstream.alias("linked-list", LinkedList.class); WithList withList = new WithList(); withList.things = new LinkedList(); String expected = "\n" + " \n" + ""; assertBothWays(withList, expected); } interface MyInterface { } public static class MyImp1 extends StandardObject implements MyInterface { int x = 1; } public static class MyImp2 extends StandardObject implements MyInterface { int y = 2; } public static class MyHolder extends StandardObject { MyInterface field1; MyInterface field2; } public void testCustomInterfaceCanHaveMultipleImplementations() { xstream.alias("intf", MyInterface.class); xstream.alias("imp1", MyImp1.class); xstream.alias("imp2", MyImp2.class); xstream.alias("h", MyHolder.class); MyHolder in = new MyHolder(); in.field1 = new MyImp1(); in.field2 = new MyImp2(); String expected = "" + "\n" + " \n" + " 1\n" + " \n" + " \n" + " 2\n" + " \n" + ""; String xml = xstream.toXML(in); assertEquals(expected, xml); MyHolder out = (MyHolder) xstream.fromXML(xml); assertEquals(MyImp1.class, out.field1.getClass()); assertEquals(MyImp2.class, out.field2.getClass()); assertEquals(2, ((MyImp2) out.field2).y); } }