KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.acceptance.someobjects.WithList;
4 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
5 import com.thoughtworks.xstream.io.xml.QNameMap;
6 import com.thoughtworks.xstream.io.xml.StaxDriver;
7
8 import javax.xml.namespace.QName JavaDoc;
9 import java.io.StringWriter JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.LinkedList JavaDoc;
12
13 public class QNameMappedConcreteClassesTest extends ConcreteClassesTest {
14
15     // For WoodStox
16
//public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
17

18     // For RI
19
public static final String JavaDoc XML_HEADER = "<?xml version='1.0' encoding='utf-8'?>";
20
21     protected QNameMap qnameMap;
22     protected HierarchicalStreamDriver staxDriver;
23     protected String JavaDoc namespace = "java://" + WithList.class.getPackage().getName();
24
25     public void testDefaultImplementationOfInterface() {
26         // lets register some qnames
27
QName JavaDoc qname = new QName JavaDoc(namespace, "withList", "w");
28         qnameMap.registerMapping(qname, WithList.class);
29
30         WithList withList = new WithList();
31         withList.things = new ArrayList JavaDoc();
32
33         String JavaDoc expected =
34                 XML_HEADER + "<w:withList xmlns:w=\"java://com.thoughtworks.acceptance.someobjects\">" +
35                 "<things></things>" +
36                 "</w:withList>";
37
38         assertBothWays(withList, expected);
39     }
40
41     public void testUsingDefaultNamespace() {
42         qnameMap.setDefaultNamespace(namespace);
43         xstream.alias("withList", WithList.class);
44
45         WithList withList = new WithList();
46         withList.things = new ArrayList JavaDoc();
47
48         String JavaDoc expected =
49                 XML_HEADER + "<withList xmlns=\"java://com.thoughtworks.acceptance.someobjects\">" +
50                 "<things></things>" +
51                 "</withList>";
52
53         assertBothWays(withList, expected);
54     }
55
56     public void testUsingDefaultNamespaceAndPrefix() {
57         qnameMap.setDefaultNamespace(namespace);
58         qnameMap.setDefaultPrefix("x");
59         QName JavaDoc qname = new QName JavaDoc(namespace, "withList", "x");
60         qnameMap.registerMapping(qname, WithList.class);
61
62         WithList withList = new WithList();
63         withList.things = new ArrayList JavaDoc();
64
65         String JavaDoc expected =
66                 XML_HEADER + "<x:withList xmlns:x=\"java://com.thoughtworks.acceptance.someobjects\">" +
67                 "<x:things></x:things>" +
68                 "</x:withList>";
69
70         assertBothWays(withList, expected);
71     }
72
73     public void testAlternativeImplementationOfInterface() {
74         xstream.alias("with-list", WithList.class);
75         xstream.alias("linked-list", LinkedList JavaDoc.class);
76
77         WithList withList = new WithList();
78         withList.things = new LinkedList JavaDoc();
79
80         String JavaDoc expected =
81                 "<?xml version='1.0' encoding='utf-8'?><with-list><things class=\"linked-list\"></things></with-list>";
82
83         assertBothWays(withList, expected);
84     }
85
86     public void testCustomInterfaceCanHaveMultipleImplementations() {
87         xstream.alias("intf", MyInterface.class);
88         xstream.alias("imp1", MyImp1.class);
89         xstream.alias("imp2", MyImp2.class);
90         xstream.alias("h", MyHolder.class);
91
92         MyHolder in = new MyHolder();
93         in.field1 = new MyImp1();
94         in.field2 = new MyImp2();
95
96         String JavaDoc expected = "" +
97                 "<?xml version='1.0' encoding='utf-8'?><h><field1 class=\"imp1\"><x>1</x></field1><field2 class=\"imp2\"><y>2</y></field2></h>";
98
99         String JavaDoc xml = xstream.toXML(in);
100         assertEquals(expected, xml);
101
102         MyHolder out = (MyHolder) xstream.fromXML(xml);
103         assertEquals(MyImp1.class, out.field1.getClass());
104         assertEquals(MyImp2.class, out.field2.getClass());
105         assertEquals(2, ((MyImp2) out.field2).y);
106     }
107
108     protected HierarchicalStreamDriver createDriver() {
109         // careful, called from inside base class constructor
110
qnameMap = new QNameMap();
111         staxDriver = new StaxDriver(qnameMap);
112         return staxDriver;
113     }
114
115     protected String JavaDoc toXML(Object JavaDoc root) {
116         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
117         xstream.marshal(root, staxDriver.createWriter(buffer));
118         return buffer.toString();
119     }
120
121 }
122
Popular Tags