| TestEqual.java |
1 /*
2 * Copyright (c) 1998-2005, The University of Sheffield.
3 *
4 * This file is part of GATE (see http://gate.ac.uk/), and is free
5 * software, licenced under the GNU Library General Public License,
6 * Version 2, June 1991 (in the distribution as file licence.html,
7 * and also available at http://gate.ac.uk/gate/licence.html).
8 *
9 * Valentin Tablan 12 July 2002
10 *
11 * $Id: TestEqual.java,v 1.5 2005/01/11 13:51:37 ian Exp $
12 */
13
14 package gate.util;
15
16 import gate.*;
17
18 /**
19 * This class provides some static utility methods such as equality test for
20 * annotation sets and documents. They are mainly used by test classes.
21 */
22 public class TestEqual {
23 /**
24 * Checks two documents for equality.
25 * @param doc1 a document
26 * @param doc2 another document
27 * @return a boolean.
28 */
29 public static boolean documentsEqual(Document doc1, Document doc2){
30 message = "";
31 if(doc1 == null ^ doc2 == null){
32 message = "Documents not equal: null<>non-null!";
33 return false;
34 }
35 if(doc1 == null) return true;
36 if(! check(doc1.getContent(), doc2.getContent())){
37 message = "Document contents different!";
38 return false;
39 }
40
41 if(! check(doc1.getAnnotations(), doc2.getAnnotations())){
42 message = "Documents default AS not equal!";
43 return false;
44 }
45
46 if(doc1 instanceof TextualDocument){
47 if(doc2 instanceof TextualDocument){
48 if(! check(((TextualDocument)doc1).getEncoding(),
49 ((TextualDocument)doc2).getEncoding())){
50 message = "Textual documents with different encodings!";
51 return false;
52 }
53 }else{
54 message = "Documents not equal: textual<>non-textual!";
55 return false;
56 }
57 }
58 if(! check(doc1.getFeatures(), doc2.getFeatures())){
59 message = "Documents features not equal!";
60 return false;
61 }
62
63 //needs friend declaration :(
64 // if(!markupAware.equals(doc.markupAware)) return false;
65
66 if(! check(doc1.getNamedAnnotationSets(),
67 doc2.getNamedAnnotationSets())){
68 message = "Documents named annots not equal!";
69 return false;
70 }
71
72 // if(doc1 instanceof DocumentImpl){
73 // if(doc2 instanceof DocumentImpl){
74 // if(! check(((DocumentImpl)doc1).getNextNodeId(),
75 // ((DocumentImpl)doc2).getNextNodeId())){
76 // message = "Documents next nodeID not equal!";
77 // return false;
78 // }
79 // if(! check(((DocumentImpl)doc1).getNextAnnotationId(),
80 // ((DocumentImpl)doc2).getNextAnnotationId())){
81 // message = "Documents next annotationIDs not equal!";
82 // return false;
83 // }
84 // }else{
85 // message = "Documents not equal: DocumentImpl<>non-DocumentImpl!";
86 // return false;
87 // }
88 // }
89
90 if(! check(doc1.getSourceUrl(), doc2.getSourceUrl())){
91 message = "Documents sourceURLs not equal!";
92 return false;
93 }
94 if(! (check(doc1.getSourceUrlStartOffset(),
95 doc2.getSourceUrlStartOffset())
96 &&
97 check(doc1.getSourceUrlEndOffset(),
98 doc2.getSourceUrlEndOffset()))){
99 message = "Documents sourceURLOffsets not equal!";
100 return false;
101 }
102 return true;
103 }
104
105 /** Two AnnotationSet are equal if their name, the documents of which belong
106 * to the AnnotationSets and annotations from the sets are the same
107 */
108 public static boolean annotationSetsEqual(AnnotationSet as1,
109 AnnotationSet as2) {
110 if(as1 == null ^ as2 == null) return false;
111 if(as1 == null) return true;
112 //Sets equality
113 if(as1.size() != as2.size()) return false;
114 try{
115 if(! as1.containsAll(as2)) return false;
116 }catch(ClassCastException unused) {
117 return false;
118 }catch(NullPointerException unused) {
119 return false;
120 }
121
122 //removed to prevent infinite looping in testDocumentsEqual()
123 // // verify the documents which they belong to
124 // if (! check (as1.getDocument(), as2.getDocument())) return false;
125
126 // verify the name of the AnnotationSets
127 if (! check(as1.getName(), as2.getName())) return false;
128 return true;
129 } // equals
130
131
132
133
134 /** Check: test 2 objects for equality */
135 static protected boolean check(Object a, Object b) {
136 if(a == null || b == null) return a == b;
137 else return a.equals(b);
138 } // check(a,b)
139
140 /**
141 * If set to true, explanation messages will be printed when a test fails.
142 */
143 public static String message = "";
144 }