1 package com.thoughtworks.acceptance; 2 3 import com.thoughtworks.acceptance.objects.OpenSourceSoftware; 4 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 11 public class InheritanceTest extends AbstractAcceptanceTest { 12 public void testHandlesInheritanceHeirarchies() { 13 OpenSourceSoftware openSourceSoftware = new OpenSourceSoftware("apache", "geronimo", "license"); 14 String 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 name; 27 28 public ParentClass(String name) { 29 this.name = name; 30 } 31 32 public String getParentName() { 33 return name; 34 } 35 } 36 37 public static class ChildClass extends ParentClass { 38 private String name; 39 40 public ChildClass(String parentName, String childName) { 41 super(parentName); 42 this.name = childName; 43 } 44 45 public String getChildName() { 46 return name; 47 } 48 49 public String toString() { 50 return getParentName() + "/" + getChildName(); 51 } 52 53 public boolean equals(Object 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 assertEquals("PARENT", child.getParentName()); 65 assertEquals("CHILD", child.getChildName()); 66 67 String 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 stuff = new ArrayList (); 78 79 public List getParentStuff() { 80 return stuff; 81 } 82 } 83 84 public static class ChildA extends ParentA { 85 private Map stuff = new HashMap (); 86 87 public Map getChildStuff() { 88 return stuff; 89 } 90 91 public boolean equals(Object 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 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 |