Package net.sourceforge.chaperon.test

Source Code of net.sourceforge.chaperon.test.GeneralParserProcessorTestCase

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import net.sourceforge.chaperon.build.Automaton;
import net.sourceforge.chaperon.common.ConsoleLog;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.GrammarFactory;
import net.sourceforge.chaperon.process.GeneralParserProcessor;

import org.custommonkey.xmlunit.Diff;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

public class GeneralParserProcessorTestCase extends TestCase
{
  public GeneralParserProcessorTestCase(String name)
  {
    super(name);
  }

  public void testTest01() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test1.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test1.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test1.out");

    assertEqual(expected, result);
  }

/*
  public void testTest02() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test2.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test2.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test2.out");

    assertEqual(expected, result);
  }*/
  public void testTest03() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test3.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test3.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test3.out");

    assertEqual(expected, result);
  }

  public void testTest04() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test4.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test4.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test4.out");

    assertEqual(expected, result);
  }

  public void testTest04_2() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test15.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test15.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test15.out");

    assertEqual(expected, result);
  }

/*  public void testTest05() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test5.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test5.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test5.out");

    assertEqual(expected, result);
  }*/
/*  public void testTest06() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test6.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test6.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test6.out");

    assertEqual(expected, result);
  }*/
/*  public void testTest07() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test7.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test7.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test7.out");

    assertEqual(expected, result);
  }*/
/*  public void testTest08() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test8.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test8.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test8.out");

    assertEqual(expected, result);
  }*/
/*  public void testTest09() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test9.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test9.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test9.out");

    assertEqual(expected, result);
  }*/
/*  public void testTest10() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test10.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test10.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test10.out");

    assertEqual(expected, result);
  }

  public void testTest11() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test11.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test11.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test11.out");

    assertEqual(expected, result);
  }

  public void testTest12() throws Exception
  {
    System.out.println("=========================================");
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test12.xgrm");
    System.out.println("grammar:\n"+grammar);
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test12.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test12.out");

    assertEqual(expected, result);
  }*/
/* Endless loop*/
  public void testTest13() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test13.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test13.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test13.out");

    assertEqual(expected, result);
  }

  public void testTest14() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test14.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test14.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test14.out");

    assertEqual(expected, result);
  }

  public void testTest16() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test16.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test16.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test16.out");

    assertEqual(expected, result);
  }

  public void testTest17() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test17.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test17.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test17.out");

    assertEqual(expected, result);
  }

  public void testTest18() throws Exception
  {
    Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test18.xgrm");
    GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
    Document result = process(processor, "net/sourceforge/chaperon/test/parser/test18.in");
    Document expected = getDocument("net/sourceforge/chaperon/test/parser/test18.out");

    assertEqual(expected, result);
  }

  private GeneralParserProcessor getGeneralParserProcessor(Grammar grammar)
  {
    GeneralParserProcessor processor = new GeneralParserProcessor();
    processor.setLog(new ConsoleLog());

    /*Automaton automaton =
      (new AutomatonBuilder(grammar, new ConsoleLog())).getParserAutomaton();*/

    //  (new AutomatonBuilder(grammar)).getParserAutomaton();
    Automaton automaton = new Automaton(grammar, new ConsoleLog());

    assertNotNull("Test if automaton is generated", automaton);

    processor.setParserAutomaton(automaton);

    return processor;
  }

  private Document process(GeneralParserProcessor processor, String in)
    throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    SAXTransformerFactory transformerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler handler = transformerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    handler.setResult(result);

    processor.setContentHandler(new WhitespaceFilter(handler));

    parser.setContentHandler(processor);
    parser.parse(new InputSource(getClass().getClassLoader().getResourceAsStream(in)));

    return (Document)result.getNode();
  }

  private Grammar getGrammar(String in) throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    GrammarFactory grammarfactory = new GrammarFactory();
    parser.setContentHandler(grammarfactory);
    parser.parse(new InputSource(getClass().getClassLoader().getResourceAsStream(in)));

    //System.out.println("====================================\nGrammar:\n"+grammarfactory.getGrammar()+"\n");
    return grammarfactory.getGrammar();
  }

  private Document getDocument(String in) throws Exception
  {
    SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    SAXTransformerFactory transformerfactory =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    TransformerHandler handler = transformerfactory.newTransformerHandler();
    DOMResult result = new DOMResult();
    handler.setResult(result);

    parser.setContentHandler(new WhitespaceFilter(handler));
    parser.parse(new InputSource(getClass().getClassLoader().getResourceAsStream(in)));

    return (Document)result.getNode();
  }

  public final void print(Document document) throws Exception
  {
    TransformerFactory factory = TransformerFactory.newInstance();
    javax.xml.transform.Transformer serializer = factory.newTransformer();
    serializer.transform(new DOMSource(document), new StreamResult(System.out));
    System.out.println();
  }

  /**
   * Compare two XML documents provided as strings
   *
   * @param control Control document
   * @param test Document to test
   *
   * @return Diff object describing differences in documents
   */
  public final Diff compareXML(Document control, Document test)
  {
    return new Diff(control, test);
  }

  /**
   * Assert that the result of an XML comparison is similar.
   *
   * @param msg The assertion message
   * @param expected The expected XML document
   * @param actual The actual XML Document
   */
  public final void assertEqual(String msg, Document expected, Document actual)
  {
    expected.getDocumentElement().normalize();
    actual.getDocumentElement().normalize();

    Diff diff = compareXML(expected, actual);

    assertEquals(msg+", "+diff.toString(), true, diff.similar());
  }

  /**
   * Assert that the result of an XML comparison is similar.
   *
   * @param msg The assertion message
   * @param expected The expected XML document
   * @param actual The actual XML Document
   */
  public final void assertEqual(Document expected, Document actual)
  {
    expected.getDocumentElement().normalize();
    actual.getDocumentElement().normalize();

    Diff diff = compareXML(expected, actual);

    try
    {
      System.out.println("expected:");
      print(expected);
      System.out.println("actual:");
      print(actual);
    }
    catch (Exception e) {}

    assertEquals("Test if the assertion document is equal, "+diff.toString(), true, diff.similar());
  }

  public static Test suite()
  {
    return new TestSuite(GeneralParserProcessorTestCase.class);
  }
}
TOP

Related Classes of net.sourceforge.chaperon.test.GeneralParserProcessorTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.