KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > JDomAcceptanceTest


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.acceptance.someobjects.X;
4 import com.thoughtworks.acceptance.someobjects.Y;
5 import com.thoughtworks.xstream.XStream;
6 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
7 import junit.framework.TestCase;
8 import org.jdom.Document;
9 import org.jdom.input.SAXBuilder;
10 import org.jdom.output.XMLOutputter;
11 import org.jdom.output.Format;
12
13 import java.io.StringReader JavaDoc;
14 import java.util.List JavaDoc;
15
16 public class JDomAcceptanceTest extends TestCase {
17
18     private XStream xstream;
19
20     protected void setUp() throws Exception JavaDoc {
21         super.setUp();
22         xstream = new XStream();
23         xstream.alias("x", X.class);
24     }
25
26     public void testUnmarshalsObjectFromJDOM() throws Exception JavaDoc {
27         String JavaDoc xml =
28                 "<x>" +
29                 " <aStr>joe</aStr>" +
30                 " <anInt>8</anInt>" +
31                 " <innerObj>" +
32                 " <yField>walnes</yField>" +
33                 " </innerObj>" +
34                 "</x>";
35
36         Document doc = new SAXBuilder().build(new StringReader JavaDoc(xml));
37
38         X x = (X) xstream.unmarshal(new JDomReader(doc));
39
40         assertEquals("joe", x.aStr);
41         assertEquals(8, x.anInt);
42         assertEquals("walnes", x.innerObj.yField);
43     }
44
45     public void testMarshalsObjectToJDOM() {
46         X x = new X();
47         x.anInt = 9;
48         x.aStr = "zzz";
49         x.innerObj = new Y();
50         x.innerObj.yField = "ooo";
51
52         String JavaDoc expected =
53                 "<x>\n" +
54                 " <aStr>zzz</aStr>\n" +
55                 " <anInt>9</anInt>\n" +
56                 " <innerObj>\n" +
57                 " <yField>ooo</yField>\n" +
58                 " </innerObj>\n" +
59                 "</x>";
60
61         JDomWriter writer = new JDomWriter();
62         xstream.marshal(x, writer);
63         List JavaDoc result = writer.getResult();
64
65         assertEquals("Result list should contain exactly 1 element",
66                                                         1, result.size());
67
68         XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setLineSeparator("\n"));
69
70         assertEquals(expected, outputter.outputString(result));
71     }
72
73     public void testXStreamPopulatingAnObjectGraphStartingWithALiveRootObject()
74                                                 throws Exception JavaDoc {
75         String JavaDoc xml =
76                 "<component>" +
77                 " <host>host</host>" +
78                 " <port>8000</port>" +
79                 "</component>";
80
81         xstream.alias("component", Component.class);
82
83         HierarchicalStreamDriver driver = new JDomDriver();
84
85         Component component0 = new Component();
86
87         Component component1 = (Component) xstream.unmarshal(
88                         driver.createReader(new StringReader JavaDoc(xml)), component0);
89
90         assertSame(component0, component1);
91
92         assertEquals("host", component0.host);
93
94         assertEquals(8000, component0.port);
95     }
96
97     static class Component {
98         String JavaDoc host;
99         int port;
100     }
101 }
102
103
Popular Tags