1 package com.thoughtworks.xstream; 2 3 import com.thoughtworks.acceptance.StandardObject; 4 import com.thoughtworks.acceptance.someobjects.*; 5 import com.thoughtworks.xstream.converters.Converter; 6 import com.thoughtworks.xstream.converters.MarshallingContext; 7 import com.thoughtworks.xstream.converters.UnmarshallingContext; 8 import com.thoughtworks.xstream.core.JVM; 9 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 10 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 11 import com.thoughtworks.xstream.io.xml.Dom4JDriver; 12 import junit.framework.TestCase; 13 import org.dom4j.Element; 14 15 import java.io.StringReader ; 16 17 public class XStreamTest extends TestCase { 18 19 private XStream xstream; 20 21 protected void setUp() throws Exception { 22 super.setUp(); 23 xstream = new XStream(); 24 xstream.alias("x", X.class); 25 xstream.alias("y", Y.class); 26 xstream.alias("funny", FunnyConstructor.class); 27 xstream.alias("with-list", WithList.class); 28 } 29 30 public void testUnmarshalsObjectFromXml() { 31 32 String xml = 33 "<x>" + 34 " <aStr>joe</aStr>" + 35 " <anInt>8</anInt>" + 36 " <innerObj>" + 37 " <yField>walnes</yField>" + 38 " </innerObj>" + 39 "</x>"; 40 41 X x = (X) xstream.fromXML(xml); 42 43 assertEquals("joe", x.aStr); 44 assertEquals(8, x.anInt); 45 assertEquals("walnes", x.innerObj.yField); 46 } 47 48 public void testMarshalsObjectToXml() { 49 X x = new X(); 50 x.anInt = 9; 51 x.aStr = "zzz"; 52 x.innerObj = new Y(); 53 x.innerObj.yField = "ooo"; 54 55 String expected = 56 "<x>\n" + 57 " <aStr>zzz</aStr>\n" + 58 " <anInt>9</anInt>\n" + 59 " <innerObj>\n" + 60 " <yField>ooo</yField>\n" + 61 " </innerObj>\n" + 62 "</x>"; 63 64 assertEquals(expected, xstream.toXML(x)); 65 } 66 67 public void testUnmarshalsClassWithoutDefaultConstructor() { 68 if (!JVM.is14()) return; 69 70 String xml = 71 "<funny>" + 72 " <i>999</i>" + 73 "</funny>"; 74 75 FunnyConstructor funnyConstructor = (FunnyConstructor) xstream.fromXML(xml); 76 77 assertEquals(999, funnyConstructor.i); 78 } 79 80 public void testHandlesLists() { 81 WithList original = new WithList(); 82 Y y = new Y(); 83 y.yField = "a"; 84 original.things.add(y); 85 original.things.add(new X(3)); 86 original.things.add(new X(1)); 87 88 String xml = xstream.toXML(original); 89 90 String expected = 91 "<with-list>\n" + 92 " <things>\n" + 93 " <y>\n" + 94 " <yField>a</yField>\n" + 95 " </y>\n" + 96 " <x>\n" + 97 " <anInt>3</anInt>\n" + 98 " </x>\n" + 99 " <x>\n" + 100 " <anInt>1</anInt>\n" + 101 " </x>\n" + 102 " </things>\n" + 103 "</with-list>"; 104 105 assertEquals(expected, xml); 106 107 WithList result = (WithList) xstream.fromXML(xml); 108 assertEquals(original, result); 109 110 } 111 112 public void testCanHandleNonStaticPrivateInnerClass() { 113 if (!JVM.is14()) return; 114 115 NonStaticInnerClass obj = new NonStaticInnerClass(); 116 obj.field = 3; 117 118 xstream.alias("inner", NonStaticInnerClass.class); 119 120 String xml = xstream.toXML(obj); 121 122 String expected = 123 "<inner>\n" + 124 " <field>3</field>\n" + 125 "</inner>"; 126 127 assertEquals(expected, xml); 128 129 NonStaticInnerClass result = (NonStaticInnerClass) xstream.fromXML(xml); 130 assertEquals(obj.field, result.field); 131 } 132 133 public void testClassWithoutMappingUsesFullyQualifiedName() { 134 Person obj = new Person(); 135 136 String xml = xstream.toXML(obj); 137 138 String expected = "<com.thoughtworks.xstream.XStreamTest-Person/>"; 139 140 assertEquals(expected, xml); 141 142 Person result = (Person) xstream.fromXML(xml); 143 assertEquals(obj, result); 144 } 145 146 private class NonStaticInnerClass extends StandardObject { 147 int field; 148 } 149 150 public void testCanBeBeUsedMultipleTimesWithSameInstance() { 151 Y obj = new Y(); 152 obj.yField = "x"; 153 154 assertEquals(xstream.toXML(obj), xstream.toXML(obj)); 155 } 156 157 public void testXStreamWithPeekMethodWithUnderlyingDom4JImplementation() 158 throws Exception { 159 160 String xml = 161 "<person>" + 162 " <firstName>jason</firstName>" + 163 " <lastName>van Zyl</lastName>" + 164 " <element>" + 165 " <foo>bar</foo>" + 166 " </element>" + 167 "</person>"; 168 169 xstream.registerConverter(new ElementConverter()); 170 171 xstream.alias("person", Person.class); 172 173 Dom4JDriver driver = new Dom4JDriver(); 174 175 Person person = (Person) xstream.unmarshal(driver.createReader(new StringReader (xml))); 176 177 assertEquals("jason", person.firstName); 178 179 assertEquals("van Zyl", person.lastName); 180 181 assertNotNull(person.element); 182 183 assertEquals("bar", person.element.element("foo").getText()); 184 } 185 186 public static class Person extends StandardObject { 187 String firstName; 188 String lastName; 189 Element element; 190 } 191 192 private class ElementConverter implements Converter { 193 194 public boolean canConvert(Class type) { 195 return Element.class.isAssignableFrom(type); 196 } 197 198 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 199 } 200 201 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 202 203 Element element = (Element) reader.peekUnderlyingNode(); 204 205 while (reader.hasMoreChildren()) { 206 reader.moveDown(); 207 reader.moveUp(); 208 } 209 210 return element; 211 } 212 } 213 214 public void testXStreamPopulatingAnObjectGraphStartingWithALiveRootObject() 215 throws Exception { 216 217 String xml = 218 "<component>" + 219 " <host>host</host>" + 220 " <port>8000</port>" + 221 "</component>"; 222 223 xstream.alias("component", Component.class); 224 225 Dom4JDriver driver = new Dom4JDriver(); 226 227 Component component0 = new Component(); 228 229 Component component1 = (Component) xstream.unmarshal(driver.createReader(new StringReader (xml)), component0); 230 231 assertSame(component0, component1); 232 233 assertEquals("host", component0.host); 234 235 assertEquals(8000, component0.port); 236 } 237 238 static class Component { 239 String host; 240 int port; 241 } 242 243 public void testUnmarshallingWhereAllImplementationsAreSpecifiedUsingAClassIdentifier() 244 throws Exception { 245 246 String xml = 247 "<handlerManager class='com.thoughtworks.acceptance.someobjects.HandlerManager'>" + 248 " <handlers>" + 249 " <handler class='com.thoughtworks.acceptance.someobjects.Handler'>" + 250 " <protocol class='com.thoughtworks.acceptance.someobjects.Protocol'>" + 251 " <id>foo</id> " + 252 " </protocol> " + 253 " </handler>" + 254 " </handlers>" + 255 "</handlerManager>"; 256 257 HandlerManager hm = (HandlerManager) xstream.fromXML(xml); 258 259 Handler h = (Handler) hm.getHandlers().get(0); 260 261 Protocol p = h.getProtocol(); 262 263 assertEquals("foo", p.getId()); 264 } 265 } 266 | Popular Tags |