Log in Help
Print
Homegatewikicowtestunitgateyam 〉 YamTests.java
 
/*
 *  YamTest.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 February 2007
 */

package gate.yam;

import java.io.*;
import junit.framework.*;
import org.apache.log4j.Logger;
import org.springframework.core.io.*;
import org.springframework.context.support.*;
import gate.util.*;
import gate.yam.*;
import gate.yam.parse.*;
import gate.yam.translate.*;
import gate.yam.convert.*;


/**
 * Unit test for the YamFile class.
 */
public class YamTests extends TestCase
{

  /** Create the test case */
  public YamTests(String testName) {
    super(testName);
  }

  /** Logger */
  static Logger log = Logger.getLogger("gate.yam.YamTest");

  /** The URL of the bibliography file used for citations in tests */
  private static String BIB_URL = "http://gate.ac.uk/sale/bib/main.html";

  /** The anchor prefix used for citations in tests */
  private static String BIB_ANCHOR_PREFIX = "X";

  /** Resource directory name. */
  static final String resDir = AbstractTranslatorTest.resDir;

  /**
   * Test file-system translate method. Takes yam-again.yam and generates
   * an html file on disk from it, yam-again.html. Compares this with
   * yam-minimal.html. The comparison assumes that yam-again.yam is a copy of
   * yam-minimal.yam
   *
   */
  public void testFSTranslate() throws Exception
  {
    // report
    log.info("=============================================================");
    log.info("============= YamTest.testFSTranslate() =====================");
    log.info("testing file-system-based translate");
    log.debug("**NOTE**: yam-again.yam should be copy of yam-minimal.yam");

    // path, suffixes
    String path = "/yam-again.yam";
    String keyPath = "/yam-again.html";
    String responsePath = "/yam-minimal.html";

    // get the spring context and get an FSR from the path
    ClassPathXmlApplicationContext factory = new
      ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
    FileSystemResource yamLocation =
      new FileSystemResource(factory.getResource(resDir + path).getFile());

    // the yam factory method: finding a .yam
    log.info("yamLocation = " + yamLocation.getDescription());
    YamFile yam = YamFile.get(yamLocation);
    if(yam == null) fail("yam == null");
    log.info("yam.getLocation().getPath() = " + yam.getLocation().getPath());
    assertEquals(
      "path != yam.getLocation.getPath()", yamLocation.getPath(),
      yam.getLocation().getPath()
    );

    // the yam factory method: finding a .yam via a .html
    FileSystemResource htmlLocation = new
      FileSystemResource(factory.getResource(resDir + responsePath).getFile());
    YamFile yamFromHtml = YamFile.get(htmlLocation);
    log.info("yamFromHtml = " + yamFromHtml.getLocation().getDescription());
    htmlLocation = new FileSystemResource("/nonexistant.html");
    yamFromHtml = YamFile.get(htmlLocation);
    assertNull("yamFromHtml should be null", yamFromHtml);

    // check that the dependent html needs regeneration
    StringBuilder yamAgainYamPath =
      new StringBuilder(yamLocation.getFile().getPath());
    int len = yamAgainYamPath.length();
    String yamAgainHtmlPath =
      yamAgainYamPath.replace(
        len - 4, len, YamFile.FileType.HTML.suffix()
      ).toString();
    (new File(yamAgainHtmlPath)).delete();
    FileSystemResource yamAgainHtmlLocation =
      new FileSystemResource(yamAgainHtmlPath);
    log.debug("yamAgainHtmlLocation= "+yamAgainHtmlLocation.getDescription());
    YamFile yamForRegen = YamFile.needsGeneration(yamAgainHtmlLocation);
    assertNotNull("yamForRegen is null", yamForRegen);

    // check that the non-existent dependent html doesn't need generation
    log.debug("htmlLocation= "+htmlLocation.getDescription());
    yamForRegen = YamFile.needsGeneration(htmlLocation);
    assertNull("yamForRegen is not null", yamForRegen);

    // config, run the translator and get the response
    yam.setBibPageUrl(new UrlResource(BIB_URL));
    yam.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
    YamParseTree tree = yam.generate();

    // check that the tree is non-empty
    SimpleNode n = tree.getRootNode();
    if(new YamParseTreeTests("testFSTranslate").treeEmpty(n))
      fail("testFSTranslate: parse tree empty");

    // check that the output file is correct
    String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
    String response =
      YamTestUtils.getResourceAsString(resDir + responsePath, "UTF-8");
    boolean correctResponse = (response.compareTo(key) == 0);
    if(!correctResponse) {
      String errorPath = "." + responsePath + "-error";
      FileWriter fwriter = new FileWriter(errorPath);
      fwriter.write(key);
      fwriter.close();
      fail(
        "response not equal to key (1) for test file " + responsePath + 
        Strings.getNl() + "  error response in " + errorPath
      );
    }

    // check that getting via the (YAM) path and its (HTML) dependency both
    // return the same path
    FileSystemResource yamLocation2 =
      new FileSystemResource(factory.getResource(resDir + keyPath).getFile());
    YamFile yam2 = YamFile.get(yamLocation2);
    assertEquals(
      "yam != yam2",
      yam.getLocation().getDescription(), yam2.getLocation().getDescription()
    );

    log.info("=============================================================");
  } // testFSTranslate()

