/* * AbstractTranslatorTest.java * Copyright (c) 1998-2008, The University of Sheffield. * * This code is from the GATE project (http://gate.ac.uk/) and is free * software licenced under the GNU General Public License version 3. It is * distributed without any warranty. For more details see COPYING.txt in the * top level directory (or at http://gatewiki.sf.net/COPYING.txt). * * Hamish Cunningham, 2nd May 2006 */ package gate.yam; import java.io.*; import java.net.*; import java.util.*; import junit.framework.*; import org.apache.log4j.Logger; import org.springframework.util.StringUtils; import gate.util.*; import gate.util.profile.*; /** * Unit test for translators. */ abstract public class AbstractTranslatorTest extends TestCase { /** Paths of example test files. */ abstract public String[] getTestFilePaths(); /** Resource directory name. */ static final String resDir = "/gate/yam/resources"; /** Suffix of input files. */ abstract public String getInputSuffix(); /** Suffix of output files. */ abstract public String[] getOutputSuffixes(); /** Encoding of key and response files. */ String encoding = "UTF-8"; /** Create the test case */ public AbstractTranslatorTest(String testName) { super(testName); } /** Logger */ static Logger lgr = Logger.getLogger("gate.yam.translate"); /** SInS (plain) logger */ static Logger slgr = Logger.getLogger("gate.sins"); /** * Test using all the example files. */ public void testAll() throws Exception { lgr.info("============= AbstractTranslatorTest.testAll() ============="); // start profiling double totalDocLength = 0; int docs = 0; Profiler prof = new Profiler(); prof.enableGCCalling(true); prof.printToSystemOut(true); prof.initRun("AbstractTranslatorTest.testAll() started"); prof.printToSystemOut(true); prof.checkPoint("", new String[0], false, false, false); List<String> failureMessages = new ArrayList<String>(); // iterate on the paths for(int i=0; i<getTestFilePaths().length; i++) { // iterate on the output types for(int j=0; j<getOutputSuffixes().length; j++) { // path, suffixes String path = getTestFilePaths()[i]; String inSuffix = getInputSuffix(); String outSuffix = getOutputSuffixes()[j]; // report //lgr.info( System.out.println( "testing translation of " + path + " from " + inSuffix + " to " + outSuffix ); // a reader for the test file InputStream testFileStream = this.getClass().getResourceAsStream(resDir + path + "." + inSuffix); if(testFileStream == null) fail("couldn't get resource " + path + "." + inSuffix); BufferedReader testReader = new BufferedReader(new InputStreamReader(testFileStream, encoding)); // get the key string String resName = resDir + path + "." + outSuffix; String key = YamTestUtils.getResourceAsString(resName, encoding); if(key == null) { lgr.info("*** couldn't get resource " + resName + " - ignoring ***"); System.out.println( "*** couldn't get resource " + resName + " - ignoring ***" ); continue; } // run the translator and get the response prof.checkPoint("", new String[0], false, false, true); StringWriter responseWriter = new StringWriter(); doTranslation( testReader, responseWriter, outSuffix, resDir + path ); String response = responseWriter.toString(); prof.checkPoint( "Translated " + path, new String[] { "translation", outSuffix }, false, false, true ); // update the profiler averages (num docs, content length) docs++; URL u = this.getClass().getResource(resDir + path + "." + inSuffix); File f = new File(u.getPath()); long inputLength = f.length(); //lgr.info("***************** " + inputLength + " ************"); totalDocLength += inputLength; // key and response should be identical boolean correctResponse = (response.compareTo(key) == 0); if(!correctResponse) { String errorPath = "." + path + "." + outSuffix + "-error"; FileWriter fwriter = new FileWriter(errorPath); fwriter.write(response); fwriter.close(); failureMessages.add( "response not equal to key for test file " + path + Strings.getNl() + " error response in " + errorPath ); } } // j loop } // i loop prof.checkPoint("Done", new String[0], false, false, true); lgr.info( "Docs processsed = " + docs + "; size = " + totalDocLength + "kb" ); //TODO System.out.println( "Docs processsed = " + docs + "; size = " + totalDocLength + "kb" ); prof.printCategAvg("translation", docs, totalDocLength, "kb"); /*prof.printCategAvg("tex", docs, totalDocLength, "kb"); prof.printCategAvg("html", docs, totalDocLength, "kb"); prof.printCategAvg("tree", docs, totalDocLength, "kb");*/ lgr.info("============================================================"); if(!failureMessages.isEmpty()) { fail(StringUtils.collectionToDelimitedString(failureMessages, "\n")); } } // testAll /** Run the translator and get the response */ abstract public Writer doTranslation( Reader testReader, Writer responseWriter, String outputType, String testName ) throws Exception; }