KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright 2002-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 junit.framework.ComparisonFailure;
25 import junit.framework.TestCase;
26 import nu.xom.Attribute;
27 import nu.xom.Comment;
28 import nu.xom.DocType;
29 import nu.xom.Document;
30 import nu.xom.Element;
31 import nu.xom.Node;
32 import nu.xom.ProcessingInstruction;
33 import nu.xom.Text;
34
35
36 /**
37  * <p>
38  * Provides utility methods to compare nodes for deep equality in an
39  * infoset sense.
40  * </p>
41  *
42  * @author Elliotte Rusty Harold
43  * @version 1.0
44  *
45  */

46 public class XOMTestCase extends TestCase {
47
48     /**
49      * <p>
50      * Create a new <code>XOMTestCase</code> with the specified name.
51      * </p>
52      */

53     public XOMTestCase(String JavaDoc name) {
54         super(name);
55     }
56     
57     /**
58      * <p>
59      * Asserts that two text nodes are equal. Text nodes are considered
60      * equal if they are identical char by char, or if both are null.
61      * Unicode and whitespace normalization is not performed before
62      * comparison. If the two nodes are not equal, a
63      * <code>ComparisonFailure</code> is thrown.
64      * </p>
65      *
66      * @param expected the text the test should produce
67      * @param actual the text the test does produce
68      *
69      * @throws ComparisonFailure if the text nodes are not equal
70      */

71     public static void assertEquals(Text expected, Text actual) {
72         assertEquals(null, expected, actual);
73     }
74     
75     
76     /**
77      * <p>
78      * Asserts that two text nodes are equal. Text nodes are considered
79      * equal if they are identical char by char, or if both are null.
80      * Unicode and whitespace normalization is not performed before
81      * comparison. If the two nodes are not equal, a
82      * <code>ComparisonFailure</code> is thrown with the given
83      * message.
84      * </p>
85      *
86      * @param message printed if the texts are not equal
87      * @param expected the text the test should produce
88      * @param actual the text the test does produce
89      *
90      * @throws ComparisonFailure if the text nodes are not equal
91      */

92     public static void assertEquals(
93       String JavaDoc message, Text expected, Text actual) {
94         
95         if (actual == expected) return;
96         nullCheck(message, expected, actual);
97
98         assertEquals(message, expected.getValue(), actual.getValue());
99     }
100
101     
102     private static void nullCheck(String JavaDoc message, Node expected, Node actual) {
103         
104         if (expected == null) {
105             throw new ComparisonFailure(message, null, actual.toXML());
106         }
107         else if (actual == null) {
108             throw new ComparisonFailure(message, expected.toXML(), null);
109         }
110         
111     }
112
113
114     /**
115      * <p>
116      * Asserts that two attribute nodes are equal.
117      * Attribute nodes are considered equal if their
118      * qualified names, namespace URIs, and values
119      * are equal. The type is not considered because it tends not to
120      * survive a roundtrip. If the two nodes are not equal, a
121      * <code>ComparisonFailure</code> is thrown.
122      * </p>
123      *
124      * <p>
125      * There is special handling for the <code>xml:base</code>
126      * attribute. In order to facilitate comparison between relative
127      * and absolute URIs, two <code>xml:base</code> attributes are
128      * considered equal if one might be a relative form of the other.
129      * </p>
130      *
131      * @param expected the attribute the test should produce
132      * @param actual the attribute the test does produce
133      *
134      * @throws ComparisonFailure if the sttributes are not equal
135      */

136     public static void assertEquals(
137       Attribute expected, Attribute actual) {
138         assertEquals(null, expected, actual);
139     }
140
141     
142     /**
143      * <p>
144      * Asserts that two attribute nodes are equal.
145      * Attribute nodes are considered equal if their
146      * qualified names, namespace URIs, and values
147      * are equal. The type is not considered because this tends not to
148      * survive a roundtrip. If the two nodes are not equal, a
149      * <code>ComparisonFailure</code> is thrown with the given
150      * message.
151      * </p>
152      *
153      * <p>
154      * There is special handling for the <code>xml:base</code>
155      * attribute. In order to facilitate comparison between relative and
156      * absolute URIs, two <code>xml:base</code> attributes are
157      * considered equal if one might be a relative form of the other.
158      * </p>
159      *
160      * @param message printed if the attributes are not equal
161      * @param expected the attribute the test should produce
162      * @param actual the attribute the test does produce
163      *
164      * @throws ComparisonFailure if the attributes are not equal
165      */

166     public static void assertEquals(
167       String JavaDoc message, Attribute expected, Attribute actual) {
168         
169         if (actual == expected) return;
170         nullCheck(message, expected, actual);
171         
172         String JavaDoc value1 = expected.getValue();
173         String JavaDoc value2 = actual.getValue();
174         if ("xml:base".equals(expected.getQualifiedName())) {
175             // handle possibility that one is relative and other is not
176
if (value1.equals(value2)) return;
177             if (value1.startsWith("../")) {
178                 assertTrue(message, value2.endsWith(value1.substring(2)));
179             }
180             else {
181                 assertTrue(message,
182                   value1.endsWith('/' + value2) || value2.endsWith('/' + value1));
183             }
184         }
185         else {
186             assertEquals(message, value1, value2);
187             assertEquals(message, expected.getLocalName(), actual.getLocalName());
188             assertEquals(message,
189               expected.getQualifiedName(), actual.getQualifiedName()
190             );
191             assertEquals(message,
192               expected.getNamespaceURI(), actual.getNamespaceURI()
193             );
194         }
195
196     }
197
198     
199     /**
200      * <p>
201      * Asserts that two <code>DocType</code> nodes are equal.
202      * <code>DocType</code> nodes are considered equal if their
203      * root element names, public IDs, and system IDs
204      * are equal. The internal DTD subsets are not considered.
205      * If the two nodes are not equal, a
206      * <code>ComparisonFailure</code> is thrown.
207      * </p>
208      *
209      * @param expected the DOCTYPE declaration the test should produce
210      * @param actual the DOCTYPE declaration the test does produce
211      *
212      * @throws ComparisonFailure if the document type declarations
213      * are not equal
214      */

215     public static void assertEquals(DocType expected, DocType actual) {
216         assertEquals(null, expected, actual);
217     }
218
219     
220     /**
221      * <p>
222      * Asserts that two <code>DocType</code> nodes are equal.
223      * <code>DocType</code> nodes are considered equal if their
224      * root element name, public ID, and system ID
225      * are equal. The internal DTD subsets are not considered.
226      * If the two nodes are not equal, a
227      * <code>ComparisonFailure</code> is thrown with the given
228      * message.
229      * </p>
230      *
231      * @param message printed if the DOCTYPE declarations are not equal
232      * @param expected the DOCTYPE declaration the test should produce
233      * @param actual the DOCTYPE declaration the test does produce
234      *
235      * @throws ComparisonFailure if the document type declarations
236      * are not equal
237      *
238      */

239     public static void assertEquals(
240       String JavaDoc message, DocType expected, DocType actual) {
241         
242         if (actual == expected) return;
243         nullCheck(message, expected, actual);
244
245         assertEquals(message,
246           expected.getPublicID(),
247           actual.getPublicID()
248         );
249         assertEquals(message,
250           expected.getSystemID(),
251           actual.getSystemID()
252         );
253         assertEquals(message,
254           expected.getRootElementName(),
255           actual.getRootElementName()
256         );
257     }
258
259     
260     /**
261      * <p>
262      * Asserts that two element nodes are equal.
263      * Element nodes are considered equal if their
264      * qualified names, namespace URI, attributes,
265      * declared namespaces, and children
266      * are equal. Consecutive text node children are coalesced
267      * before the comparison is made. If the two nodes are not equal,
268      * a <code>ComparisonFailure</code> is thrown.
269      * </p>
270      *
271      * @param expected the element the test should produce
272      * @param actual the element the test does produce
273      *
274      * @throws ComparisonFailure if the elements are not equal
275      */

276     public static void assertEquals(
277       Element expected, Element actual) {
278         assertEquals(null, expected, actual);
279
280     }
281
282     
283     /**
284      * <p>
285      * Asserts that two element nodes are equal.
286      * Element nodes are considered equal if their
287      * qualified names, namespace URI, attributes,
288      * declared namespaces, and children
289      * are equal. Consecutive text node children are coalesced
290      * before the comparison is made. If the two nodes are not equal,
291      * a <code>ComparisonFailure</code> is thrown with the given
292      * message.
293      * </p>
294      *
295      * @param message printed if the elements are not equal
296      * @param expected the element the test should produce
297      * @param actual the element the test does produce
298      *
299      * @throws ComparisonFailure if the elements are not equal
300      */

301     public static void assertEquals(String JavaDoc message,
302       Element expected, Element actual) {
303         
304         if (actual == expected) return;
305         nullCheck(message, expected, actual);
306
307         assertEquals(message,
308           expected.getLocalName(),
309           actual.getLocalName()
310         );
311         assertEquals(message,
312           expected.getQualifiedName(),
313           actual.getQualifiedName()
314         );
315         assertEquals(message,
316           expected.getNamespaceURI(),
317           actual.getNamespaceURI()
318         );
319
320         assertEquals(message,
321           expected.getAttributeCount(),
322           actual.getAttributeCount()
323         );
324         
325         for (int i = 0; i < expected.getAttributeCount(); i++ ) {
326             Attribute att1 = expected.getAttribute(i);
327             Attribute att2
328               = actual.getAttribute(
329                 att1.getLocalName(),
330                 att1.getNamespaceURI()
331                 );
332             assertNotNull(message, att2);
333             assertEquals(message, att1, att2);
334         }
335
336         // Check declared namespaces by listing all the prefixes
337
// on element1 and making sure element2 gives the same value
338
// for those prefixes, and vice versa. This is necessary
339
// to handle a few weird cases that arise in XInclude
340
// when prefixes are declared multiple times, to account for
341
// the fact that some serializers may drop redundant
342
// namespace declarations.
343
for (int i = 0;
344              i < expected.getNamespaceDeclarationCount();
345              i++ ) {
346             String JavaDoc prefix1 = expected.getNamespacePrefix(i);
347             String JavaDoc uri1 = expected.getNamespaceURI(prefix1);
348             assertNotNull(message, actual.getNamespaceURI(prefix1));
349             assertEquals(message,
350               uri1, actual.getNamespaceURI(prefix1)
351             );
352         }
353         for (int i = 0;
354              i < actual.getNamespaceDeclarationCount();
355              i++ ) {
356             String JavaDoc prefix1 = actual.getNamespacePrefix(i);
357             String JavaDoc uri1 = actual.getNamespaceURI(prefix1);
358             assertNotNull(message, expected.getNamespaceURI(prefix1));
359             assertEquals(message,
360               uri1, expected.getNamespaceURI(prefix1)
361             );
362         }
363         
364         if (expected.getChildCount() != actual.getChildCount()) {
365            // combine text nodes; this modifies the elements
366
// but shouldn't be a big problem as long as
367
// this is only used for unit testing
368
combineTextNodes(expected);
369            combineTextNodes(actual);
370         }
371         assertEquals(message,
372           expected.getChildCount(), actual.getChildCount());
373         for (int i = 0; i < expected.getChildCount(); i++) {
374             Node child1 = expected.getChild(i);
375             Node child2 = actual.getChild(i);
376             assertEquals(message, child1, child2);
377         }
378
379     }
380     
381     
382     private static void combineTextNodes(Element element) {
383
384         for (int i = 0; i < element.getChildCount()-1; i++) {
385             Node child = element.getChild(i);
386             if (child instanceof Text) {
387                   Node followingSibling = element.getChild(i+1);
388                   if (followingSibling instanceof Text) {
389                       Text combined = new Text(child.getValue()
390                         + followingSibling.getValue());
391                       element.replaceChild(child, combined);
392                       element.removeChild(followingSibling);
393                       i--;
394                   }
395             }
396         }
397
398     }
399
400     
401     /**
402      * <p>
403      * Asserts that two document nodes are equal.
404      * Document nodes are considered equal if their
405      * children are equal. If the two nodes are not equal,
406      * a <code>ComparisonFailure</code> is thrown.
407      * </p>
408      *
409      * @param expected the document the test should produce
410      * @param actual the document the test does produce
411      *
412      * @throws ComparisonFailure if the documents are not equal
413      */

414     public static void assertEquals(
415       Document expected, Document actual) {
416         assertEquals(null, expected, actual);
417     }
418     
419     
420     /**
421      * <p>
422      * Asserts that two document nodes are equal.
423      * Document nodes are considered equal if their
424      * children are equal. If the two nodes are not equal,
425      * a <code>ComparisonFailure</code> is thrown with the given
426      * message.
427      * </p>
428      *
429      * @param message printed if the documents are not equal
430      * @param expected the document the test should produce
431      * @param actual the document the test does produce
432      *
433      * @throws ComparisonFailure if the documents are not equal
434      */

435     public static void assertEquals(
436       String JavaDoc message, Document expected, Document actual) {
437
438         if (actual == expected) return;
439         nullCheck(message, expected, actual);
440
441         assertEquals(message,
442           expected.getChildCount(),
443           actual.getChildCount()
444         );
445         for (int i = 0; i < actual.getChildCount(); i++) {
446             Node child1 = expected.getChild(i);
447             Node child2 = actual.getChild(i);
448             assertEquals(message, child1, child2);
449         }
450
451     }
452
453     
454      /**
455      * <p>
456      * Asserts that two comment nodes are equal. Comment nodes are
457      * considered equal if they are identical char by char, or if both
458      * are null. Unicode and whitespace normalization is not performed
459      * before comparison. If the two nodes are not equal, a
460      * <code>ComparisonFailure</code> is thrown.
461      * </p>
462      *
463      * @param expected the comment the test should produce
464      * @param actual the comment the test does produce
465      *
466      * @throws ComparisonFailure if the comments are not equal
467      */

468     public static void assertEquals(Comment expected, Comment actual) {
469         assertEquals(null, expected, actual);
470     }
471     
472     
473    /**
474      * <p>
475      * Asserts that two comment nodes are equal. Comment nodes are considered
476      * equal if they are identical char by char, or if both are null.
477      * Unicode and whitespace normalization is not performed before
478      * comparison. If the two nodes are not equal, a
479      * <code>ComparisonFailure</code> is thrown with the given
480      * message.
481      * </p>
482      *
483      * @param message printed if the comments are not equal
484      * @param expected the comment the test should produce
485      * @param actual the comment the test does produce
486      *
487      * @throws ComparisonFailure if the comments are not equal
488      */

489     public static void assertEquals(
490       String JavaDoc message, Comment expected, Comment actual) {
491         
492         if (actual == expected) return;
493         nullCheck(message, expected, actual);
494         assertEquals(message, expected.getValue(), actual.getValue());
495         
496     }
497     
498     
499     /**
500      * <p>
501      * Asserts that two processing instruction nodes are equal.
502      * Processing instruction nodes are considered
503      * equal if they have the same target and the same value.
504      * If the two nodes are not equal, a
505      * <code>ComparisonFailure</code> is thrown.
506      * </p>
507      *
508      * @param expected the processing instruction the test should produce
509      * @param actual the processing instruction the test does produce
510      *
511      * @throws ComparisonFailure if the processing instructions
512      * are not equal
513      */

514     public static void assertEquals(ProcessingInstruction expected,
515       ProcessingInstruction actual) {
516         assertEquals(null, expected, actual);
517     }
518     
519     
520     /**
521      * <p>
522      * Asserts that two processing instruction nodes are equal.
523      * Processing instruction nodes are considered
524      * equal if they have the same target and the same value.
525      * If the two nodes are not equal, a
526      * <code>ComparisonFailure</code> is thrown with the given
527      * message.
528      * </p>
529      *
530      * @param message printed if the processing instructions are
531      * not equal
532      * @param expected the processing instruction the test
533      * should produce
534      * @param actual the processing instruction the test does produce
535      *
536      * @throws ComparisonFailure if the processing instructions
537      * are not equal
538      */

539     public static void assertEquals(String JavaDoc message,
540       ProcessingInstruction expected,
541       ProcessingInstruction actual) {
542
543         if (actual == expected) return;
544         nullCheck(message, expected, actual);
545
546         assertEquals(message, expected.getValue(), actual.getValue());
547         assertEquals(message, expected.getTarget(), actual.getTarget());
548         
549     }
550
551     
552     /**
553      * <p>
554      * Asserts that two nodes are equal. If the two nodes are not
555      * equal a <code>ComparisonFailure</code> is thrown.
556      * The subclass is not considered. The basic XOM class
557      * is considered, but the subclass is not. For example,
558      * a <code>Text</code> object can be equal to an object that
559      * is an <code>HTMLText</code>, but it can never be equal to
560      * a <code>Comment</code>.
561      * </p>
562      *
563      * @param expected the node the test should produce
564      * @param actual the node the test does produce
565      *
566      * @throws ComparisonFailure if the nodes are not equal
567      */

568     public static void assertEquals(Node expected, Node actual) {
569         assertEquals(null, expected, actual);
570     }
571     
572     
573     /**
574      * <p>
575      * Asserts that two nodes are equal. If the two nodes are not
576      * equal a <code>ComparisonFailure</code> is thrown with the given
577      * message. The subclass is not considered. The basic XOM class
578      * is considered, but the subclass is not. For example,
579      * a <code>Text</code> object can be equal to an object that
580      * is an <code>HTMLText</code>, but it can never be equal to
581      * a <code>Comment</code>.
582      * </p>
583      *
584      * @param message printed if the nodes are not equal
585      * @param expected the node the test should produce
586      * @param actual the node the test does produce
587      *
588      * @throws ComparisonFailure if the nodes are not equal
589      */

590     public static void assertEquals(
591       String JavaDoc message, Node expected, Node actual) {
592         
593         if (actual == expected) return;
594         nullCheck(message, expected, actual);
595
596         try {
597             if (expected instanceof Document) {
598                 assertEquals(message, (Document) expected, (Document) actual);
599             }
600             else if (expected instanceof Element) {
601                 assertEquals(message, (Element) expected, (Element) actual);
602             }
603             else if (expected instanceof Text) {
604                 assertEquals(message, (Text) expected, (Text) actual);
605             }
606             else if (expected instanceof DocType) {
607                 assertEquals(message,
608                   (DocType) expected,
609                   (DocType) actual
610                 );
611             }
612             else if (expected instanceof Comment) {
613                 assertEquals(message,
614                   (Comment) expected,
615                   (Comment) actual
616                 );
617             }
618             else if (expected instanceof ProcessingInstruction) {
619                 assertEquals(message,
620                   (ProcessingInstruction) expected,
621                   (ProcessingInstruction) actual
622                 );
623             }
624             else {
625                 throw new IllegalArgumentException JavaDoc(
626                   "Unexpected node type "
627                   + expected.getClass().getName()
628                 );
629             }
630         }
631         catch (ClassCastException JavaDoc ex) {
632             fail(message + "; Mismatched node types: "
633              + expected.getClass().getName() + " != "
634              + actual.getClass().getName());
635         }
636         
637     }
638    
639     
640 }
641
Popular Tags