  /**
   * Test a translation from yam to html out of context. Takes yam-again.yam,
   * and writes it to a new disk file, yam-test*.yam. YAM statements are now
   * out of their original context. Translates yam-test*.yam to yam-test*.html
   * and compares this to yam-minimal.html.
   */
  public void testContextPath() throws Exception
  {
    // report
    log.info("============= YamTest.testContextPath() =====================");
    log.info("testing out of context translate");

    // path, suffixes
    String path = "/yam-again.yam";
    String keyPath = "/yam-minimal.html";

    // get the spring context and get an FSR from the path
    ClassPathXmlApplicationContext factory = new
      ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
    FileSystemResource yamLocation =
      new FileSystemResource(factory.getResource(resDir + path).getFile());

    // create a temp file with a copy of yam-again
    log.info(yamLocation.getPath());
    String yamText = Files.getString(yamLocation.getPath());
    File tempFile = File.createTempFile("yam-test", ".yam");
    FileWriter fwriter = new FileWriter(tempFile);
    fwriter.write(yamText);
    fwriter.close();

    // do translation with the context path set
    YamFile translator = YamFile.get(new FileSystemResource(tempFile));
    log.info("setContextPath(yamLocation.getPath()) =" + yamLocation.getPath());
    translator.setContextPath(yamLocation.getFile().getParent() + "/");
    translator.setBibPageUrl(new UrlResource(BIB_URL));
    translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
    YamParseTree tree = translator.generate();

    // measure difference with yam-minimal.html
    String newResponsePath = tempFile.getPath().replaceAll("\\.yam", ".html");
    String newResponse = Files.getString(newResponsePath);
    String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
    boolean correctResponse = (newResponse.compareTo(key) == 0);
    if(!correctResponse) {
      String errorPath =
        "./" + tempFile.getName().replaceAll("\\.yam", ".html") + "-error";
      fwriter = new FileWriter(errorPath);
      fwriter.write(newResponse);
      fwriter.close();
      fail(
        "newResponse not equal to key (2) for test file " + newResponsePath + 
        Strings.getNl() + "  error response in " + errorPath
      );
    }

    log.info("=============================================================");
  } // testContextPath()

