KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > ConcreteClassesTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.acceptance.someobjects.WithList;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.LinkedList JavaDoc;
7
8 public class ConcreteClassesTest extends AbstractAcceptanceTest {
9
10     public void testDefaultImplementationOfInterface() {
11
12         xstream.alias("with-list", WithList.class);
13
14         WithList withList = new WithList();
15         withList.things = new ArrayList JavaDoc();
16
17         String JavaDoc expected =
18                 "<with-list>\n" +
19                 " <things/>\n" +
20                 "</with-list>";
21
22         assertBothWays(withList, expected);
23
24     }
25
26     public void testAlternativeImplementationOfInterface() {
27
28         xstream.alias("with-list", WithList.class);
29         xstream.alias("linked-list", LinkedList JavaDoc.class);
30
31         WithList withList = new WithList();
32         withList.things = new LinkedList JavaDoc();
33
34         String JavaDoc expected =
35                 "<with-list>\n" +
36                 " <things class=\"linked-list\"/>\n" +
37                 "</with-list>";
38
39         assertBothWays(withList, expected);
40
41     }
42
43     interface MyInterface {
44     }
45
46     public static class MyImp1 extends StandardObject implements MyInterface {
47         int x = 1;
48     }
49
50     public static class MyImp2 extends StandardObject implements MyInterface {
51         int y = 2;
52     }
53
54     public static class MyHolder extends StandardObject {
55         MyInterface field1;
56         MyInterface field2;
57     }
58
59     public void testCustomInterfaceCanHaveMultipleImplementations() {
60         xstream.alias("intf", MyInterface.class);
61         xstream.alias("imp1", MyImp1.class);
62         xstream.alias("imp2", MyImp2.class);
63         xstream.alias("h", MyHolder.class);
64
65         MyHolder in = new MyHolder();
66         in.field1 = new MyImp1();
67         in.field2 = new MyImp2();
68
69         String JavaDoc expected = "" +
70                 "<h>\n" +
71                 " <field1 class=\"imp1\">\n" +
72                 " <x>1</x>\n" +
73                 " </field1>\n" +
74                 " <field2 class=\"imp2\">\n" +
75                 " <y>2</y>\n" +
76                 " </field2>\n" +
77                 "</h>";
78
79         String JavaDoc xml = xstream.toXML(in);
80         assertEquals(expected, xml);
81
82         MyHolder out = (MyHolder) xstream.fromXML(xml);
83         assertEquals(MyImp1.class, out.field1.getClass());
84         assertEquals(MyImp2.class, out.field2.getClass());
85         assertEquals(2, ((MyImp2) out.field2).y);
86     }
87 }
88
Popular Tags