001 package gate.xml;
002
003 /**
004 * <p>Title: TestRepositioningInfo.java </p>
005 * <p>Description: Test to check if RepositioningInfo works. </p>
006 * <p>Company: University Of Sheffield</p>
007 * @author Niraj Aswani
008 * @version 1.0
009 */
010
011 import junit.framework.*;
012 import gate.*;
013 import gate.creole.*;
014 import gate.corpora.*;
015 import gate.util.BomStrippingInputStreamReader;
016
017 import java.net.*;
018 import java.io.*;
019
020 /**
021 * This class tests if Repositinioning Information works.
022 * It creates a document using an inline xml file with preserveOriginalContent
023 * and collectRepositioningInfo options keeping true, which has all
024 * sorts of special entities like &, " etc. + it contains both
025 * kind of unix and dos types new line characters. It then saves the
026 * document to the temporary location on the disk using
027 * "save preserving document format" option and then compares the contents of
028 * both the original and the temporary document to see if they are equal.
029 */
030 public class TestRepositioningInfo
031 extends TestCase {
032
033 /** Constructor */
034 public TestRepositioningInfo(String dummy) {
035 super(dummy);
036 }
037
038 /**
039 * This method sets up the parameters for the files to be tested
040 */
041 protected void setUp() {
042
043 testFile = TestDocument.getTestServerName() + "tests/test-inline.xml";
044
045 // creating documents
046 try {
047 FeatureMap params = Factory.newFeatureMap();
048 params.put("sourceUrl",new URL(testFile));
049 params.put("preserveOriginalContent", new Boolean("true"));
050 params.put("collectRepositioningInfo", new Boolean("true"));
051 doc = (Document) Factory.createResource("gate.corpora.DocumentImpl",params);
052 }
053 catch (MalformedURLException murle) {
054 fail("Document cannot be created ");
055 }
056 catch (ResourceInstantiationException rie) {
057 fail("Resources cannot be created for the test document");
058 }
059 }
060
061 /** Fixture tear down - removes the document resource */
062 public void tearDown() throws Exception {
063 Factory.deleteResource(doc);
064 } // tearDown
065
066
067 /**
068 * This method tests if Repositinioning Information works.
069 * It creates a document using an xml file with preserveOriginalContent
070 * and collectRepositioningInfo options keeping true and which has all
071 * sorts of special entities like &, " etc. + it contains both
072 * kind of unix and dos stype new line characters. It then saves the
073 * document to the temporary location on the disk using
074 * "save preserving document format" option and then compares the contents of
075 * both the original and the temporary document to see if they are equal.
076 * @throws java.lang.Exception
077 */
078 public void testRepositioningInfo() throws Exception {
079
080 // here we need to save the document to the file
081 String encoding = ((DocumentImpl)doc).getEncoding();
082 File outputFile = File.createTempFile("test-inline1","xml");
083 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),encoding);
084 writer.write(((gate.Document)doc).toXml(null, true));
085 writer.flush();
086 writer.close();
087 Reader readerForSource = new BomStrippingInputStreamReader(new URL(testFile).openStream(),encoding);
088 Reader readerForDesti = new BomStrippingInputStreamReader(new FileInputStream(outputFile),encoding);
089 while(true) {
090 int input1 = readerForSource.read();
091 int input2 = readerForDesti.read();
092 if(input1 < 0 || input2 < 0) {
093 assertTrue(input1 < 0 && input2 < 0);
094 readerForSource.close();
095 readerForDesti.close();
096 outputFile.delete();
097 return;
098 } else {
099 assertEquals(input1,input2);
100 }
101 }
102 }
103
104 /** Test suite routine for the test runner */
105 public static Test suite() {
106 return new TestSuite(TestRepositioningInfo.class);
107 } // suite
108
109
110 /** A test file URL */
111 private String testFile = "";
112
113 /** Document instance */
114 private Document doc = null;
115
116 }
|