KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10
11 public class InheritanceTest extends AbstractAcceptanceTest {
12     public void testHandlesInheritanceHeirarchies() {
13         OpenSourceSoftware openSourceSoftware = new OpenSourceSoftware("apache", "geronimo", "license");
14         String JavaDoc xml =
15                 "<oss>\n" +
16                 " <license>license</license>\n" +
17                 " <name>geronimo</name>\n" +
18                 " <vendor>apache</vendor>\n" +
19                 "</oss>";
20
21         xstream.alias("oss", OpenSourceSoftware.class);
22         assertBothWays(openSourceSoftware, xml);
23     }
24
25     public static class ParentClass {
26         private String JavaDoc name;
27
28         public ParentClass(String JavaDoc name) {
29             this.name = name;
30         }
31
32         public String JavaDoc getParentName() {
33             return name;
34         }
35     }
36
37     public static class ChildClass extends ParentClass {
38         private String JavaDoc name;
39
40         public ChildClass(String JavaDoc parentName, String JavaDoc childName) {
41             super(parentName);
42             this.name = childName;
43         }
44
45         public String JavaDoc getChildName() {
46             return name;
47         }
48
49         public String JavaDoc toString() {
50             return getParentName() + "/" + getChildName();
51         }
52
53         public boolean equals(Object JavaDoc obj) {
54             return toString().equals(obj.toString());
55         }
56     }
57
58     public void testInheritanceHidingPrivateFieldOfSameName() {
59         xstream.alias("parent", ParentClass.class);
60         xstream.alias("child", ChildClass.class);
61
62         ChildClass child = new ChildClass("PARENT", "CHILD");
63         // sanity checks
64
assertEquals("PARENT", child.getParentName());
65         assertEquals("CHILD", child.getChildName());
66
67         String JavaDoc expected = "" +
68                 "<child>\n" +
69                 " <name>CHILD</name>\n" +
70                 " <name defined-in=\"parent\">PARENT</name>\n" +
71                 "</child>";
72
73         assertBothWays(child, expected);
74     }
75
76     public static class ParentA extends StandardObject {
77         private List JavaDoc stuff = new ArrayList JavaDoc();
78
79         public List JavaDoc getParentStuff() {
80             return stuff;
81         }
82     }
83
84     public static class ChildA extends ParentA {
85         private Map JavaDoc stuff = new HashMap JavaDoc();
86
87         public Map JavaDoc getChildStuff() {
88             return stuff;
89         }
90
91         public boolean equals(Object JavaDoc obj) {
92             ChildA a = (ChildA) obj;
93             if (!getChildStuff().getClass().equals(a.getChildStuff().getClass())) {
94                 return false;
95             }
96             if (!getParentStuff().getClass().equals(a.getParentStuff().getClass())) {
97                 return false;
98             }
99             return getChildStuff().equals(a.getChildStuff())
100                     && getParentStuff().equals(a.getParentStuff());
101         }
102     }
103
104     public void testHiddenFieldsWithDifferentType() {
105         xstream.alias("child-a", ChildA.class);
106         xstream.alias("parent-a", ParentA.class);
107         ChildA childA = new ChildA();
108         childA.getChildStuff().put("hello", "world");
109         childA.getParentStuff().add("woo");
110         String JavaDoc expected = "" +
111                 "<child-a>\n" +
112                 " <stuff>\n" +
113                 " <entry>\n" +
114                 " <string>hello</string>\n" +
115                 " <string>world</string>\n" +
116                 " </entry>\n" +
117                 " </stuff>\n" +
118                 " <stuff defined-in=\"parent-a\">\n" +
119                 " <string>woo</string>\n" +
120                 " </stuff>\n" +
121                 "</child-a>";
122         assertBothWays(childA, expected);
123     }
124 }
125
Popular Tags