001: /* Copyright 2003, 2004 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import nu.xom.Attribute;
025: import nu.xom.Comment;
026: import nu.xom.DocType;
027: import nu.xom.Document;
028: import nu.xom.Element;
029: import nu.xom.Node;
030: import nu.xom.ProcessingInstruction;
031: import nu.xom.Text;
032:
033: /**
034: * <p>
035: * Tests for subclasses of XOM classes.
036: * This makes sure XOM is sufficiently polymorphic.
037: * </p>
038: *
039: * @author Elliotte Rusty Harold
040: * @version 1.0
041: *
042: */
043: public class SubclassTest extends XOMTestCase {
044:
045: private Element root;
046: private Document doc;
047:
048: public SubclassTest(String name) {
049: super (name);
050: }
051:
052: protected void setUp() {
053: root = new Element("root");
054: doc = new Document(new ElementSubclass("root"));
055: }
056:
057: public void testAttributeClassInCopy() {
058: root.addAttribute(new AttributeSubclass("name", "value"));
059: assertTrue(root.getAttribute(0) instanceof AttributeSubclass);
060: Element copy = (Element) root.copy();
061: assertTrue(copy.getAttribute(0) instanceof AttributeSubclass);
062: }
063:
064: private class AttributeSubclass extends Attribute {
065:
066: AttributeSubclass(String name, String value) {
067: super (name, value);
068: }
069:
070: public Node copy() {
071: return new AttributeSubclass(this .getQualifiedName(), this
072: .getValue());
073: }
074:
075: }
076:
077: public void testTextClassInCopy() {
078: root.appendChild(new TextSubclass("value"));
079: assertTrue(root.getChild(0) instanceof TextSubclass);
080: Element copy = (Element) root.copy();
081: assertTrue(copy.getChild(0) instanceof TextSubclass);
082: }
083:
084: private class TextSubclass extends Text {
085:
086: TextSubclass(String value) {
087: super (value);
088: }
089:
090: public Node copy() {
091: return new TextSubclass(this .getValue());
092: }
093: }
094:
095: public void testElementClassInCopy() {
096: root.appendChild(new ElementSubclass("child"));
097: assertTrue(root.getChild(0) instanceof ElementSubclass);
098: Element copy = (Element) root.copy();
099: assertTrue(copy.getChild(0) instanceof ElementSubclass);
100: }
101:
102: private class ElementSubclass extends Element {
103:
104: ElementSubclass(String name) {
105: super (name);
106: }
107:
108: protected Element shallowCopy() {
109: return new ElementSubclass(this .getQualifiedName());
110: }
111:
112: }
113:
114: public void testCommentClassInCopy() {
115: root.appendChild(new CommentSubclass("value"));
116: assertTrue(root.getChild(0) instanceof CommentSubclass);
117: Element copy = (Element) root.copy();
118: assertTrue(copy.getChild(0) instanceof CommentSubclass);
119: }
120:
121: private class CommentSubclass extends Comment {
122:
123: CommentSubclass(String value) {
124: super (value);
125: }
126:
127: public Node copy() {
128: return new CommentSubclass(this .getValue());
129: }
130:
131: }
132:
133: private class DocTypeSubclass extends DocType {
134:
135: DocTypeSubclass(String name) {
136: super (name);
137: }
138:
139: public Node copy() {
140: return new DocTypeSubclass(this .getRootElementName());
141: }
142: }
143:
144: public void testProcessingInstructionClassInCopy() {
145: root.appendChild(new ProcessingInstructionSubclass("target",
146: "value"));
147: assertTrue(root.getChild(0) instanceof ProcessingInstructionSubclass);
148: Element copy = (Element) root.copy();
149: assertTrue(copy.getChild(0) instanceof ProcessingInstructionSubclass);
150: }
151:
152: private class ProcessingInstructionSubclass extends
153: ProcessingInstruction {
154:
155: ProcessingInstructionSubclass(String target, String data) {
156: super (target, data);
157: }
158:
159: public Node copy() {
160: return new ProcessingInstructionSubclass(this .getTarget(),
161: this .getValue());
162: }
163:
164: }
165:
166: public void testProcessingInstructionClassInDocCopy() {
167: doc.insertChild(new ProcessingInstructionSubclass("target",
168: "value"), 0);
169: assertTrue(doc.getChild(0) instanceof ProcessingInstructionSubclass);
170: Document copy = (Document) doc.copy();
171: assertTrue(copy.getChild(0) instanceof ProcessingInstructionSubclass);
172: }
173:
174: public void testCommentClassInDocCopy() {
175: doc.insertChild(new CommentSubclass("target"), 0);
176: assertTrue(doc.getChild(0) instanceof CommentSubclass);
177: Document copy = (Document) doc.copy();
178: assertTrue(copy.getChild(0) instanceof CommentSubclass);
179: }
180:
181: public void testElementClassInDocCopy() {
182: assertTrue(doc.getChild(0) instanceof ElementSubclass);
183: Document copy = (Document) doc.copy();
184: assertTrue(copy.getChild(0) instanceof ElementSubclass);
185: }
186:
187: public void testDocTypeClassInDocCopy() {
188: doc.insertChild(new DocTypeSubclass("root"), 0);
189: assertTrue(doc.getChild(0) instanceof DocTypeSubclass);
190: Document copy = (Document) doc.copy();
191: assertTrue(copy.getChild(0) instanceof DocTypeSubclass);
192: }
193:
194: }
|