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 ; 9 import java.io.StringWriter ; 10 import java.util.ArrayList ; 11 import java.util.LinkedList ; 12 13 public class QNameMappedConcreteClassesTest extends ConcreteClassesTest { 14 15 18 public static final String XML_HEADER = "<?xml version='1.0' encoding='utf-8'?>"; 20 21 protected QNameMap qnameMap; 22 protected HierarchicalStreamDriver staxDriver; 23 protected String namespace = "java://" + WithList.class.getPackage().getName(); 24 25 public void testDefaultImplementationOfInterface() { 26 QName qname = new QName (namespace, "withList", "w"); 28 qnameMap.registerMapping(qname, WithList.class); 29 30 WithList withList = new WithList(); 31 withList.things = new ArrayList (); 32 33 String 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 (); 47 48 String 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 qname = new QName (namespace, "withList", "x"); 60 qnameMap.registerMapping(qname, WithList.class); 61 62 WithList withList = new WithList(); 63 withList.things = new ArrayList (); 64 65 String 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 .class); 76 77 WithList withList = new WithList(); 78 withList.things = new LinkedList (); 79 80 String 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 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 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 qnameMap = new QNameMap(); 111 staxDriver = new StaxDriver(qnameMap); 112 return staxDriver; 113 } 114 115 protected String toXML(Object root) { 116 StringWriter buffer = new StringWriter (); 117 xstream.marshal(root, staxDriver.createWriter(buffer)); 118 return buffer.toString(); 119 } 120 121 } 122 | Popular Tags |