001: /*
002: * Copyright (C) 2003, 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 26. September 2003 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
015: import com.thoughtworks.acceptance.objects.StandardObject;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: public class InheritanceTest extends AbstractAcceptanceTest {
023: public void testHandlesInheritanceHierarchies() {
024: OpenSourceSoftware openSourceSoftware = new OpenSourceSoftware(
025: "apache", "geronimo", "license");
026: String xml = "<oss>\n" + " <vendor>apache</vendor>\n"
027: + " <name>geronimo</name>\n"
028: + " <license>license</license>\n" + "</oss>";
029:
030: xstream.alias("oss", OpenSourceSoftware.class);
031: assertBothWays(openSourceSoftware, xml);
032: }
033:
034: public static class ParentClass {
035: private String name;
036:
037: public ParentClass() {
038: }
039:
040: public ParentClass(String name) {
041: this .name = name;
042: }
043:
044: public String getParentName() {
045: return name;
046: }
047: }
048:
049: public static class ChildClass extends ParentClass {
050: private String name;
051:
052: public ChildClass() {
053: }
054:
055: public ChildClass(String parentName, String childName) {
056: super (parentName);
057: this .name = childName;
058: }
059:
060: public String getChildName() {
061: return name;
062: }
063:
064: public String toString() {
065: return getParentName() + "/" + getChildName();
066: }
067:
068: public boolean equals(Object obj) {
069: return toString().equals(obj.toString());
070: }
071: }
072:
073: public void testInheritanceHidingPrivateFieldOfSameName() {
074: xstream.alias("parent", ParentClass.class);
075: xstream.alias("child", ChildClass.class);
076:
077: ChildClass child = new ChildClass("PARENT", "CHILD");
078: // sanity checks
079: assertEquals("PARENT", child.getParentName());
080: assertEquals("CHILD", child.getChildName());
081:
082: String expected = "" + "<child>\n"
083: + " <name defined-in=\"parent\">PARENT</name>\n"
084: + " <name>CHILD</name>\n" + "</child>";
085:
086: assertBothWays(child, expected);
087: }
088:
089: public static class StaticChildClass extends ParentClass {
090: private static String name = "CHILD";
091:
092: public StaticChildClass() {
093: }
094:
095: public StaticChildClass(String parentName) {
096: super (parentName);
097: }
098: }
099:
100: public void testHandlesStaticFieldInChildDoesNotHideFieldInParent() {
101: xstream.alias("child", StaticChildClass.class);
102:
103: StaticChildClass child = new StaticChildClass("PARENT");
104: String expected = "" + "<child>\n" + " <name>PARENT</name>\n"
105: + "</child>";
106:
107: StaticChildClass serialized = (StaticChildClass) assertBothWays(
108: child, expected);
109: assertEquals("PARENT", child.getParentName());
110: assertEquals("CHILD", StaticChildClass.name);
111: }
112:
113: public static class ParentA extends StandardObject {
114: private List stuff = new ArrayList();
115:
116: public List getParentStuff() {
117: return stuff;
118: }
119: }
120:
121: public static class ChildA extends ParentA {
122: private Map stuff = new HashMap();
123:
124: public Map getChildStuff() {
125: return stuff;
126: }
127:
128: public boolean equals(Object obj) {
129: ChildA a = (ChildA) obj;
130: if (!getChildStuff().getClass().equals(
131: a.getChildStuff().getClass())) {
132: return false;
133: }
134: if (!getParentStuff().getClass().equals(
135: a.getParentStuff().getClass())) {
136: return false;
137: }
138: return getChildStuff().equals(a.getChildStuff())
139: && getParentStuff().equals(a.getParentStuff());
140: }
141: }
142:
143: public void testHiddenFieldsWithDifferentType() {
144: xstream.alias("child-a", ChildA.class);
145: xstream.alias("parent-a", ParentA.class);
146: ChildA childA = new ChildA();
147: childA.getChildStuff().put("hello", "world");
148: childA.getParentStuff().add("woo");
149: String expected = "" + "<child-a>\n"
150: + " <stuff defined-in=\"parent-a\">\n"
151: + " <string>woo</string>\n" + " </stuff>\n"
152: + " <stuff>\n" + " <entry>\n"
153: + " <string>hello</string>\n"
154: + " <string>world</string>\n" + " </entry>\n"
155: + " </stuff>\n" + "</child-a>";
156: assertBothWays(childA, expected);
157: }
158: }
|