1 21 22 package nu.xom.tests; 23 24 import nu.xom.Attribute; 25 import nu.xom.Comment; 26 import nu.xom.DocType; 27 import nu.xom.Document; 28 import nu.xom.Element; 29 import nu.xom.Node; 30 import nu.xom.ProcessingInstruction; 31 import nu.xom.Text; 32 33 34 44 public class SubclassTest extends XOMTestCase { 45 46 private Element root; 47 private Document doc; 48 49 50 public SubclassTest(String name) { 51 super(name); 52 } 53 54 55 protected void setUp() { 56 root = new Element("root"); 57 doc = new Document(new ElementSubclass("root")); 58 } 59 60 61 public void testAttributeClassInCopy() { 62 root.addAttribute(new AttributeSubclass("name", "value")); 63 assertTrue(root.getAttribute(0) instanceof AttributeSubclass); 64 Element copy = (Element) root.copy(); 65 assertTrue(copy.getAttribute(0) instanceof AttributeSubclass); 66 } 67 68 69 private class AttributeSubclass extends Attribute { 70 71 AttributeSubclass(String name, String value) { 72 super(name, value); 73 } 74 75 public Node copy() { 76 return new AttributeSubclass(this.getQualifiedName(), this.getValue()); 77 } 78 79 } 80 81 82 public void testTextClassInCopy() { 83 root.appendChild(new TextSubclass("value")); 84 assertTrue(root.getChild(0) instanceof TextSubclass); 85 Element copy = (Element) root.copy(); 86 assertTrue(copy.getChild(0) instanceof TextSubclass); 87 } 88 89 90 private class TextSubclass extends Text { 91 92 TextSubclass(String value) { 93 super(value); 94 } 95 96 public Node copy() { 97 return new TextSubclass(this.getValue()); 98 } 99 } 100 101 102 public void testElementClassInCopy() { 103 root.appendChild(new ElementSubclass("child")); 104 assertTrue(root.getChild(0) instanceof ElementSubclass); 105 Element copy = (Element) root.copy(); 106 assertTrue(copy.getChild(0) instanceof ElementSubclass); 107 } 108 109 110 private class ElementSubclass extends Element { 111 112 ElementSubclass(String name) { 113 super(name); 114 } 115 116 protected Element shallowCopy() { 117 return new ElementSubclass(this.getQualifiedName()); 118 } 119 120 } 121 122 123 public void testCommentClassInCopy() { 124 root.appendChild(new CommentSubclass("value")); 125 assertTrue(root.getChild(0) instanceof CommentSubclass); 126 Element copy = (Element) root.copy(); 127 assertTrue(copy.getChild(0) instanceof CommentSubclass); 128 } 129 130 131 private class CommentSubclass extends Comment { 132 133 CommentSubclass(String value) { 134 super(value); 135 } 136 137 public Node copy() { 138 return new CommentSubclass(this.getValue()); 139 } 140 141 } 142 143 144 private class DocTypeSubclass extends DocType { 145 146 DocTypeSubclass(String name) { 147 super(name); 148 } 149 150 public Node copy() { 151 return new DocTypeSubclass(this.getRootElementName()); 152 } 153 } 154 155 156 public void testProcessingInstructionClassInCopy() { 157 root.appendChild(new ProcessingInstructionSubclass("target", "value")); 158 assertTrue(root.getChild(0) instanceof ProcessingInstructionSubclass); 159 Element copy = (Element) root.copy(); 160 assertTrue(copy.getChild(0) instanceof ProcessingInstructionSubclass); 161 } 162 163 164 private class ProcessingInstructionSubclass extends ProcessingInstruction { 165 166 ProcessingInstructionSubclass(String target, String data) { 167 super(target, data); 168 } 169 170 public Node copy() { 171 return new ProcessingInstructionSubclass(this.getTarget(), this.getValue()); 172 } 173 174 } 175 176 177 public void testProcessingInstructionClassInDocCopy() { 178 doc.insertChild(new ProcessingInstructionSubclass("target", "value"), 0); 179 assertTrue(doc.getChild(0) instanceof ProcessingInstructionSubclass); 180 Document copy = (Document) doc.copy(); 181 assertTrue(copy.getChild(0) instanceof ProcessingInstructionSubclass); 182 } 183 184 185 public void testCommentClassInDocCopy() { 186 doc.insertChild(new CommentSubclass("target"), 0); 187 assertTrue(doc.getChild(0) instanceof CommentSubclass); 188 Document copy = (Document) doc.copy(); 189 assertTrue(copy.getChild(0) instanceof CommentSubclass); 190 } 191 192 193 public void testElementClassInDocCopy() { 194 assertTrue(doc.getChild(0) instanceof ElementSubclass); 195 Document copy = (Document) doc.copy(); 196 assertTrue(copy.getChild(0) instanceof ElementSubclass); 197 } 198 199 200 public void testDocTypeClassInDocCopy() { 201 doc.insertChild(new DocTypeSubclass("root"), 0); 202 assertTrue(doc.getChild(0) instanceof DocTypeSubclass); 203 Document copy = (Document) doc.copy(); 204 assertTrue(copy.getChild(0) instanceof DocTypeSubclass); 205 } 206 207 208 } 209 | Popular Tags |