KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > AbstractTestCase


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: AbstractTestCase.java,v 1.3.2.2 2004/01/15 22:02:37 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import org.dom4j.*;
13 import org.dom4j.CharacterData;
14 import org.dom4j.util.NodeComparator;
15 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
16 import org.ozoneDB.xml.dom4j.o3impl.OzoneDocumentFactoryImpl;
17 import test.OzoneTestCase;
18
19 /** An abstract base class for some DOM4J test cases
20   *
21   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
22   * Adaptation for Ozone by Per Nyfelt
23   * @version $Revision$
24   */

25 public class AbstractTestCase extends OzoneTestCase {
26
27     protected static final boolean COMPARE_TEXT = false;
28
29     protected Document document;
30
31     protected NodeFactory nodeFactory;
32
33     protected XPathFactory xpathFactory;
34
35     public AbstractTestCase(String JavaDoc name) {
36         super(name);
37     }
38
39
40     public void log(String JavaDoc text) {
41         System.out.println(text);
42     }
43
44
45     public void assertDocumentsEqual(Document doc1, Document doc2) throws Exception JavaDoc {
46         try {
47             assertTrue( "Doc1 not null", doc1 != null );
48             assertTrue( "Doc2 not null", doc2 != null );
49
50             doc1.normalize();
51             doc2.normalize();
52
53             assertNodesEqual(doc1, doc2);
54
55             NodeComparator comparator = new NodeComparator();
56             assertTrue( "Documents are equal", comparator.compare( doc1, doc2 ) == 0 );
57
58             if ( COMPARE_TEXT ) {
59                 String JavaDoc text1 = doc1.asXML();
60                 String JavaDoc text2 = doc2.asXML();
61
62                 assertEquals( "Text of documents is equal", text1, text2 );
63             }
64         }
65         catch (Exception JavaDoc e) {
66             log( "Failed during comparison of: " + doc1 + " and: " + doc2 );
67             throw e;
68         }
69     }
70
71
72     public void assertNodesEqual( Document n1, Document n2 ) {
73         assertEquals( "Document names", n1.getName(), n2.getName() );
74         assertNodesEqual( n1.getDocType(), n2.getDocType() );
75         assertNodesEqualContent( n1, n2 );
76     }
77
78     public void assertNodesEqual( Element n1, Element n2 ) {
79         assertNodesEqual( n1.getQName(), n2.getQName() );
80
81         int c1 = n1.attributeCount();
82         int c2 = n2.attributeCount();
83
84         assertEquals(
85             "Elements have same number of attributes (" + c1 + ", " + c2
86                 + " for: " + n1 + " and " + n2,
87             c1, c2
88         );
89
90         for ( int i = 0; i < c1; i++ ) {
91             Attribute a1 = n1.attribute(i);
92             Attribute a2 = n2.attribute(i);
93             assertNodesEqual( a1, a2 );
94         }
95
96         assertNodesEqualContent( n1, n2 );
97     }
98
99     public void assertNodesEqual( Attribute n1, Attribute n2 ) {
100         assertNodesEqual( n1.getQName(), n2.getQName() );
101
102         assertEquals(
103             "Attribute values for: " + n1 + " and " + n2,
104             n1.getValue(), n2.getValue()
105         );
106     }
107
108     public void assertNodesEqual( QName n1, QName n2 ) {
109         assertEquals(
110             "URIs equal for: " + n1.getQualifiedName() + " and " + n2.getQualifiedName(),
111             n1.getNamespaceURI(), n2.getNamespaceURI()
112         );
113         assertEquals(
114             "qualified names equal",
115             n1.getQualifiedName(), n2.getQualifiedName()
116         );
117     }
118
119     public void assertNodesEqual( CharacterData t1, CharacterData t2 ) {
120         assertEquals(
121             "Text equal for: " + t1 + " and " + t2,
122             t1.getText(), t2.getText()
123         );
124     }
125
126     public void assertNodesEqual( DocumentType o1, DocumentType o2 ) {
127         if ( o1 != o2 ) {
128             if ( o1 == null ) {
129                 assertTrue( "Missing DocType: " + o2, false );
130             }
131             else if ( o2 == null ) {
132                 assertTrue( "Missing DocType: " + o1, false );
133             }
134             else {
135                 assertEquals( "DocType name equal", o1.getName(), o2.getName() );
136                 assertEquals( "DocType publicID equal", o1.getPublicID(), o2.getPublicID() );
137                 assertEquals( "DocType systemID equal", o1.getSystemID(), o2.getSystemID() );
138             }
139         }
140     }
141
142     public void assertNodesEqual( Entity o1, Entity o2 ) {
143         assertEquals( "Entity names equal", o1.getName(), o2.getName() );
144         assertEquals( "Entity values equal", o1.getText(), o2.getText() );
145     }
146
147     public void assertNodesEqual( ProcessingInstruction n1, ProcessingInstruction n2 ) {
148         assertEquals( "PI targets equal", n1.getTarget(), n2.getTarget() );
149         assertEquals( "PI text equal", n1.getText(), n2.getText() );
150     }
151
152     public void assertNodesEqual( Namespace n1, Namespace n2 ) {
153         assertEquals( "AbstractNamespace prefixes equal", n1.getPrefix(), n2.getPrefix() );
154         assertEquals( "AbstractNamespace URIs equal", n1.getURI(), n2.getURI() );
155     }
156
157     public void assertNodesEqualContent( Branch b1, Branch b2 ) {
158         int c1 = b1.nodeCount();
159         int c2 = b2.nodeCount();
160
161         if ( c1 != c2 ) {
162             log( "Content of: " + b1 );
163             log( "is: " + b1.content() );
164             log( "Content of: " + b2 );
165             log( "is: " + b2.content() );
166         }
167
168         assertEquals(
169             "Branches have same number of children (" + c1 + ", " + c2
170                 + " for: " + b1 + " and " + b2,
171             c1, c2
172         );
173         for ( int i = 0; i < c1; i++ ) {
174             Node n1 = b1.node(i);
175             Node n2 = b2.node(i);
176             assertNodesEqual( n1, n2 );
177         }
178     }
179
180     public void assertNodesEqual( Node n1, Node n2 ) {
181         int nodeType1 = n1.getNodeType();
182         int nodeType2 = n2.getNodeType();
183         assertTrue( "Nodes are of same type: ", nodeType1 == nodeType2 );
184
185         switch (nodeType1) {
186             case Node.ELEMENT_NODE:
187                 assertNodesEqual((Element) n1, (Element) n2);
188                 break;
189             case Node.DOCUMENT_NODE:
190                 assertNodesEqual((Document) n1, (Document) n2);
191                 break;
192             case Node.ATTRIBUTE_NODE:
193                 assertNodesEqual((Attribute) n1, (Attribute) n2);
194                 break;
195             case Node.TEXT_NODE:
196                 assertNodesEqual((Text) n1, (Text) n2);
197                 break;
198             case Node.CDATA_SECTION_NODE:
199                 assertNodesEqual((CDATA) n1, (CDATA) n2);
200                 break;
201             case Node.ENTITY_REFERENCE_NODE:
202                 assertNodesEqual((Entity) n1, (Entity) n2);
203                 break;
204             case Node.PROCESSING_INSTRUCTION_NODE:
205                 assertNodesEqual((ProcessingInstruction) n1, (ProcessingInstruction) n2);
206                 break;
207             case Node.COMMENT_NODE:
208                 assertNodesEqual((Comment) n1, (Comment) n2);
209                 break;
210             case Node.DOCUMENT_TYPE_NODE:
211                 assertNodesEqual((DocumentType) n1, (DocumentType) n2);
212                 break;
213             case Node.NAMESPACE_NODE:
214                 assertNodesEqual((Namespace) n1, (Namespace) n2);
215                 break;
216             default:
217                 assertTrue( "Invalid node types. node1: " + n1 + " and node2: " + n2, false );
218         }
219     }
220
221
222     // Implementation methods
223
//-------------------------------------------------------------------------
224
protected void setUp() throws Exception JavaDoc {
225         O3DocumentHelper.configure(db());
226         nodeFactory = OzoneDocumentFactoryImpl.getInstance(db());
227         xpathFactory = OzoneDocumentFactoryImpl.getInstance(db());
228         document = createDocument();
229
230         Element root = document.addElement( "root" );
231
232         Element author1 = root.addElement( "author" )
233             .addAttribute( "name", "James" )
234             .addAttribute( "location", "UK" )
235             .addText("James Strachan");
236
237         Element url1 = author1.addElement( "url" )
238             .addText( "http://sourceforge.net/users/jstrachan/" );
239
240         Element author2 = root.addElement( "author" )
241             .addAttribute( "name", "Bob" )
242             .addAttribute( "location", "Canada" )
243             .addText("Bob McWhirter");
244
245         Element url2 = author2.addElement( "url" )
246             .addText( "http://sourceforge.net/users/werken/" );
247     }
248
249     protected Document createDocument() throws Exception JavaDoc {
250         return O3DocumentHelper.createDocument();
251     }
252
253
254     /** @return the root element of the document */
255     protected Element getRootElement() {
256         Element root = document.getRootElement();
257         assertTrue( "Document has root element", root != null );
258         return root;
259     }
260
261
262 }
263
264
265
266
267 /*
268  * Redistribution and use of this software and associated documentation
269  * ("Software"), with or without modification, are permitted provided
270  * that the following conditions are met:
271  *
272  * 1. Redistributions of source code must retain copyright
273  * statements and notices. Redistributions must also contain a
274  * copy of this document.
275  *
276  * 2. Redistributions in binary form must reproduce the
277  * above copyright notice, this list of conditions and the
278  * following disclaimer in the documentation and/or other
279  * materials provided with the distribution.
280  *
281  * 3. The name "DOM4J" must not be used to endorse or promote
282  * products derived from this Software without prior written
283  * permission of MetaStuff, Ltd. For written permission,
284  * please contact dom4j-info@metastuff.com.
285  *
286  * 4. Products derived from this Software may not be called "DOM4J"
287  * nor may "DOM4J" appear in their names without prior written
288  * permission of MetaStuff, Ltd. DOM4J is a registered
289  * trademark of MetaStuff, Ltd.
290  *
291  * 5. Due credit should be given to the DOM4J Project
292  * (http://dom4j.org/).
293  *
294  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
295  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
296  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
297  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
298  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
299  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
300  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
301  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
302  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
303  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
304  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
305  * OF THE POSSIBILITY OF SUCH DAMAGE.
306  *
307  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
308  *
309  * $Id: AbstractTestCase.java,v 1.3.2.2 2004/01/15 22:02:37 per_nyfelt Exp $
310  */

311
Popular Tags