KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > SubclassTest


1 /* Copyright 2003, 2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

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 /**
35  * <p>
36  * Tests for subclasses of XOM classes.
37  * This makes sure XOM is sufficiently polymorphic.
38  * </p>
39  *
40  * @author Elliotte Rusty Harold
41  * @version 1.0
42  *
43  */

44 public class SubclassTest extends XOMTestCase {
45
46     private Element root;
47     private Document doc;
48     
49     
50     public SubclassTest(String JavaDoc 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 JavaDoc name, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc target, String JavaDoc 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