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