001 /*
002 * AbstractTranslatorTest.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham, 2nd May 2006
011 */
012
013 package gate.yam;
014
015 import java.io.*;
016 import java.net.*;
017 import junit.framework.*;
018 import org.apache.log4j.Logger;
019 import gate.util.*;
020 import gate.util.profile.*;
021
022
023 /**
024 * Unit test for translators.
025 */
026 abstract public class AbstractTranslatorTest extends TestCase
027 {
028
029 /** Paths of example test files. */
030 abstract public String[] getTestFilePaths();
031
032 /** Resource directory name. */
033 static final String resDir = "/gate/yam/resources";
034
035 /** Suffix of input files. */
036 abstract public String getInputSuffix();
037
038 /** Suffix of output files. */
039 abstract public String[] getOutputSuffixes();
040
041 /** Encoding of key and response files. */
042 String encoding = "UTF-8";
043
044 /** Create the test case */
045 public AbstractTranslatorTest(String testName) {
046 super(testName);
047 }
048
049 /** Logger */
050 static Logger lgr = Logger.getLogger("gate.yam.translate");
051
052 /** SInS (plain) logger */
053 static Logger slgr = Logger.getLogger("gate.sins");
054
055 /**
056 * Test using all the example files.
057 */
058 public void testAll() throws Exception
059 {
060 lgr.info("============= AbstractTranslatorTest.testAll() =============");
061 // start profiling
062 double totalDocLength = 0;
063 int docs = 0;
064 Profiler prof = new Profiler();
065 prof.enableGCCalling(true);
066 prof.printToSystemOut(true);
067 prof.initRun("AbstractTranslatorTest.testAll() started");
068 prof.printToSystemOut(true);
069 prof.checkPoint("", new String[0], false, false, false);
070
071 // iterate on the paths
072 for(int i=0; i<getTestFilePaths().length; i++)
073 {
074 // iterate on the output types
075 for(int j=0; j<getOutputSuffixes().length; j++)
076 {
077 // path, suffixes
078 String path = getTestFilePaths()[i];
079 String inSuffix = getInputSuffix();
080 String outSuffix = getOutputSuffixes()[j];
081
082 // report
083 //lgr.info(
084 System.out.println(
085 "testing translation of " + path + " from " + inSuffix +
086 " to " + outSuffix
087 );
088
089 // a reader for the test file
090 InputStream testFileStream =
091 this.getClass().getResourceAsStream(resDir + path + "." + inSuffix);
092 if(testFileStream == null)
093 fail("couldn't get resource " + path + "." + inSuffix);
094 BufferedReader testReader =
095 new BufferedReader(new InputStreamReader(testFileStream, encoding));
096
097 // get the key string
098 String resName = resDir + path + "." + outSuffix;
099 String key = YamTestUtils.getResourceAsString(resName, encoding);
100 if(key == null) {
101 lgr.info("*** couldn't get resource " + resName + " - ignoring ***");
102 System.out.println(
103 "*** couldn't get resource " + resName + " - ignoring ***"
104 );
105 continue;
106 }
107
108 // run the translator and get the response
109 prof.checkPoint("", new String[0], false, false, true);
110 StringWriter responseWriter = new StringWriter();
111 doTranslation(
112 testReader, responseWriter, outSuffix, resDir + path
113 );
114 String response = responseWriter.toString();
115 prof.checkPoint(
116 "Translated " + path, new String[] { "translation", outSuffix },
117 false, false, true
118 );
119
120 // update the profiler averages (num docs, content length)
121 docs++;
122 URL u = this.getClass().getResource(resDir + path + "." + inSuffix);
123 File f = new File(u.getPath());
124 long inputLength = f.length();
125 //lgr.info("***************** " + inputLength + " ************");
126 totalDocLength += inputLength;
127
128 // key and response should be identical
129 boolean correctResponse = (response.compareTo(key) == 0);
130 if(!correctResponse) {
131 String errorPath =
132 "." + path + "." + outSuffix + "-error";
133 FileWriter fwriter = new FileWriter(errorPath);
134 fwriter.write(response);
135 fwriter.close();
136 fail(
137 "response not equal to key for test file " + path +
138 Strings.getNl() + " error response in " + errorPath
139 );
140 }
141 } // j loop
142 } // i loop
143
144 prof.checkPoint("Done", new String[0], false, false, true);
145 lgr.info(
146 "Docs processsed = " + docs + "; size = " + totalDocLength + "kb"
147 );
148 //TODO
149 System.out.println(
150 "Docs processsed = " + docs + "; size = " + totalDocLength + "kb"
151 );
152 prof.printCategAvg("translation", docs, totalDocLength, "kb");
153 /*prof.printCategAvg("tex", docs, totalDocLength, "kb");
154 prof.printCategAvg("html", docs, totalDocLength, "kb");
155 prof.printCategAvg("tree", docs, totalDocLength, "kb");*/
156 lgr.info("============================================================");
157 } // testAll
158
159 /** Run the translator and get the response */
160 abstract public Writer
161 doTranslation(
162 Reader testReader, Writer responseWriter,
163 String outputType, String testName
164 ) throws Exception;
165 }
|