001 /*
002 * YamTest.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 February 2007
011 */
012
013 package gate.yam;
014
015 import java.io.*;
016 import junit.framework.*;
017 import org.apache.log4j.Logger;
018 import org.springframework.core.io.*;
019 import org.springframework.context.support.*;
020 import gate.util.*;
021 import gate.yam.*;
022 import gate.yam.parse.*;
023 import gate.yam.translate.*;
024 import gate.yam.convert.*;
025
026
027 /**
028 * Unit test for the YamFile class.
029 */
030 public class YamTests extends TestCase
031 {
032
033 /** Create the test case */
034 public YamTests(String testName) {
035 super(testName);
036 }
037
038 /** Logger */
039 static Logger log = Logger.getLogger("gate.yam.YamTest");
040
041 /** The URL of the bibliography file used for citations in tests */
042 private static String BIB_URL = "http://gate.ac.uk/sale/bib/main.html";
043
044 /** The anchor prefix used for citations in tests */
045 private static String BIB_ANCHOR_PREFIX = "X";
046
047 /** Resource directory name. */
048 static final String resDir = AbstractTranslatorTest.resDir;
049
050 /**
051 * Test file-system translate method. Takes yam-again.yam and generates
052 * an html file on disk from it, yam-again.html. Compares this with
053 * yam-minimal.html. The comparison assumes that yam-again.yam is a copy of
054 * yam-minimal.yam
055 *
056 */
057 public void testFSTranslate() throws Exception
058 {
059 // report
060 log.info("=============================================================");
061 log.info("============= YamTest.testFSTranslate() =====================");
062 log.info("testing file-system-based translate");
063 log.debug("**NOTE**: yam-again.yam should be copy of yam-minimal.yam");
064
065 // path, suffixes
066 String path = "/yam-again.yam";
067 String keyPath = "/yam-again.html";
068 String responsePath = "/yam-minimal.html";
069
070 // get the spring context and get an FSR from the path
071 ClassPathXmlApplicationContext factory = new
072 ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
073 FileSystemResource yamLocation =
074 new FileSystemResource(factory.getResource(resDir + path).getFile());
075
076 // the yam factory method: finding a .yam
077 log.info("yamLocation = " + yamLocation.getDescription());
078 YamFile yam = YamFile.get(yamLocation);
079 if(yam == null) fail("yam == null");
080 log.info("yam.getLocation().getPath() = " + yam.getLocation().getPath());
081 assertEquals(
082 "path != yam.getLocation.getPath()", yamLocation.getPath(),
083 yam.getLocation().getPath()
084 );
085
086 // the yam factory method: finding a .yam via a .html
087 FileSystemResource htmlLocation = new
088 FileSystemResource(factory.getResource(resDir + responsePath).getFile());
089 YamFile yamFromHtml = YamFile.get(htmlLocation);
090 log.info("yamFromHtml = " + yamFromHtml.getLocation().getDescription());
091 htmlLocation = new FileSystemResource("/nonexistant.html");
092 yamFromHtml = YamFile.get(htmlLocation);
093 assertNull("yamFromHtml should be null", yamFromHtml);
094
095 // check that the dependent html needs regeneration
096 StringBuilder yamAgainYamPath =
097 new StringBuilder(yamLocation.getFile().getPath());
098 int len = yamAgainYamPath.length();
099 String yamAgainHtmlPath =
100 yamAgainYamPath.replace(
101 len - 4, len, YamFile.FileType.HTML.suffix()
102 ).toString();
103 (new File(yamAgainHtmlPath)).delete();
104 FileSystemResource yamAgainHtmlLocation =
105 new FileSystemResource(yamAgainHtmlPath);
106 log.debug("yamAgainHtmlLocation= "+yamAgainHtmlLocation.getDescription());
107 YamFile yamForRegen = YamFile.needsGeneration(yamAgainHtmlLocation);
108 assertNotNull("yamForRegen is null", yamForRegen);
109
110 // check that the non-existent dependent html doesn't need generation
111 log.debug("htmlLocation= "+htmlLocation.getDescription());
112 yamForRegen = YamFile.needsGeneration(htmlLocation);
113 assertNull("yamForRegen is not null", yamForRegen);
114
115 // config, run the translator and get the response
116 yam.setBibPageUrl(new UrlResource(BIB_URL));
117 yam.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
118 YamParseTree tree = yam.generate();
119
120 // check that the tree is non-empty
121 SimpleNode n = tree.getRootNode();
122 if(new YamParseTreeTests("testFSTranslate").treeEmpty(n))
123 fail("testFSTranslate: parse tree empty");
124
125 // check that the output file is correct
126 String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
127 String response =
128 YamTestUtils.getResourceAsString(resDir + responsePath, "UTF-8");
129 boolean correctResponse = (response.compareTo(key) == 0);
130 if(!correctResponse) {
131 String errorPath = "." + responsePath + "-error";
132 FileWriter fwriter = new FileWriter(errorPath);
133 fwriter.write(key);
134 fwriter.close();
135 fail(
136 "response not equal to key (1) for test file " + responsePath +
137 Strings.getNl() + " error response in " + errorPath
138 );
139 }
140
141 // check that getting via the (YAM) path and its (HTML) dependency both
142 // return the same path
143 FileSystemResource yamLocation2 =
144 new FileSystemResource(factory.getResource(resDir + keyPath).getFile());
145 YamFile yam2 = YamFile.get(yamLocation2);
146 assertEquals(
147 "yam != yam2",
148 yam.getLocation().getDescription(), yam2.getLocation().getDescription()
149 );
150
151 log.info("=============================================================");
152 } // testFSTranslate()
153
154 /**
155 * Test a translation from yam to html out of context. Takes yam-again.yam,
156 * and writes it to a new disk file, yam-test*.yam. YAM statements are now
157 * out of their original context. Translates yam-test*.yam to yam-test*.html
158 * and compares this to yam-minimal.html.
159 */
160 public void testContextPath() throws Exception
161 {
162 // report
163 log.info("============= YamTest.testContextPath() =====================");
164 log.info("testing out of context translate");
165
166 // path, suffixes
167 String path = "/yam-again.yam";
168 String keyPath = "/yam-minimal.html";
169
170 // get the spring context and get an FSR from the path
171 ClassPathXmlApplicationContext factory = new
172 ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
173 FileSystemResource yamLocation =
174 new FileSystemResource(factory.getResource(resDir + path).getFile());
175
176 // create a temp file with a copy of yam-again
177 log.info(yamLocation.getPath());
178 String yamText = Files.getString(yamLocation.getPath());
179 File tempFile = File.createTempFile("yam-test", ".yam");
180 FileWriter fwriter = new FileWriter(tempFile);
181 fwriter.write(yamText);
182 fwriter.close();
183
184 // do translation with the context path set
185 YamFile translator = YamFile.get(new FileSystemResource(tempFile));
186 log.info("setContextPath(yamLocation.getPath()) =" + yamLocation.getPath());
187 translator.setContextPath(yamLocation.getFile().getParent() + "/");
188 translator.setBibPageUrl(new UrlResource(BIB_URL));
189 translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
190 YamParseTree tree = translator.generate();
191
192 // measure difference with yam-minimal.html
193 String newResponsePath = tempFile.getPath().replaceAll("\\.yam", ".html");
194 String newResponse = Files.getString(newResponsePath);
195 String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
196 boolean correctResponse = (newResponse.compareTo(key) == 0);
197 if(!correctResponse) {
198 String errorPath =
199 "./" + tempFile.getName().replaceAll("\\.yam", ".html") + "-error";
200 fwriter = new FileWriter(errorPath);
201 fwriter.write(newResponse);
202 fwriter.close();
203 fail(
204 "newResponse not equal to key (2) for test file " + newResponsePath +
205 Strings.getNl() + " error response in " + errorPath
206 );
207 }
208
209 log.info("=============================================================");
210 } // testContextPath()
211
212 /**
213 * Test the file types enum.
214 */
215 public void testFileTypes() throws Exception {
216 log.info("============= YamTest.testFileTypes() =====================");
217 for(YamFile.FileType type : YamFile.FileType.values())
218 log.debug("type=" + type + "; suffix=" + type.suffix());
219
220 assertEquals(
221 "suffix should be .html", ".html", YamFile.FileType.HTML.suffix()
222 );
223 if(! (YamFile.FileType.LATEX.translator() instanceof LaTeXTranslator))
224 fail("wrong translator type");
225 if(! YamFile.FileType.dependent("something.html"))
226 fail(".html not dependent");
227 if(YamFile.FileType.dependent("something"))
228 fail("something dependent");
229
230 File tempFile = File.createTempFile("yam-test-file-types-", ".yam");
231 YamFile yam = YamFile.get(tempFile);
232 File htmlFile = yam.getOutputFile(YamFile.FileType.HTML);
233
234 String pathShouldBe = yam.getLocation().getFile().getPath();
235 pathShouldBe =
236 pathShouldBe.substring(0, pathShouldBe.lastIndexOf('.')) + ".html";
237
238 assertEquals(
239 "oops, paths not equal", pathShouldBe, htmlFile.getPath()
240 );
241 log.debug("htmlFile path = " + htmlFile.getPath());
242 } // testFileTypes()
243
244 /**
245 * <p>Test HtmlToYamConverter, using an html file generated with no includes
246 * processing. Takes the file yam-minimal-no-includes.html, and converts to
247 * yam im memory. Compares this in-memory yam with a disk copy of
248 * yam-minimal.yam.
249 * </p>
250 * <p>
251 * If there is a difference between the original and resultant yam, then
252 * the resultant yam will be output to <tt>yam-again.yam-conversion-fault<tt>
253 * Results can be viewed by e.g:
254 * </p>
255 * <p><tt>
256 * tkdiff yam-minimal-no-includes.yam-conversion-fault \
257 * test/resources/yam/yam-minimal.yam &
258 * </tt></p>
259 */
260 public void testHtmlToYamNoIncludes() throws Exception
261 {
262
263 log.info("==============================================================");
264 log.info("============= YamTest.testHtmlToYamNoIncludes() ==============");
265 log.info("testing html to yam, from html with no includes processing");
266
267 String keyPath = "/yam-minimal.yam";
268 String sourceHtmlPath = "/yam-minimal-no-includes.html";
269 String errorPath = "./yam-minimal-no-includes.yam-conversion-fault";
270
271 // get the spring context and get an FSR from the source path
272 ClassPathXmlApplicationContext factory = new
273 ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
274 File htmlFile = factory.getResource(resDir + sourceHtmlPath).getFile();
275 log.debug("htmlFile.getPath() = " + htmlFile.getPath());
276
277 // do the conversion
278 StringReader sourceHtmlReader = new StringReader(Files.getString(htmlFile));
279 String response = HtmlToYamConverter.readerToString(sourceHtmlReader);
280
281 // check that the conversion result is correct
282 String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
283 boolean correctResponse = (response.compareTo(key) == 0);
284 if(!correctResponse) {
285 FileWriter fwriter = new FileWriter(errorPath);
286 fwriter.write(response);
287 fwriter.close();
288 //fail(
289 //TODO
290 log.warn(
291 "conversion response not equal (3) to key for test file " + keyPath +
292 Strings.getNl() + " error response in " + errorPath
293 );
294 }
295
296 } // end testHtmlToYamIndependently()
297
298 /**
299 * <p>
300 * Test a round trip translation from yam to html and back to yam. Takes the
301 * file yam-again.yam and copies to yam-test.*.yam. Translates this to
302 * yam-test*.html on disk. Converts yam-test*.html to yam in memory. Compares
303 * this back to yam-again.yam.
304 * </p>
305 * <p>
306 * There is some repetition of to other methods, and this method could instead
307 * make use of files output form those other methods (e.g. take a html file
308 * already generated from yam in a previous test). However, keeping this test
309 * separate keeps the intention clear and isolates it from changes to other
310 * tests.
311 * </p>
312 * <p>
313 * If there is a difference between the original and resultant yam, then
314 * the resultant yam will be output and can be viewed with e.g. tkdiff.
315 * The intermediate html may also be viewed.
316 * </p>
317 *
318 * @throws Exception
319 */
320 public void testYamToHtmlToYam() throws Exception
321 {
322
323 log.info("===============================================================");
324 log.info("============= YamTest.testYamToHtmlToYam() ====================");
325 log.info("Testing yam to html to yam round trip");
326
327 String originalYamPath = "/yam-again.yam";
328
329 // get the spring context and get an FSR from the path
330 ClassPathXmlApplicationContext factory = new
331 ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
332 FileSystemResource originalYamLocation = new FileSystemResource(
333 factory.getResource(resDir + originalYamPath).getFile()
334 );
335
336 // create a temp yam file with a copy of yam-again
337 log.info(originalYamLocation.getPath());
338 String originalYamText = Files.getString(originalYamLocation.getPath());
339 File tempYamFile = File.createTempFile("yam-test", ".yam");
340 FileWriter fwriter = new FileWriter(tempYamFile);
341 fwriter.write(originalYamText);
342 fwriter.close();
343
344 // do translation from the temp yam file to html with doIncludes false
345 YamFile translator = YamFile.get(new FileSystemResource(tempYamFile));
346 translator.setContextPath(originalYamLocation.getFile().getParent() + "/");
347 translator.setBibPageUrl(new UrlResource(BIB_URL));
348 translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
349 translator.setDoIncludes(false);
350 YamParseTree tree = translator.generate();
351
352 // Get the html and convert it back to yam
353 String tempHtmlPath = tempYamFile.getPath().replaceAll("\\.yam", ".html");
354 StringReader sourceHtmlReader
355 = new StringReader(Files.getString(tempHtmlPath));
356 String responseYam = HtmlToYamConverter.readerToString(sourceHtmlReader);
357
358 // check that the conversion result is correct
359 String keyYam =
360 YamTestUtils.getResourceAsString(resDir + originalYamPath, "UTF-8");
361 boolean correctResponse = (responseYam.compareTo(keyYam) == 0);
362 if(!correctResponse) {
363 String errorPath =
364 "./" + tempYamFile.getName() + "-error";
365 fwriter = new FileWriter(errorPath);
366 fwriter.write(responseYam);
367 fwriter.close();
368 //fail(
369 //TODO
370 log.warn(
371 "conversion response not equal (4) to key for test file " + originalYamPath
372 + Strings.getNl() + " error response in " + errorPath
373 + Strings.getNl() + " intermediate html in " + tempHtmlPath
374 );
375 }
376
377 } //testYamToHtmlToYam()
378
379 /**
380 * Test a translation of yam to html without processing includes. Takes
381 * yam-again.yam and copies to the disk file yam-test*.yam. Translates this
382 * to yam-test*.html without processing includes, and compares this to
383 * yam-minimal-no-includes.html.
384 */
385 public void testNoIncludes() throws Exception
386 {
387 // report
388 log.info("============================================================");
389 log.info("============= YamTest.testNoIncludes() =====================");
390 log.info("Testing yam to html with no includes processing");
391
392 // path, suffixes
393 String path = "/yam-again.yam";
394 String keyPath = "/yam-minimal-no-includes.html";
395
396 // get the spring context and get an FSR from the path
397 ClassPathXmlApplicationContext factory = new
398 ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
399 FileSystemResource yamLocation =
400 new FileSystemResource(factory.getResource(resDir + path).getFile());
401
402 // create a temp file with a copy of yam-again
403 log.info(yamLocation.getPath());
404 String yamText = Files.getString(yamLocation.getPath());
405 File tempFile = File.createTempFile("yam-test", ".yam");
406 FileWriter fwriter = new FileWriter(tempFile);
407 fwriter.write(yamText);
408 fwriter.close();
409
410 // do translation with doIncludes false
411 YamFile translator = YamFile.get(new FileSystemResource(tempFile));
412 translator.setContextPath(yamLocation.getFile().getParent() + "/");
413 translator.setBibPageUrl(new UrlResource(BIB_URL));
414 translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
415 translator.setDoIncludes(false);
416 YamParseTree tree = translator.generate();
417
418 String newResponsePath = tempFile.getPath().replaceAll("\\.yam", ".html");
419 String newResponse = Files.getString(newResponsePath);
420 String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
421 boolean correctResponse = (newResponse.compareTo(key) == 0);
422 if(!correctResponse) {
423 String errorPath =
424 "./" + tempFile.getName().replaceAll("\\.yam", ".html") + "-error";
425 fwriter = new FileWriter(errorPath);
426 fwriter.write(newResponse);
427 fwriter.close();
428 fail(
429 "newResponse not equal to key (5) for test file " + newResponsePath +
430 Strings.getNl() + " error response in " + errorPath
431 );
432 }
433
434 log.info("=============================================================");
435 } // testNoIncludes()
436
437 } // YamTest
|