1 package com.thoughtworks.acceptance; 2 3 public class CustomClassesTest extends AbstractAcceptanceTest { 4 5 public static class SamplePerson extends StandardObject { 6 int anInt; 7 String firstName; 8 String lastName; 9 } 10 11 public void testCustomObjectWithBasicFields() { 12 13 xstream.alias("friend", SamplePerson.class); 14 15 SamplePerson person = new SamplePerson(); 16 person.anInt = 3; 17 person.firstName = "Joe"; 18 person.lastName = "Walnes"; 19 20 String expected = 21 "<friend>\n" + 22 " <anInt>3</anInt>\n" + 23 " <firstName>Joe</firstName>\n" + 24 " <lastName>Walnes</lastName>\n" + 25 "</friend>"; 26 27 assertBothWays(person, expected); 28 29 } 30 31 public static class SamplePersonHolder { 32 String aString; 33 SamplePerson brother; 34 35 public boolean equals(Object obj) { 36 SamplePersonHolder containerObject = (SamplePersonHolder) obj; 37 return aString.equals(containerObject.aString) 38 && brother.equals(containerObject.brother); 39 } 40 } 41 42 public void testCustomObjectWithCustomObjectField() { 43 xstream.alias("friend", SamplePerson.class); 44 xstream.alias("personHolder", SamplePersonHolder.class); 45 46 SamplePersonHolder personHolder = new SamplePersonHolder(); 47 personHolder.aString = "hello world"; 48 49 SamplePerson person = new SamplePerson(); 50 person.anInt = 3; 51 person.firstName = "Joe"; 52 person.lastName = "Walnes"; 53 54 personHolder.brother = person; 55 56 String expected = 57 "<personHolder>\n" + 58 " <aString>hello world</aString>\n" + 59 " <brother>\n" + 60 " <anInt>3</anInt>\n" + 61 " <firstName>Joe</firstName>\n" + 62 " <lastName>Walnes</lastName>\n" + 63 " </brother>\n" + 64 "</personHolder>"; 65 66 assertBothWays(personHolder, expected); 67 68 } 69 70 public void testNullObjectsDoNotHaveFieldsWritten() { 71 72 xstream.alias("cls", WithSomeFields.class); 73 74 WithSomeFields obj = new WithSomeFields(); 75 76 String expected = "<cls/>"; 77 78 assertBothWays(obj, expected); 79 } 80 81 public void testEmptyStringsAreNotTreatedAsNulls() { 82 xstream.alias("cls", WithSomeFields.class); 83 84 WithSomeFields obj = new WithSomeFields(); 85 obj.b = ""; 86 87 String expected = "" + 88 "<cls>\n" + 89 " <b></b>\n" + 90 "</cls>"; 91 92 assertBothWays(obj, expected); 93 } 94 95 public static class WithSomeFields extends StandardObject { 96 Object a; 97 String b; 98 } 99 100 public void testNullsAreDistinguishedFromEmptyStrings() { 101 LotsOfStrings in = new LotsOfStrings(); 102 in.a = "."; 103 in.b = ""; 104 in.c = null; 105 106 String xml = xstream.toXML(in); 107 LotsOfStrings out = (LotsOfStrings) xstream.fromXML(xml); 108 109 assertEquals(".", out.a); 110 assertEquals("", out.b); 111 assertNull(out.c); 112 } 113 114 public static class LotsOfStrings { 115 String a; 116 String b; 117 String c; 118 } 119 120 public void testFieldWithObjectType() { 121 String expected = "" + 122 "<thing>\n" + 123 " <one>1.0</one>\n" + 124 " <two class=\"double\">2.0</two>\n" + 125 "</thing>"; 126 xstream.alias("thing", FieldWithObjectType.class); 127 128 assertBothWays(new FieldWithObjectType(), expected); 129 } 130 131 public static class FieldWithObjectType extends StandardObject { 132 Double one = new Double (1.0); 133 Object two = new Double (2.0); 134 } 135 } 136 | Popular Tags |