  /**
   * Test the file types enum.
   */
  public void testFileTypes() throws Exception {
    log.info("============= YamTest.testFileTypes() =====================");
    for(YamFile.FileType type : YamFile.FileType.values())
      log.debug("type=" + type + "; suffix=" + type.suffix());

    assertEquals(
      "suffix should be .html", ".html", YamFile.FileType.HTML.suffix()
    );
    if(! (YamFile.FileType.LATEX.translator() instanceof LaTeXTranslator))
      fail("wrong translator type");
    if(! YamFile.FileType.dependent("something.html"))
      fail(".html not dependent");
    if(YamFile.FileType.dependent("something"))
      fail("something dependent");

    File tempFile = File.createTempFile("yam-test-file-types-", ".yam");
    YamFile yam = YamFile.get(tempFile);
    File htmlFile = yam.getOutputFile(YamFile.FileType.HTML);

    String pathShouldBe = yam.getLocation().getFile().getPath();
    pathShouldBe =
      pathShouldBe.substring(0, pathShouldBe.lastIndexOf('.')) + ".html";

    assertEquals(
      "oops, paths not equal", pathShouldBe, htmlFile.getPath()
    );
    log.debug("htmlFile path = " + htmlFile.getPath());
  } // testFileTypes()

  /**
   * <p>Test HtmlToYamConverter, using an html file generated with no includes
   * processing. Takes the file yam-minimal-no-includes.html, and converts to
   * yam im memory. Compares this in-memory yam with a disk copy of
   * yam-minimal.yam.
   * </p>
   *  <p>
   * If there is a difference between the original and resultant yam, then
   * the resultant yam will be output to <tt>yam-again.yam-conversion-fault<tt>
   * Results can be viewed by e.g:
   * </p>
   * <p><tt>
   * tkdiff yam-minimal-no-includes.yam-conversion-fault \
   * test/resources/yam/yam-minimal.yam &
   * </tt></p>
   */
  public void testHtmlToYamNoIncludes() throws Exception
  {

    log.info("==============================================================");
    log.info("============= YamTest.testHtmlToYamNoIncludes() ==============");
    log.info("testing html to yam, from html with no includes processing");

    String keyPath = "/yam-minimal.yam";
    String sourceHtmlPath = "/yam-minimal-no-includes.html";
    String errorPath = "./yam-minimal-no-includes.yam-conversion-fault";

    // get the spring context and get an FSR from the source path
    ClassPathXmlApplicationContext factory = new
      ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
    File htmlFile = factory.getResource(resDir + sourceHtmlPath).getFile();
    log.debug("htmlFile.getPath() = " + htmlFile.getPath());

    // do the conversion
    StringReader sourceHtmlReader = new StringReader(Files.getString(htmlFile));
    String response = HtmlToYamConverter.readerToString(sourceHtmlReader);

    // check that the conversion result is correct
    String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
    boolean correctResponse = (response.compareTo(key) == 0);
    if(!correctResponse) {
      FileWriter fwriter = new FileWriter(errorPath);
      fwriter.write(response);
      fwriter.close();
      //fail(
//TODO
log.warn(
        "conversion response not equal (3) to key for test file " + keyPath +
        Strings.getNl() + "  error response in " + errorPath
      );
    }

  } // end testHtmlToYamIndependently()

  /**
   * <p>
   * Test a round trip translation from yam to html and back to yam. Takes the
   * file yam-again.yam and copies to yam-test.*.yam. Translates this to
   * yam-test*.html on disk. Converts yam-test*.html to yam in memory. Compares
   * this back to yam-again.yam.
   * </p>
   * <p>
   * There is some repetition of to other methods, and this method could instead
   * make use of files output form those other methods (e.g. take a html file
   * already generated from yam in a previous test). However, keeping this test
   * separate keeps the intention clear and isolates it from changes to other
   * tests.
   * </p>
   * <p>
   * If there is a difference between the original and resultant yam, then
   * the resultant yam will be output and can be viewed with e.g. tkdiff.
   * The intermediate html may also be viewed.
   * </p>
   *
   * @throws Exception
   */
  public void testYamToHtmlToYam() throws Exception
  {

    log.info("===============================================================");
    log.info("============= YamTest.testYamToHtmlToYam() ====================");
    log.info("Testing yam to html to yam round trip");

    String originalYamPath = "/yam-again.yam";

    // get the spring context and get an FSR from the path
    ClassPathXmlApplicationContext factory = new
      ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
    FileSystemResource originalYamLocation = new FileSystemResource(
      factory.getResource(resDir + originalYamPath).getFile()
    );

    // create a temp yam file with a copy of yam-again
    log.info(originalYamLocation.getPath());
    String originalYamText = Files.getString(originalYamLocation.getPath());
    File tempYamFile = File.createTempFile("yam-test", ".yam");
    FileWriter fwriter = new FileWriter(tempYamFile);
    fwriter.write(originalYamText);
    fwriter.close();

    // do translation from the temp yam file to html with doIncludes false
    YamFile translator = YamFile.get(new FileSystemResource(tempYamFile));
    translator.setContextPath(originalYamLocation.getFile().getParent() + "/");
    translator.setBibPageUrl(new UrlResource(BIB_URL));
    translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);
    translator.setDoIncludes(false);
    YamParseTree tree = translator.generate();

