1 package com.thoughtworks.acceptance;
2
3 public class CustomClassesTest extends AbstractAcceptanceTest {
4
5 class SamplePerson {
6 int anInt;
7 String firstName;
8 String lastName;
9
10 public boolean equals(Object obj) {
11 SamplePerson samplePerson = (SamplePerson) obj;
12 return anInt == samplePerson.anInt
13 && firstName.equals(samplePerson.firstName)
14 && lastName.equals(samplePerson.lastName);
15 }
16 }
17
18 public void testCustomObjectWithBasicFields() {
19
20 xstream.alias("friend", SamplePerson.class);
21
22 SamplePerson person = new SamplePerson();
23 person.anInt = 3;
24 person.firstName = "Joe";
25 person.lastName = "Walnes";
26
27 String expected =
28 "<friend>\n" +
29 " <anInt>3</anInt>\n" +
30 " <firstName>Joe</firstName>\n" +
31 " <lastName>Walnes</lastName>\n" +
32 "</friend>";
33
34 assertBothWays(person, expected);
35
36 }
37
38 class SamplePersonHolder {
39 String aString;
40 SamplePerson brother;
41
42 public boolean equals(Object obj) {
43 SamplePersonHolder containerObject = (SamplePersonHolder) obj;
44 return aString.equals(containerObject.aString)
45 && brother.equals(containerObject.brother);
46 }
47 }
48
49 public void testCustomObjectWithCustomObjectField() {
50 xstream.alias("friend", SamplePerson.class);
51 xstream.alias("personHolder", SamplePersonHolder.class);
52
53 SamplePersonHolder personHolder = new SamplePersonHolder();
54 personHolder.aString = "hello world";
55
56 SamplePerson person = new SamplePerson();
57 person.anInt = 3;
58 person.firstName = "Joe";
59 person.lastName = "Walnes";
60
61 personHolder.brother = person;
62
63 String expected =
64 "<personHolder>\n" +
65 " <aString>hello world</aString>\n" +
66 " <brother>\n" +
67 " <anInt>3</anInt>\n" +
68 " <firstName>Joe</firstName>\n" +
69 " <lastName>Walnes</lastName>\n" +
70 " </brother>\n" +
71 "</personHolder>";
72
73 assertBothWays(personHolder, expected);
74
75 }
76
77 public void testNullObjectsDoNotHaveFieldsWritten() {
78
79 xstream.alias("cls", WithSomeFields.class);
80
81 WithSomeFields obj = new WithSomeFields();
82
83 String expected = "<cls/>";
84
85 assertBothWays(obj, expected);
86 }
87
88 public class WithSomeFields {
89 Object a;
90 String b;
91
92 public boolean equals(Object obj) {
93 WithSomeFields w = (WithSomeFields) obj;
94 return w.a == a && w.b == b;
95 }
96 }
97
98 }
This page was automatically generated by Maven