01: /*
02: * Copyright (C) 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 03. September 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import java.io.StringReader;
15: import java.util.List;
16:
17: import junit.framework.TestCase;
18:
19: import org.jdom.Document;
20: import org.jdom.input.SAXBuilder;
21: import org.jdom.output.Format;
22: import org.jdom.output.XMLOutputter;
23:
24: import com.thoughtworks.acceptance.someobjects.X;
25: import com.thoughtworks.acceptance.someobjects.Y;
26: import com.thoughtworks.xstream.XStream;
27:
28: public class JDomAcceptanceTest extends TestCase {
29:
30: private XStream xstream;
31:
32: protected void setUp() throws Exception {
33: super .setUp();
34: xstream = new XStream();
35: xstream.alias("x", X.class);
36: }
37:
38: public void testUnmarshalsObjectFromJDOM() throws Exception {
39: String xml = "<x>" + " <aStr>joe</aStr>"
40: + " <anInt>8</anInt>" + " <innerObj>"
41: + " <yField>walnes</yField>" + " </innerObj>"
42: + "</x>";
43:
44: Document doc = new SAXBuilder().build(new StringReader(xml));
45:
46: X x = (X) xstream.unmarshal(new JDomReader(doc));
47:
48: assertEquals("joe", x.aStr);
49: assertEquals(8, x.anInt);
50: assertEquals("walnes", x.innerObj.yField);
51: }
52:
53: public void testMarshalsObjectToJDOM() {
54: X x = new X();
55: x.anInt = 9;
56: x.aStr = "zzz";
57: x.innerObj = new Y();
58: x.innerObj.yField = "ooo";
59:
60: String expected = "<x>\n" + " <aStr>zzz</aStr>\n"
61: + " <anInt>9</anInt>\n" + " <innerObj>\n"
62: + " <yField>ooo</yField>\n" + " </innerObj>\n"
63: + "</x>";
64:
65: JDomWriter writer = new JDomWriter();
66: xstream.marshal(x, writer);
67: List result = writer.getTopLevelNodes();
68:
69: assertEquals("Result list should contain exactly 1 element", 1,
70: result.size());
71:
72: XMLOutputter outputter = new XMLOutputter(Format
73: .getPrettyFormat().setLineSeparator("\n"));
74:
75: assertEquals(expected, outputter.outputString(result));
76: }
77: }
|