KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 public class DefaultCollectionTest extends AbstractAcceptanceTest {
7
8     public static class Farm extends StandardObject {
9         int size;
10         List JavaDoc animals = new ArrayList JavaDoc();
11
12         public Farm(int size) {
13             this.size = size;
14         }
15
16         public void add(Animal animal) {
17             animals.add(animal);
18         }
19     }
20
21     public static class Animal extends StandardObject {
22         String JavaDoc name;
23
24         public Animal(String JavaDoc name) {
25             this.name = name;
26         }
27     }
28
29     protected void setUp() throws Exception JavaDoc {
30         super.setUp();
31         xstream.alias("farm", Farm.class);
32         xstream.alias("animal", Animal.class);
33     }
34
35     public void testWithout() {
36         Farm farm = new Farm(100);
37         farm.add(new Animal("Cow"));
38         farm.add(new Animal("Sheep"));
39
40         String JavaDoc expected = "" +
41                 "<farm>\n" +
42                 " <size>100</size>\n" +
43                 " <animals>\n" +
44                 " <animal>\n" +
45                 " <name>Cow</name>\n" +
46                 " </animal>\n" +
47                 " <animal>\n" +
48                 " <name>Sheep</name>\n" +
49                 " </animal>\n" +
50                 " </animals>\n" +
51                 "</farm>";
52
53         assertBothWays(farm, expected);
54     }
55
56     public void testWith() {
57         Farm farm = new Farm(100);
58         farm.add(new Animal("Cow"));
59         farm.add(new Animal("Sheep"));
60
61         String JavaDoc expected = "" +
62                 "<farm>\n" +
63                 " <size>100</size>\n" +
64                 " <animal>\n" +
65                 " <name>Cow</name>\n" +
66                 " </animal>\n" +
67                 " <animal>\n" +
68                 " <name>Sheep</name>\n" +
69                 " </animal>\n" +
70                 "</farm>";
71
72         xstream.addDefaultCollection(Farm.class, "animals");
73         assertBothWays(farm, expected);
74     }
75 }
76
Popular Tags