01: /*
02: * Copyright (C) 2003, 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 26. September 2003 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: import com.thoughtworks.acceptance.objects.StandardObject;
15: import com.thoughtworks.acceptance.someobjects.WithList;
16:
17: import java.util.ArrayList;
18: import java.util.LinkedList;
19:
20: public class ConcreteClassesTest extends AbstractAcceptanceTest {
21:
22: public void testDefaultImplementationOfInterface() {
23:
24: xstream.alias("with-list", WithList.class);
25:
26: WithList withList = new WithList();
27: withList.things = new ArrayList();
28:
29: String expected = "<with-list>\n" + " <things/>\n"
30: + "</with-list>";
31:
32: assertBothWays(withList, expected);
33:
34: }
35:
36: public void testAlternativeImplementationOfInterface() {
37:
38: xstream.alias("with-list", WithList.class);
39: xstream.alias("linked-list", LinkedList.class);
40:
41: WithList withList = new WithList();
42: withList.things = new LinkedList();
43:
44: String expected = "<with-list>\n"
45: + " <things class=\"linked-list\"/>\n"
46: + "</with-list>";
47:
48: assertBothWays(withList, expected);
49:
50: }
51:
52: interface MyInterface {
53: }
54:
55: public static class MyImp1 extends StandardObject implements
56: MyInterface {
57: int x = 1;
58: }
59:
60: public static class MyImp2 extends StandardObject implements
61: MyInterface {
62: int y = 2;
63: }
64:
65: public static class MyHolder extends StandardObject {
66: MyInterface field1;
67: MyInterface field2;
68: }
69:
70: public void testCustomInterfaceCanHaveMultipleImplementations() {
71: xstream.alias("intf", MyInterface.class);
72: xstream.alias("imp1", MyImp1.class);
73: xstream.alias("imp2", MyImp2.class);
74: xstream.alias("h", MyHolder.class);
75:
76: MyHolder in = new MyHolder();
77: in.field1 = new MyImp1();
78: in.field2 = new MyImp2();
79:
80: String expected = "" + "<h>\n" + " <field1 class=\"imp1\">\n"
81: + " <x>1</x>\n" + " </field1>\n"
82: + " <field2 class=\"imp2\">\n" + " <y>2</y>\n"
83: + " </field2>\n" + "</h>";
84:
85: String xml = xstream.toXML(in);
86: assertEquals(expected, xml);
87:
88: MyHolder out = (MyHolder) xstream.fromXML(xml);
89: assertEquals(MyImp1.class, out.field1.getClass());
90: assertEquals(MyImp2.class, out.field2.getClass());
91: assertEquals(2, ((MyImp2) out.field2).y);
92: }
93: }
|