1   package gate.xml;
2   
3   /**
4    * <p>Title: TestRepositioningInfo.java </p>
5    * <p>Description: Test to check if RepositioningInfo works. </p>
6    * <p>Company: University Of Sheffield</p>
7    * @author Niraj Aswani
8    * @version 1.0
9    */
10  
11  import junit.framework.*;
12  import gate.*;
13  import gate.creole.*;
14  import gate.corpora.*;
15  import java.net.*;
16  import java.io.*;
17  
18  /**
19   * This class tests if Repositinioning Information works.
20   * It creates a document using an inline xml file with preserveOriginalContent
21   * and collectRepositioningInfo options keeping true, which has all
22   * sorts of special entities like &amp, &quot etc. + it contains both
23   * kind of unix and dos types new line characters.  It then saves the
24   * document to the temporary location on the disk using
25   * "save preserving document format" option and then compares the contents of
26   * both the original and the temporary document to see if they are equal.
27   */
28  public class TestRepositioningInfo
29      extends TestCase {
30  
31    /** Constructor */
32    public TestRepositioningInfo(String dummy) {
33      super(dummy);
34    }
35  
36    /**
37     * This method sets up the parameters for the files to be tested
38     */
39    protected void setUp() {
40  
41      testFile = TestDocument.getTestServerName() + "tests/test-inline.xml";
42  
43      // creating documents
44      try {
45        FeatureMap params = Factory.newFeatureMap();
46        params.put("sourceUrl",new URL(testFile));
47        params.put("preserveOriginalContent", new Boolean("true"));
48        params.put("collectRepositioningInfo", new Boolean("true"));
49        doc = (Document) Factory.createResource("gate.corpora.DocumentImpl",params);
50      }
51      catch (MalformedURLException murle) {
52        fail("Document cannot be created ");
53      }
54      catch (ResourceInstantiationException rie) {
55        fail("Resources cannot be created for the test document");
56      }
57    }
58  
59    /** Fixture tear down - removes the document resource */
60    public void tearDown() throws Exception {
61      Factory.deleteResource(doc);
62    } // tearDown
63  
64  
65    /**
66     * This method tests if Repositinioning Information works.
67     * It creates a document using an xml file with preserveOriginalContent
68     * and collectRepositioningInfo options keeping true and which has all
69     * sorts of special entities like &amp, &quot etc. + it contains both
70     * kind of unix and dos stype new line characters.  It then saves the
71     * document to the temporary location on the disk using
72     * "save preserving document format" option and then compares the contents of
73     * both the original and the temporary document to see if they are equal.
74     * @throws java.lang.Exception
75     */
76    public void testRepositioningInfo() throws Exception {
77  
78      // here we need to save the document to the file
79        String encoding = ((DocumentImpl)doc).getEncoding();
80        File outputFile = File.createTempFile("test-inline1","xml");
81        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),encoding);
82        writer.write(((gate.Document)doc).toXml(null, true));
83        writer.flush();
84        writer.close();
85        InputStreamReader readerForSource = new InputStreamReader(new URL(testFile).openStream(),encoding);
86        InputStreamReader readerForDesti = new InputStreamReader(new FileInputStream(outputFile),encoding);
87        while(true) {
88          int input1 = readerForSource.read();
89          int input2 = readerForDesti.read();
90          if(input1 < 0 || input2 < 0) {
91            assertTrue(input1 < 0 && input2 < 0);
92            readerForSource.close();
93            readerForDesti.close();
94            outputFile.delete();
95            return;
96          } else {
97            assertEquals(input1,input2);
98          }
99        }
100   }
101 
102   /** Test suite routine for the test runner */
103   public static Test suite() {
104     return new TestSuite(TestRepositioningInfo.class);
105   } // suite
106 
107 
108   /** A test file URL */
109   private String testFile = "";
110 
111   /** Document instance */
112   private Document doc = null;
113 
114 }