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.1 2002/07/12 13:24:30 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      if(doc1 instanceof DocumentImpl){
76        if(doc2 instanceof DocumentImpl){
77          if(! check(((DocumentImpl)doc1).getNextNodeId(),
78                     ((DocumentImpl)doc2).getNextNodeId())){
79            message = "Documents next nodeID not equal!";
80            return false;
81          }
82          if(! check(((DocumentImpl)doc1).getNextAnnotationId(),
83                     ((DocumentImpl)doc2).getNextAnnotationId())){
84            message = "Documents next annotationIDs not equal!";
85            return false;
86          }
87        }else{
88          message = "Documents not equal: DocumentImpl<>non-DocumentImpl!";
89          return false;
90        }
91      }
92  
93      if(! check(doc1.getSourceUrl(), doc2.getSourceUrl())){
94        message = "Documents sourceURLs not equal!";
95        return false;
96      }
97      if(! (check(doc1.getSourceUrlStartOffset(),
98                 doc2.getSourceUrlStartOffset())
99           &&
100          check(doc1.getSourceUrlEndOffset(),
101                doc2.getSourceUrlEndOffset()))){
102       message = "Documents sourceURLOffsets not equal!";
103       return false;
104     }
105     return true;
106   }
107 
108   /** Two AnnotationSet are equal if their name, the documents of which belong
109     *  to the AnnotationSets and annotations from the sets are the same
110     */
111   public static boolean annotationSetsEqual(AnnotationSet as1,
112                                             AnnotationSet as2) {
113     if(as1 == null ^ as2 == null) return false;
114     if(as1 == null) return true;
115     //Sets equality
116     if(as1.size() != as2.size()) return false;
117     try{
118       if(! as1.containsAll(as2)) return false;
119     }catch(ClassCastException unused)   {
120       return false;
121     }catch(NullPointerException unused) {
122       return false;
123     }
124 
125 //removed to prevent infinite looping in testDocumentsEqual()
126 //    // verify the documents which they belong to
127 //    if (! check (as1.getDocument(), as2.getDocument())) return false;
128 
129     // verify the name of the AnnotationSets
130     if (! check(as1.getName(), as2.getName())) return false;
131     return true;
132   } // equals
133 
134 
135 
136 
137   /** Check: test 2 objects for equality */
138   static protected boolean check(Object a, Object b) {
139     if(a == null || b == null) return a == b;
140     else return a.equals(b);
141   } // check(a,b)
142 
143   /**
144    * If set to true, explanation messages will be printed when a test fails.
145    */
146   public static String message = "";
147 }