1 package com.thoughtworks.xstream;
2
3 import com.thoughtworks.someobjects.FunnyConstructor;
4 import com.thoughtworks.someobjects.WithList;
5 import com.thoughtworks.someobjects.X;
6 import com.thoughtworks.someobjects.Y;
7 import junit.framework.TestCase;
8
9 public class XStreamTest extends TestCase {
10
11 private XStream xstream;
12
13 protected void setUp() throws Exception {
14 super.setUp();
15 xstream = new XStream();
16 xstream.alias("x", X.class);
17 xstream.alias("y", Y.class);
18 xstream.alias("funny", FunnyConstructor.class);
19 xstream.alias("with-list", WithList.class);
20 }
21
22 public void testUnmarshalsObjectFromXml() {
23
24 String xml =
25 "<x>" +
26 " <aStr>joe</aStr>" +
27 " <anInt>8</anInt>" +
28 " <innerObj>" +
29 " <yField>walnes</yField>" +
30 " </innerObj>" +
31 "</x>";
32
33 X x = (X) xstream.fromXML(xml);
34
35 assertEquals("joe", x.aStr);
36 assertEquals(8, x.anInt);
37 assertEquals("walnes", x.innerObj.yField);
38 }
39
40 public void testMarshalsObjectToXml() {
41 X x = new X();
42 x.anInt = 9;
43 x.aStr = "zzz";
44 x.innerObj = new Y();
45 x.innerObj.yField = "ooo";
46
47 String expected =
48 "<x>\n" +
49 " <aStr>zzz</aStr>\n" +
50 " <anInt>9</anInt>\n" +
51 " <innerObj>\n" +
52 " <yField>ooo</yField>\n" +
53 " </innerObj>\n" +
54 "</x>";
55
56 assertEquals(expected, xstream.toXML(x));
57 }
58
59 public void testUnmarshalsClassWithoutDefaultConstructor() {
60 String xml =
61 "<funny>" +
62 " <i>999</i>" +
63 "</funny>";
64
65 FunnyConstructor funnyConstructor = (FunnyConstructor) xstream.fromXML(xml);
66
67 assertEquals(999, funnyConstructor.i);
68 }
69
70 public void testHandlesLists() {
71 WithList original = new WithList();
72 Y y = new Y();
73 y.yField = "a";
74 original.things.add(y);
75 original.things.add(new FunnyConstructor(3));
76 original.things.add(new FunnyConstructor(1));
77
78 String xml = xstream.toXML(original);
79
80 String expected =
81 "<with-list>\n" +
82 " <things>\n" +
83 " <y>\n" +
84 " <yField>a</yField>\n" +
85 " </y>\n" +
86 " <funny>\n" +
87 " <i>3</i>\n" +
88 " </funny>\n" +
89 " <funny>\n" +
90 " <i>1</i>\n" +
91 " </funny>\n" +
92 " </things>\n" +
93 "</with-list>";
94
95 assertEquals(expected, xml);
96
97 WithList result = (WithList) xstream.fromXML(xml);
98 assertEquals(original, result);
99
100 }
101
102 public void testNonStaticPrivateInnerClassCanBeUsed() {
103 NonStaticInnerClass obj = new NonStaticInnerClass();
104 obj.field = 3;
105
106 xstream.alias("inner", NonStaticInnerClass.class);
107
108 String xml = xstream.toXML(obj);
109
110 String expected =
111 "<inner>\n" +
112 " <field>3</field>\n" +
113 "</inner>";
114
115 assertEquals(expected, xml);
116
117 NonStaticInnerClass result = (NonStaticInnerClass) xstream.fromXML(xml);
118 assertEquals(obj.field, result.field);
119 }
120
121 public void testClassWithoutMappingUsesFullyQualifiedName() {
122 NonStaticInnerClass obj = new NonStaticInnerClass();
123 obj.field = 3;
124
125 String xml = xstream.toXML(obj);
126
127 String expected =
128 "<com.thoughtworks.xstream.XStreamTest-NonStaticInnerClass>\n" +
129 " <field>3</field>\n" +
130 "</com.thoughtworks.xstream.XStreamTest-NonStaticInnerClass>";
131
132 assertEquals(expected, xml);
133
134 NonStaticInnerClass result = (NonStaticInnerClass) xstream.fromXML(xml);
135 assertEquals(obj.field, result.field);
136 }
137
138 private class NonStaticInnerClass {
139 int field;
140 }
141
142 }
This page was automatically generated by Maven