KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > GeneralParserProcessorTestCase


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

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import net.sourceforge.chaperon.build.Automaton;
16 import net.sourceforge.chaperon.common.ConsoleLog;
17 import net.sourceforge.chaperon.model.grammar.Grammar;
18 import net.sourceforge.chaperon.model.grammar.GrammarFactory;
19 import net.sourceforge.chaperon.process.GeneralParserProcessor;
20
21 import org.custommonkey.xmlunit.Diff;
22
23 import org.w3c.dom.Document JavaDoc;
24
25 import org.xml.sax.InputSource JavaDoc;
26 import org.xml.sax.XMLReader JavaDoc;
27
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.dom.DOMResult JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
33 import javax.xml.transform.sax.TransformerHandler JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35
36 public class GeneralParserProcessorTestCase extends TestCase
37 {
38   public GeneralParserProcessorTestCase(String JavaDoc name)
39   {
40     super(name);
41   }
42
43   public void testTest01() throws Exception JavaDoc
44   {
45     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test1.xgrm");
46     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
47     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test1.in");
48     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test1.out");
49
50     assertEqual(expected, result);
51   }
52
53 /*
54   public void testTest02() throws Exception
55   {
56     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test2.xgrm");
57     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
58     Document result = process(processor, "net/sourceforge/chaperon/test/parser/test2.in");
59     Document expected = getDocument("net/sourceforge/chaperon/test/parser/test2.out");
60
61     assertEqual(expected, result);
62   }*/

63   public void testTest03() throws Exception JavaDoc
64   {
65     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test3.xgrm");
66     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
67     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test3.in");
68     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test3.out");
69
70     assertEqual(expected, result);
71   }
72
73   public void testTest04() throws Exception JavaDoc
74   {
75     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test4.xgrm");
76     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
77     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test4.in");
78     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test4.out");
79
80     assertEqual(expected, result);
81   }
82
83   public void testTest04_2() throws Exception JavaDoc
84   {
85     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test15.xgrm");
86     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
87     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test15.in");
88     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test15.out");
89
90     assertEqual(expected, result);
91   }
92
93 /* public void testTest05() throws Exception
94   {
95     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test5.xgrm");
96     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
97     Document result = process(processor, "net/sourceforge/chaperon/test/parser/test5.in");
98     Document expected = getDocument("net/sourceforge/chaperon/test/parser/test5.out");
99
100     assertEqual(expected, result);
101   }*/

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

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

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

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

138 /* public void testTest10() throws Exception
139   {
140     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test10.xgrm");
141     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
142     Document result = process(processor, "net/sourceforge/chaperon/test/parser/test10.in");
143     Document expected = getDocument("net/sourceforge/chaperon/test/parser/test10.out");
144
145     assertEqual(expected, result);
146   }
147
148   public void testTest11() throws Exception
149   {
150     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test11.xgrm");
151     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
152     Document result = process(processor, "net/sourceforge/chaperon/test/parser/test11.in");
153     Document expected = getDocument("net/sourceforge/chaperon/test/parser/test11.out");
154
155     assertEqual(expected, result);
156   }
157
158   public void testTest12() throws Exception
159   {
160     System.out.println("=========================================");
161     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test12.xgrm");
162     System.out.println("grammar:\n"+grammar);
163     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
164     Document result = process(processor, "net/sourceforge/chaperon/test/parser/test12.in");
165     Document expected = getDocument("net/sourceforge/chaperon/test/parser/test12.out");
166
167     assertEqual(expected, result);
168   }*/

169 /* Endless loop*/
170   public void testTest13() throws Exception JavaDoc
171   {
172     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test13.xgrm");
173     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
174     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test13.in");
175     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test13.out");
176
177     assertEqual(expected, result);
178   }
179
180   public void testTest14() throws Exception JavaDoc
181   {
182     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test14.xgrm");
183     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
184     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test14.in");
185     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test14.out");
186
187     assertEqual(expected, result);
188   }
189
190   public void testTest16() throws Exception JavaDoc
191   {
192     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test16.xgrm");
193     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
194     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test16.in");
195     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test16.out");
196
197     assertEqual(expected, result);
198   }
199
200   public void testTest17() throws Exception JavaDoc
201   {
202     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test17.xgrm");
203     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
204     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test17.in");
205     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test17.out");
206
207     assertEqual(expected, result);
208   }
209
210   public void testTest18() throws Exception JavaDoc
211   {
212     Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test18.xgrm");
213     GeneralParserProcessor processor = getGeneralParserProcessor(grammar);
214     Document JavaDoc result = process(processor, "net/sourceforge/chaperon/test/parser/test18.in");
215     Document JavaDoc expected = getDocument("net/sourceforge/chaperon/test/parser/test18.out");
216
217     assertEqual(expected, result);
218   }
219
220   private GeneralParserProcessor getGeneralParserProcessor(Grammar grammar)
221   {
222     GeneralParserProcessor processor = new GeneralParserProcessor();
223     processor.setLog(new ConsoleLog());
224
225     /*Automaton automaton =
226       (new AutomatonBuilder(grammar, new ConsoleLog())).getParserAutomaton();*/

227
228     // (new AutomatonBuilder(grammar)).getParserAutomaton();
229
Automaton automaton = new Automaton(grammar, new ConsoleLog());
230
231     assertNotNull("Test if automaton is generated", automaton);
232
233     processor.setParserAutomaton(automaton);
234
235     return processor;
236   }
237
238   private Document JavaDoc process(GeneralParserProcessor processor, String JavaDoc in)
239     throws Exception JavaDoc
240   {
241     SAXParserFactory JavaDoc parserfactory = SAXParserFactory.newInstance();
242     parserfactory.setNamespaceAware(true);
243
244     XMLReader JavaDoc parser = parserfactory.newSAXParser().getXMLReader();
245
246     SAXTransformerFactory JavaDoc transformerfactory =
247       (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
248     TransformerHandler JavaDoc handler = transformerfactory.newTransformerHandler();
249     DOMResult JavaDoc result = new DOMResult JavaDoc();
250     handler.setResult(result);
251
252     processor.setContentHandler(new WhitespaceFilter(handler));
253
254     parser.setContentHandler(processor);
255     parser.parse(new InputSource JavaDoc(getClass().getClassLoader().getResourceAsStream(in)));
256
257     return (Document JavaDoc)result.getNode();
258   }
259
260   private Grammar getGrammar(String JavaDoc in) throws Exception JavaDoc
261   {
262     SAXParserFactory JavaDoc parserfactory = SAXParserFactory.newInstance();
263     parserfactory.setNamespaceAware(true);
264
265     XMLReader JavaDoc parser = parserfactory.newSAXParser().getXMLReader();
266
267     GrammarFactory grammarfactory = new GrammarFactory();
268     parser.setContentHandler(grammarfactory);
269     parser.parse(new InputSource JavaDoc(getClass().getClassLoader().getResourceAsStream(in)));
270
271     //System.out.println("====================================\nGrammar:\n"+grammarfactory.getGrammar()+"\n");
272
return grammarfactory.getGrammar();
273   }
274
275   private Document JavaDoc getDocument(String JavaDoc in) throws Exception JavaDoc
276   {
277     SAXParserFactory JavaDoc parserfactory = SAXParserFactory.newInstance();
278     parserfactory.setNamespaceAware(true);
279
280     XMLReader JavaDoc parser = parserfactory.newSAXParser().getXMLReader();
281
282     SAXTransformerFactory JavaDoc transformerfactory =
283       (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
284     TransformerHandler JavaDoc handler = transformerfactory.newTransformerHandler();
285     DOMResult JavaDoc result = new DOMResult JavaDoc();
286     handler.setResult(result);
287
288     parser.setContentHandler(new WhitespaceFilter(handler));
289     parser.parse(new InputSource JavaDoc(getClass().getClassLoader().getResourceAsStream(in)));
290
291     return (Document JavaDoc)result.getNode();
292   }
293
294   public final void print(Document JavaDoc document) throws Exception JavaDoc
295   {
296     TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
297     javax.xml.transform.Transformer JavaDoc serializer = factory.newTransformer();
298     serializer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(System.out));
299     System.out.println();
300   }
301
302   /**
303    * Compare two XML documents provided as strings
304    *
305    * @param control Control document
306    * @param test Document to test
307    *
308    * @return Diff object describing differences in documents
309    */

310   public final Diff compareXML(Document JavaDoc control, Document JavaDoc test)
311   {
312     return new Diff(control, test);
313   }
314
315   /**
316    * Assert that the result of an XML comparison is similar.
317    *
318    * @param msg The assertion message
319    * @param expected The expected XML document
320    * @param actual The actual XML Document
321    */

322   public final void assertEqual(String JavaDoc msg, Document JavaDoc expected, Document JavaDoc actual)
323   {
324     expected.getDocumentElement().normalize();
325     actual.getDocumentElement().normalize();
326
327     Diff diff = compareXML(expected, actual);
328
329     assertEquals(msg+", "+diff.toString(), true, diff.similar());
330   }
331
332   /**
333    * Assert that the result of an XML comparison is similar.
334    *
335    * @param msg The assertion message
336    * @param expected The expected XML document
337    * @param actual The actual XML Document
338    */

339   public final void assertEqual(Document JavaDoc expected, Document JavaDoc actual)
340   {
341     expected.getDocumentElement().normalize();
342     actual.getDocumentElement().normalize();
343
344     Diff diff = compareXML(expected, actual);
345
346     try
347     {
348       System.out.println("expected:");
349       print(expected);
350       System.out.println("actual:");
351       print(actual);
352     }
353     catch (Exception JavaDoc e) {}
354
355     assertEquals("Test if the assertion document is equal, "+diff.toString(), true, diff.similar());
356   }
357
358   public static Test suite()
359   {
360     return new TestSuite(GeneralParserProcessorTestCase.class);
361   }
362 }
363
Popular Tags