/*
* 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.*;
import net.sourceforge.chaperon.build.LexicalAutomatonBuilder;
import net.sourceforge.chaperon.common.ConsoleLog;
import net.sourceforge.chaperon.model.lexicon.Lexicon;
import net.sourceforge.chaperon.model.lexicon.LexiconFactory;
import net.sourceforge.chaperon.process.*;
import org.custommonkey.xmlunit.Diff;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
public class LexicalProcessorTestCase extends TestCase
{
private String name;
private Element step;
public LexicalProcessorTestCase(String name)
{
super(name);
this.name = name;
}
public LexicalProcessorTestCase(String name, Element step)
{
super(name);
this.name = name;
this.step = step;
}
public void runTest() throws Throwable
{
System.out.println("======================= "+name+" =======================");
Lexicon lexicon = getLexicon(step);
LexicalProcessor processor = getLexicalProcessor(lexicon);
Document result = process(processor, step);
Document expected = getResult(step);
assertEqual(expected, result);
}
private LexicalProcessor getLexicalProcessor(Lexicon lexicon)
{
LexicalProcessor processor = new LexicalProcessor();
processor.setLog(new ConsoleLog());
LexicalAutomaton automaton =
// (new LexicalAutomatonBuilder(getLexicon(), new ConsoleLog())).getLexicalAutomaton();
(new LexicalAutomatonBuilder(lexicon)).getLexicalAutomaton();
assertNotNull("Test if automaton is generated", automaton);
processor.setLexicalAutomaton(automaton);
NodeList list = step.getElementsByTagName("parameter");
for (int i = 0; i<list.getLength(); i++)
{
Element element = (Element)list.item(i);
if (element.getAttribute("name").equals("grouping"))
processor.setGrouping(Boolean.valueOf(element.getAttribute("value")).booleanValue());
}
return processor;
}
private Document process(LexicalProcessor processor, Element step)
throws Exception
{
NodeList list = step.getElementsByTagNameNS(LexicalProcessor.NS, LexicalProcessor.TEXT);
Node node = list.item(0);
TransformerFactory streamerfactory = TransformerFactory.newInstance();
Transformer streamer = streamerfactory.newTransformer();
SAXTransformerFactory serializerfactory =
(SAXTransformerFactory)SAXTransformerFactory.newInstance();
TransformerHandler serializer = serializerfactory.newTransformerHandler();
DOMResult result = new DOMResult();
serializer.setResult(result);
processor.setContentHandler(new WhitespaceFilter(serializer));
streamer.transform(new DOMSource(node), new SAXResult(processor));
return (Document)result.getNode();
}
private Lexicon getLexicon(Element step) throws Exception
{
NodeList list =
step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/lexicon/1.0", "lexicon");
Node node = list.item(0);
TransformerFactory streamerfactory = TransformerFactory.newInstance();
Transformer streamer = streamerfactory.newTransformer();
LexiconFactory lexiconfactory = new LexiconFactory();
streamer.transform(new DOMSource(node), new SAXResult(lexiconfactory));
return lexiconfactory.getLexicon();
}
private Document getResult(Element step) throws Exception
{
NodeList list =
step.getElementsByTagNameNS(LexicalProcessor.NS_OUTPUT, LexicalProcessor.OUTPUT);
Node node = list.item(0);
TransformerFactory streamerfactory = TransformerFactory.newInstance();
Transformer streamer = streamerfactory.newTransformer();
DOMResult result = new DOMResult();
streamer.transform(new DOMSource(node), result);
return (Document)result.getNode();
}
private static Document getDocument(String filename, InputStream in)
throws Exception
{
SAXParserFactory parserfactory = SAXParserFactory.newInstance();
parserfactory.setNamespaceAware(true);
XMLReader parser = parserfactory.newSAXParser().getXMLReader();
SAXTransformerFactory serializerfactory =
(SAXTransformerFactory)SAXTransformerFactory.newInstance();
TransformerHandler handler = serializerfactory.newTransformerHandler();
DOMResult result = new DOMResult();
handler.setResult(result);
parser.setContentHandler(new WhitespaceFilter(handler));
InputSource inputsource = new InputSource(in);
inputsource.setSystemId(filename);
parser.parse(inputsource);
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);
if (!diff.similar())
{
try
{
System.out.println("expected:");
print(expected);
System.out.println("actual:");
print(actual);
}
catch (Exception e)
{
e.printStackTrace();
}
}
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);
if (!diff.similar())
{
try
{
System.out.println("expected:");
print(expected);
System.out.println("actual:");
print(actual);
}
catch (Exception e)
{
e.printStackTrace();
}
}
assertEquals("Test if the assertion document is equal, "+diff.toString(), true, diff.similar());
}
public static Test suite()
{
TestSuite suite = new TestSuite("LexicalProcessorTest");
try
{
File directory = new File(LexicalProcessorTestCase.class.getResource("lexer").getFile());
File[] files = directory.listFiles();
for (int i = 0; i<files.length; i++)
if ((files[i].getName().endsWith(".xml")) /* && (files[i].getName().startsWith("recursion"))*/)
{
Document test = getDocument(files[i].toString(), new FileInputStream(files[i]));
NodeList list = test.getDocumentElement().getElementsByTagName("step");
for (int j = 0; j<list.getLength(); j++)
{
Element step = (Element)list.item(j);
suite.addTest(new LexicalProcessorTestCase("LexicalProcessorTest "+files[i].getName()+
":"+step.getAttribute("name"), step));
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return suite;
}
}