    // Get the html and convert it back to yam
    String tempHtmlPath = tempYamFile.getPath().replaceAll("\\.yam", ".html");
    StringReader sourceHtmlReader
            = new StringReader(Files.getString(tempHtmlPath));
    String responseYam = HtmlToYamConverter.readerToString(sourceHtmlReader);

    // check that the conversion result is correct
    String keyYam =
      YamTestUtils.getResourceAsString(resDir + originalYamPath, "UTF-8");
    boolean correctResponse = (responseYam.compareTo(keyYam) == 0);
    if(!correctResponse) {
      String errorPath =
        "./" + tempYamFile.getName() + "-error";
      fwriter = new FileWriter(errorPath);
      fwriter.write(responseYam);
      fwriter.close();
      //fail(
//TODO
log.warn(
        "conversion response not equal (4) to key for test file " + originalYamPath
        + Strings.getNl() + "  error response in " + errorPath
        + Strings.getNl() + "  intermediate html in " + tempHtmlPath
      );
    }

  } //testYamToHtmlToYam()

  /**
   * Test a translation of yam to html without processing includes. Takes
   * yam-again.yam and copies to the disk file yam-test*.yam. Translates this
   * to yam-test*.html without processing includes, and compares this to
   * yam-minimal-no-includes.html.
   */
  public void testNoIncludes() throws Exception
  {
    // report
    log.info("============================================================");
    log.info("============= YamTest.testNoIncludes() =====================");
    log.info("Testing yam to html with no includes processing");

    // path, suffixes
    String path = "/yam-again.yam";
    String keyPath = "/yam-minimal-no-includes.html";

    // get the spring context and get an FSR from the path
    ClassPathXmlApplicationContext factory = new
      ClassPathXmlApplicationContext(resDir + "/spring-app-context.xml");
    FileSystemResource yamLocation =
      new FileSystemResource(factory.getResource(resDir + path).getFile());

    // create a temp file with a copy of yam-again
    log.info(yamLocation.getPath());
    String yamText = Files.getString(yamLocation.getPath());
    File tempFile = File.createTempFile("yam-test", ".yam");
    FileWriter fwriter = new FileWriter(tempFile);
    fwriter.write(yamText);
    fwriter.close();

    // do translation with doIncludes false
    YamFile translator = YamFile.get(new FileSystemResource(tempFile));
    translator.setContextPath(yamLocation.getFile().getParent() + "/");
    translator.setBibPageUrl(new UrlResource(BIB_URL));
    translator.setBibAnchorPrefix(BIB_ANCHOR_PREFIX);    
    translator.setDoIncludes(false);
    YamParseTree tree = translator.generate();

    String newResponsePath = tempFile.getPath().replaceAll("\\.yam", ".html");
    String newResponse = Files.getString(newResponsePath);
    String key = YamTestUtils.getResourceAsString(resDir + keyPath, "UTF-8");
    boolean correctResponse = (newResponse.compareTo(key) == 0);
    if(!correctResponse) {
      String errorPath =
        "." + keyPath.replaceAll("\\.yam", ".html") + "-error";
      fwriter = new FileWriter(errorPath);
      fwriter.write(newResponse);
      fwriter.close();
      fail(
        "newResponse not equal to key (5) for test file " + newResponsePath + 
        Strings.getNl() + "  error response in " + errorPath
      );
    }

    log.info("=============================================================");
  } // testNoIncludes()

} // YamTest