001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 12 July 2002
011 *
012 * $Id: TestEqual.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 */
014
015 package gate.util;
016
017 import gate.*;
018
019 /**
020 * This class provides some static utility methods such as equality test for
021 * annotation sets and documents. They are mainly used by test classes.
022 */
023 public class TestEqual {
024 /**
025 * Checks two documents for equality.
026 * @param doc1 a document
027 * @param doc2 another document
028 * @return a boolean.
029 */
030 public static boolean documentsEqual(Document doc1, Document doc2){
031 message = "";
032 if(doc1 == null ^ doc2 == null){
033 message = "Documents not equal: null<>non-null!";
034 return false;
035 }
036 if(doc1 == null) return true;
037 if(! check(doc1.getContent(), doc2.getContent())){
038 message = "Document contents different!";
039 return false;
040 }
041
042 if(! check(doc1.getAnnotations(), doc2.getAnnotations())){
043 message = "Documents default AS not equal!";
044 return false;
045 }
046
047 if(doc1 instanceof TextualDocument){
048 if(doc2 instanceof TextualDocument){
049 if(! check(((TextualDocument)doc1).getEncoding(),
050 ((TextualDocument)doc2).getEncoding())){
051 message = "Textual documents with different encodings!";
052 return false;
053 }
054 }else{
055 message = "Documents not equal: textual<>non-textual!";
056 return false;
057 }
058 }
059 if(! check(doc1.getFeatures(), doc2.getFeatures())){
060 message = "Documents features not equal!";
061 return false;
062 }
063
064 //needs friend declaration :(
065 // if(!markupAware.equals(doc.markupAware)) return false;
066
067 if(! check(doc1.getNamedAnnotationSets(),
068 doc2.getNamedAnnotationSets())){
069 message = "Documents named annots not equal!";
070 return false;
071 }
072
073 // if(doc1 instanceof DocumentImpl){
074 // if(doc2 instanceof DocumentImpl){
075 // if(! check(((DocumentImpl)doc1).getNextNodeId(),
076 // ((DocumentImpl)doc2).getNextNodeId())){
077 // message = "Documents next nodeID not equal!";
078 // return false;
079 // }
080 // if(! check(((DocumentImpl)doc1).getNextAnnotationId(),
081 // ((DocumentImpl)doc2).getNextAnnotationId())){
082 // message = "Documents next annotationIDs not equal!";
083 // return false;
084 // }
085 // }else{
086 // message = "Documents not equal: DocumentImpl<>non-DocumentImpl!";
087 // return false;
088 // }
089 // }
090
091 if(! check(doc1.getSourceUrl(), doc2.getSourceUrl())){
092 message = "Documents sourceURLs not equal!";
093 return false;
094 }
095 if(! (check(doc1.getSourceUrlStartOffset(),
096 doc2.getSourceUrlStartOffset())
097 &&
098 check(doc1.getSourceUrlEndOffset(),
099 doc2.getSourceUrlEndOffset()))){
100 message = "Documents sourceURLOffsets not equal!";
101 return false;
102 }
103 return true;
104 }
105
106 /** Two AnnotationSet are equal if their name, the documents of which belong
107 * to the AnnotationSets and annotations from the sets are the same
108 */
109 public static boolean annotationSetsEqual(AnnotationSet as1,
110 AnnotationSet as2) {
111 if(as1 == null ^ as2 == null) return false;
112 if(as1 == null) return true;
113 //Sets equality
114 if(as1.size() != as2.size()) return false;
115 try{
116 if(! as1.containsAll(as2)) return false;
117 }catch(ClassCastException unused) {
118 return false;
119 }catch(NullPointerException unused) {
120 return false;
121 }
122
123 //removed to prevent infinite looping in testDocumentsEqual()
124 // // verify the documents which they belong to
125 // if (! check (as1.getDocument(), as2.getDocument())) return false;
126
127 // verify the name of the AnnotationSets
128 if (! check(as1.getName(), as2.getName())) return false;
129 return true;
130 } // equals
131
132
133
134
135 /** Check: test 2 objects for equality */
136 static protected boolean check(Object a, Object b) {
137 if(a == null || b == null) return a == b;
138 else return a.equals(b);
139 } // check(a,b)
140
141 /**
142 * If set to true, explanation messages will be printed when a test fails.
143 */
144 public static String message = "";
145 }
|