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