001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.*;
012:
013: import net.sourceforge.chaperon.build.ParserAutomatonBuilder;
014: import net.sourceforge.chaperon.common.ConsoleLog;
015: import net.sourceforge.chaperon.model.grammar.Grammar;
016: import net.sourceforge.chaperon.model.grammar.GrammarFactory;
017: import net.sourceforge.chaperon.process.*;
018:
019: import org.custommonkey.xmlunit.Diff;
020:
021: import org.w3c.dom.*;
022:
023: import org.xml.sax.InputSource;
024: import org.xml.sax.XMLReader;
025:
026: import java.io.*;
027:
028: import javax.xml.parsers.*;
029: import javax.xml.transform.*;
030: import javax.xml.transform.dom.*;
031: import javax.xml.transform.sax.*;
032: import javax.xml.transform.stream.*;
033:
034: public class ParserProcessorTestCase extends TestCase {
035: private String name;
036: private Element step;
037:
038: public ParserProcessorTestCase(String name) {
039: super (name);
040: this .name = name;
041: }
042:
043: public ParserProcessorTestCase(String name, Element step) {
044: super (name);
045: this .name = name;
046: this .step = step;
047: }
048:
049: public void runTest() throws Throwable {
050: System.out.println("======================= " + name
051: + " =======================");
052:
053: Grammar grammar = getGrammar(step);
054:
055: //System.out.println("Grammar:\n"+grammar);
056: ParserProcessor processor = getParserProcessor(grammar);
057:
058: Document result = null;
059:
060: result = process(processor, step);
061:
062: Document expected = getResult(step);
063:
064: assertEqual(expected, result);
065: }
066:
067: private ParserProcessor getParserProcessor(Grammar grammar) {
068: ParserProcessor processor = new ParserProcessor();
069: processor.setLog(new ConsoleLog());
070:
071: ParserAutomaton automaton = (new ParserAutomatonBuilder(
072: grammar, new ConsoleLog())).getParserAutomaton();
073:
074: // (new ParserAutomatonBuilder(grammar)).getParserAutomaton();
075: assertNotNull("Test if automaton is generated", automaton);
076:
077: //System.out.println("Automaton:\n"+automaton);
078: processor.setParserAutomaton(automaton);
079:
080: NodeList list = step.getElementsByTagName("parameter");
081: for (int i = 0; i < list.getLength(); i++) {
082: Element element = (Element) list.item(i);
083: if (element.getAttribute("name").equals("flatten"))
084: processor.setFlatten(Boolean.valueOf(
085: element.getAttribute("value")).booleanValue());
086: }
087:
088: return processor;
089: }
090:
091: private Document process(ParserProcessor processor, Element step)
092: throws Exception {
093: NodeList list = step.getElementsByTagNameNS(
094: LexicalProcessor.NS_OUTPUT, LexicalProcessor.OUTPUT);
095: Node node = list.item(0);
096:
097: TransformerFactory streamerfactory = TransformerFactory
098: .newInstance();
099: Transformer streamer = streamerfactory.newTransformer();
100:
101: SAXTransformerFactory serializerfactory = (SAXTransformerFactory) SAXTransformerFactory
102: .newInstance();
103: TransformerHandler serializer = serializerfactory
104: .newTransformerHandler();
105: DOMResult result = new DOMResult();
106: serializer.setResult(result);
107:
108: processor.setContentHandler(new WhitespaceFilter(serializer));
109:
110: streamer.transform(new DOMSource(node),
111: new SAXResult(processor));
112:
113: return (Document) result.getNode();
114: }
115:
116: private Grammar getGrammar(Element step) throws Exception {
117: NodeList list = step.getElementsByTagNameNS(
118: "http://chaperon.sourceforge.net/schema/grammar/1.0",
119: "grammar");
120: Node node = list.item(0);
121:
122: TransformerFactory streamerfactory = TransformerFactory
123: .newInstance();
124: Transformer streamer = streamerfactory.newTransformer();
125:
126: GrammarFactory grammarfactory = new GrammarFactory();
127:
128: streamer.transform(new DOMSource(node), new SAXResult(
129: grammarfactory));
130:
131: Grammar grammar = grammarfactory.getGrammar();
132:
133: System.out.println("Grammar:\n" + grammar);
134:
135: return grammar;
136: }
137:
138: private Document getResult(Element step) throws Exception {
139: NodeList list = step.getElementsByTagNameNS(
140: ParserProcessor.NS_OUTPUT, ParserProcessor.OUTPUT);
141: Node node = list.item(0);
142:
143: TransformerFactory streamerfactory = TransformerFactory
144: .newInstance();
145: Transformer streamer = streamerfactory.newTransformer();
146:
147: //DOMSource source = new DOMSource(node);
148: DOMResult result = new DOMResult();
149:
150: streamer.transform(new DOMSource(node), result);
151:
152: return (Document) result.getNode();
153: }
154:
155: private static Document getDocument(String filename, InputStream in)
156: throws Exception {
157: SAXParserFactory parserfactory = SAXParserFactory.newInstance();
158: parserfactory.setNamespaceAware(true);
159:
160: XMLReader parser = parserfactory.newSAXParser().getXMLReader();
161:
162: SAXTransformerFactory serializerfactory = (SAXTransformerFactory) SAXTransformerFactory
163: .newInstance();
164: TransformerHandler handler = serializerfactory
165: .newTransformerHandler();
166: DOMResult result = new DOMResult();
167: handler.setResult(result);
168:
169: parser.setContentHandler(new WhitespaceFilter(handler));
170:
171: InputSource inputsource = new InputSource(in);
172: inputsource.setSystemId(filename);
173: parser.parse(inputsource);
174:
175: return (Document) result.getNode();
176: }
177:
178: public final void print(Document document) throws Exception {
179: TransformerFactory factory = TransformerFactory.newInstance();
180: javax.xml.transform.Transformer serializer = factory
181: .newTransformer();
182: serializer.transform(new DOMSource(document), new StreamResult(
183: System.out));
184: System.out.println();
185: }
186:
187: /**
188: * Compare two XML documents provided as strings
189: *
190: * @param control Control document
191: * @param test Document to test
192: *
193: * @return Diff object describing differences in documents
194: */
195: public final Diff compareXML(Document control, Document test) {
196: return new Diff(control, test);
197: }
198:
199: /**
200: * Assert that the result of an XML comparison is similar.
201: *
202: * @param msg The assertion message
203: * @param expected The expected XML document
204: * @param actual The actual XML Document
205: */
206: public final void assertEqual(String msg, Document expected,
207: Document actual) {
208: expected.getDocumentElement().normalize();
209: actual.getDocumentElement().normalize();
210:
211: Diff diff = compareXML(expected, actual);
212:
213: if (!diff.similar()) {
214: try {
215: System.out.println("expected:");
216: print(expected);
217: System.out.println("actual:");
218: print(actual);
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: }
223:
224: assertEquals(msg + ", " + diff.toString(), true, diff.similar());
225: }
226:
227: /**
228: * Assert that the result of an XML comparison is similar.
229: *
230: * @param msg The assertion message
231: * @param expected The expected XML document
232: * @param actual The actual XML Document
233: */
234: public final void assertEqual(Document expected, Document actual) {
235: expected.getDocumentElement().normalize();
236: actual.getDocumentElement().normalize();
237:
238: Diff diff = compareXML(expected, actual);
239:
240: if (!diff.similar()) {
241: try {
242: System.out.println("expected:");
243: print(expected);
244: System.out.println("actual:");
245: print(actual);
246: } catch (Exception e) {
247: e.printStackTrace();
248: }
249: }
250:
251: assertEquals("Test if the assertion document is equal, "
252: + diff.toString(), true, diff.similar());
253: }
254:
255: public static Test suite() {
256: TestSuite suite = new TestSuite("ParserProcessorTest");
257:
258: try {
259: File directory = new File(ParserProcessorTestCase.class
260: .getResource("parser").getFile());
261: File[] files = directory.listFiles();
262:
263: for (int i = 0; i < files.length; i++)
264: if ((files[i].getName().endsWith(".xml")) /* && (files[i].getName().startsWith("test18"))*/) {
265: Document test = getDocument(files[i].toString(),
266: new FileInputStream(files[i]));
267:
268: NodeList list = test.getDocumentElement()
269: .getElementsByTagName("step");
270: for (int j = 0; j < list.getLength(); j++) {
271: Element step = (Element) list.item(j);
272:
273: suite.addTest(new ParserProcessorTestCase(
274: "ParserProcessorTest "
275: + files[i].getName() + ":"
276: + step.getAttribute("name"),
277: step));
278: }
279: }
280: } catch (Exception e) {
281: e.printStackTrace();
282: }
283:
284: return suite;
285: }
286: }
|