001 /*
002 * AnrlrYAMTest.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 * Tamara Polajnar 27-Apr-2005
011 */
012
013 package gate.wiki.antlr;
014
015 import java.io.BufferedReader;
016 import java.io.File;
017 import java.io.FileReader;
018 import java.io.FileWriter;
019 import java.io.IOException;
020 import java.io.InputStreamReader;
021 import java.io.Writer;
022 import java.net.URL;
023 import junit.framework.Assert;
024 import junit.framework.Test;
025 import junit.framework.TestCase;
026 import junit.framework.TestSuite;
027
028 /**
029 * JUnit testing for the YAM lexer generated by Antlr.
030 *
031 * @author tamara
032 *
033 */
034 public class AntlrYAMTest extends TestCase {
035
036 private InputStreamReader in, correct;
037 private Writer out;
038 URL inFile, correctFile;
039 File outFile;
040
041 public AntlrYAMTest(String name) {
042 super(name);
043 }
044
045 public static void main(String[] args) {
046 junit.textui.TestRunner.run(suite());
047 }
048
049 public void setUp() throws Exception {
050 }
051
052 public static Test suite(){
053 TestSuite suite = new TestSuite();
054 suite.addTest(new AntlrYAMTest("testTranslate"));
055 suite.addTest(new AntlrYAMTest("testTranslateAgain"));
056 return suite;
057 }
058
059 /**
060 * Tests translation from YAM to HTML
061 * @throws IOException
062 */
063 public void testTranslate() throws IOException {
064
065 inFile = this.getClass().getResource("/tests/test.yam");
066 outFile = File.createTempFile("test", "html");
067
068 correctFile = this.getClass().getResource("/tests/correct.html");
069
070 in = new InputStreamReader(inFile.openStream());
071
072 out = new FileWriter(outFile);
073 Tool.run(in, out, Tool.HTML);
074 in = new FileReader(outFile);
075 correct = new InputStreamReader(correctFile.openStream());
076 Assert.assertTrue(
077 compare(new BufferedReader(in), new BufferedReader(correct))
078 );
079 }
080
081 /**
082 * Needed to make sure that code runs correctly if it's invoked twice
083 * @throws IOException
084 */
085 public void testTranslateAgain() throws IOException {
086
087 inFile = this.getClass().getResource("/tests/test.yam");
088 outFile = File.createTempFile("test","html");
089 correctFile = this.getClass().getResource("/tests/correct.html");
090
091 in = new InputStreamReader(inFile.openStream());
092
093 out = new FileWriter(outFile);
094 Tool.run(in, out, Tool.HTML);
095 in = new FileReader(outFile);
096 correct = new InputStreamReader(correctFile.openStream());
097 Assert.assertTrue(
098 compare(new BufferedReader(in), new BufferedReader(correct))
099 );
100 }
101
102 public boolean compare(BufferedReader test, BufferedReader correct)
103 throws IOException {
104 String t, c;
105 int lineNo = 0;
106 try{
107 while((t = test.readLine())!=null){
108 ++ lineNo;
109 if((c = correct.readLine())!= null) {
110 if(!t.equals(c)) {
111 System.err.println(
112 "Line " + lineNo + ": " + c +
113 "\n Disagrees with line in test file:" + t
114 );
115 return false;
116 }
117
118 }
119 else {
120 System.err.println("Error in line " + lineNo + ": " + c);
121 return false;
122 }
123 }
124 } catch(IOException e) {
125 // e.printStackTrace();
126 throw new IOException();
127 }
128 return true;
129 }
130
131 }
|