KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.acceptance.objects.SampleLists;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 public class ImplicitCollectionTest extends AbstractAcceptanceTest {
9
10     public static class Farm extends StandardObject {
11         int size;
12         List JavaDoc animals = new ArrayList JavaDoc();
13
14         public Farm(int size) {
15             this.size = size;
16         }
17
18         public void add(Animal animal) {
19             animals.add(animal);
20         }
21     }
22
23     public static class Animal extends StandardObject {
24         String JavaDoc name;
25
26         public Animal(String JavaDoc name) {
27             this.name = name;
28         }
29     }
30
31     protected void setUp() throws Exception JavaDoc {
32         super.setUp();
33         xstream.alias("farm", Farm.class);
34         xstream.alias("animal", Animal.class);
35         xstream.alias("room", Room.class);
36         xstream.alias("house", House.class);
37         xstream.alias("person", Person.class);
38         xstream.alias("sample", SampleLists.class);
39     }
40
41     public void testWithout() {
42         Farm farm = new Farm(100);
43         farm.add(new Animal("Cow"));
44         farm.add(new Animal("Sheep"));
45
46         String JavaDoc expected = "" +
47                 "<farm>\n" +
48                 " <size>100</size>\n" +
49                 " <animals>\n" +
50                 " <animal>\n" +
51                 " <name>Cow</name>\n" +
52                 " </animal>\n" +
53                 " <animal>\n" +
54                 " <name>Sheep</name>\n" +
55                 " </animal>\n" +
56                 " </animals>\n" +
57                 "</farm>";
58
59         assertBothWays(farm, expected);
60     }
61
62     public void testWith() {
63         Farm farm = new Farm(100);
64         farm.add(new Animal("Cow"));
65         farm.add(new Animal("Sheep"));
66
67         String JavaDoc expected = "" +
68                 "<farm>\n" +
69                 " <size>100</size>\n" +
70                 " <animal>\n" +
71                 " <name>Cow</name>\n" +
72                 " </animal>\n" +
73                 " <animal>\n" +
74                 " <name>Sheep</name>\n" +
75                 " </animal>\n" +
76                 "</farm>";
77
78         xstream.addImplicitCollection(Farm.class, "animals");
79         assertBothWays(farm, expected);
80     }
81
82     public static class MegaFarm extends Farm {
83         public MegaFarm(int size) {
84             super(size);
85         }
86     }
87
88     public void testIneritsImplicitCollectionFromSuperclass() {
89         xstream.alias("MEGA-farm", MegaFarm.class);
90
91         Farm farm = new MegaFarm(100); // subclass
92
farm.add(new Animal("Cow"));
93         farm.add(new Animal("Sheep"));
94
95         String JavaDoc expected = "" +
96                 "<MEGA-farm>\n" +
97                 " <size>100</size>\n" +
98                 " <animal>\n" +
99                 " <name>Cow</name>\n" +
100                 " </animal>\n" +
101                 " <animal>\n" +
102                 " <name>Sheep</name>\n" +
103                 " </animal>\n" +
104                 "</MEGA-farm>";
105
106         xstream.addImplicitCollection(Farm.class, "animals");
107         assertBothWays(farm, expected);
108     }
109
110     public void testAllowsSubclassToOverrideImplicitCollectionInSuperclass() {
111         xstream.alias("MEGA-farm", MegaFarm.class);
112
113         Farm farm = new MegaFarm(100); // subclass
114
farm.add(new Animal("Cow"));
115         farm.add(new Animal("Sheep"));
116
117         String JavaDoc expected = "" +
118                 "<MEGA-farm>\n" +
119                 " <size>100</size>\n" +
120                 " <animal>\n" +
121                 " <name>Cow</name>\n" +
122                 " </animal>\n" +
123                 " <animal>\n" +
124                 " <name>Sheep</name>\n" +
125                 " </animal>\n" +
126                 "</MEGA-farm>";
127
128         xstream.addImplicitCollection(MegaFarm.class, "animals");
129         assertBothWays(farm, expected);
130     }
131
132     public static class House extends StandardObject {
133         private List JavaDoc rooms = new ArrayList JavaDoc();
134         private List JavaDoc people = new ArrayList JavaDoc();
135
136         public void add(Room room) {
137             rooms.add(room);
138         }
139
140         public void add(Person person) {
141             people.add(person);
142         }
143     }
144
145     public static class Room extends StandardObject {
146         private String JavaDoc name;
147
148         public Room(String JavaDoc name) {
149             this.name = name;
150         }
151     }
152
153     public static class Person extends StandardObject {
154         private String JavaDoc name;
155         private ArrayList JavaDoc emailAddresses = new ArrayList JavaDoc();
156
157         public Person(String JavaDoc name) {
158             this.name = name;
159         }
160
161         public void addEmailAddress(String JavaDoc email) {
162             emailAddresses.add(email);
163         }
164     }
165
166     public void testDefaultCollectionBasedOnType() {
167         House house = new House();
168         house.add(new Room("kitchen"));
169         house.add(new Room("bathroom"));
170         Person joe = new Person("joe");
171         joe.addEmailAddress("joe@house.org");
172         joe.addEmailAddress("joe.farmer@house.org");
173         house.add(joe);
174         Person jaimie = new Person("jaimie");
175         jaimie.addEmailAddress("jaimie@house.org");
176         jaimie.addEmailAddress("jaimie.farmer@house.org");
177         jaimie.addEmailAddress("jaimie.ann.farmer@house.org");
178         house.add(jaimie);
179
180         String JavaDoc expected = ""
181                 + "<house>\n"
182                 + " <room>\n"
183                 + " <name>kitchen</name>\n"
184                 + " </room>\n"
185                 + " <room>\n"
186                 + " <name>bathroom</name>\n"
187                 + " </room>\n"
188                 + " <person>\n"
189                 + " <name>joe</name>\n"
190                 + " <email>joe@house.org</email>\n"
191                 + " <email>joe.farmer@house.org</email>\n"
192                 + " </person>\n"
193                 + " <person>\n"
194                 + " <name>jaimie</name>\n"
195                 + " <email>jaimie@house.org</email>\n"
196                 + " <email>jaimie.farmer@house.org</email>\n"
197                 + " <email>jaimie.ann.farmer@house.org</email>\n"
198                 + " </person>\n"
199                 + "</house>";
200
201         xstream.addImplicitCollection(House.class, "rooms", Room.class);
202         xstream.addImplicitCollection(House.class, "people", Person.class);
203         xstream.addImplicitCollection(Person.class, "emailAddresses", "email", String JavaDoc.class);
204
205         assertBothWays(house, expected);
206     }
207 }
208
Popular Tags