KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.ConsoleLog;
16 import net.sourceforge.chaperon.model.extended.ExtendedGrammar;
17 import net.sourceforge.chaperon.process.extended.ExtendedDirectParserProcessor;
18
19 import org.custommonkey.xmlunit.Diff;
20
21 import org.exolab.castor.mapping.Mapping;
22 import org.exolab.castor.xml.Unmarshaller;
23
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37 import javax.xml.transform.Transformer JavaDoc;
38 import javax.xml.transform.TransformerFactory JavaDoc;
39 import javax.xml.transform.dom.DOMResult JavaDoc;
40 import javax.xml.transform.dom.DOMSource JavaDoc;
41 import javax.xml.transform.sax.SAXResult JavaDoc;
42 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
43 import javax.xml.transform.sax.TransformerHandler JavaDoc;
44 import javax.xml.transform.stream.StreamResult JavaDoc;
45
46 public class ExtendedParserProcessorTestCase extends TestCase
47 {
48   private String JavaDoc name;
49   private Element JavaDoc step;
50
51   public ExtendedParserProcessorTestCase(String JavaDoc name)
52   {
53     super(name);
54     this.name = name;
55   }
56
57   public ExtendedParserProcessorTestCase(String JavaDoc name, Element JavaDoc step)
58   {
59     super(name);
60     this.name = name;
61     this.step = step;
62   }
63
64   public void runTest() throws Throwable JavaDoc
65   {
66     System.out.println("======================= Test "+name+" =======================");
67
68     Document JavaDoc result = null;
69     try
70     {
71       ExtendedGrammar grammar = getExtendedGrammar(step);
72
73       System.out.println("Grammar:\n"+grammar);
74
75       ExtendedDirectParserProcessor processor = getExtendedParserProcessor(grammar);
76
77       long time = System.currentTimeMillis();
78       result = process(processor, step);
79       System.out.println("process time = "+(System.currentTimeMillis()-time)+" ms");
80     }
81     catch (Exception JavaDoc e)
82     {
83       e.printStackTrace();
84
85       NodeList JavaDoc list = step.getElementsByTagName("exception");
86
87       Element JavaDoc exception = (Element JavaDoc)list.item(0);
88       if (exception!=null)
89         assertEquals("Test if exception messages are equal", exception.getAttribute("message"),
90                      e.getMessage());
91       else
92         fail("Doesn't expect exception: "+e.getMessage());
93     }
94
95     Document JavaDoc expected = getResult(step);
96
97     if (expected!=null)
98       assertEqual(expected, result);
99     else if (result!=null)
100     {
101       System.out.println("actual:");
102       print(result);
103       fail("Result wasn't expected");
104     }
105   }
106
107   private ExtendedDirectParserProcessor getExtendedParserProcessor(ExtendedGrammar grammar)
108   {
109     ExtendedDirectParserProcessor processor = new ExtendedDirectParserProcessor();
110
111     processor.setLog(new ConsoleLog(ConsoleLog.INFO));
112     processor.setExtendedGrammar(grammar);
113
114     return processor;
115   }
116
117   private Document JavaDoc process(ExtendedDirectParserProcessor processor, Element JavaDoc step)
118     throws Exception JavaDoc
119   {
120     NodeList JavaDoc list =
121       step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/text/1.0", "text");
122     Node JavaDoc node = list.item(0);
123
124     TransformerFactory JavaDoc streamerfactory = TransformerFactory.newInstance();
125     Transformer JavaDoc streamer = streamerfactory.newTransformer();
126
127     SAXTransformerFactory JavaDoc serializerfactory =
128       (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
129     TransformerHandler JavaDoc serializer = serializerfactory.newTransformerHandler();
130     DOMResult JavaDoc result = new DOMResult JavaDoc();
131     serializer.setResult(result);
132
133     processor.setContentHandler(new WhitespaceFilter(serializer, new ConsoleLog(ConsoleLog.INFO)));
134
135     streamer.transform(new DOMSource JavaDoc(node), new SAXResult JavaDoc(processor));
136
137     return (Document JavaDoc)result.getNode();
138   }
139
140   private ExtendedGrammar getExtendedGrammar(Element JavaDoc step)
141     throws Exception JavaDoc
142   {
143     NodeList JavaDoc list =
144       step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/grammar/2.0", "grammar");
145     Node JavaDoc node = list.item(0);
146
147     Mapping mapping = new Mapping();
148     mapping.loadMapping(new InputSource JavaDoc(ExtendedGrammar.class.getResource("mapping.xml").openStream()));
149
150     Unmarshaller unmarshaller = new Unmarshaller(ExtendedGrammar.class);
151     unmarshaller.setMapping(mapping);
152
153     ExtendedGrammar grammar = (ExtendedGrammar)unmarshaller.unmarshal(node);
154
155     return grammar;
156   }
157
158   private Document JavaDoc getResult(Element JavaDoc step) throws Exception JavaDoc
159   {
160     NodeList JavaDoc list =
161       step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/syntaxtree/2.0", "output");
162     Node JavaDoc node = list.item(0);
163
164     if (node==null)
165       return null;
166
167     TransformerFactory JavaDoc streamerfactory = TransformerFactory.newInstance();
168     Transformer JavaDoc streamer = streamerfactory.newTransformer();
169
170     DOMResult JavaDoc result = new DOMResult JavaDoc();
171
172     streamer.transform(new DOMSource JavaDoc(node), result);
173
174     return (Document JavaDoc)result.getNode();
175   }
176
177   private static Document JavaDoc getDocument(String JavaDoc filename, InputStream JavaDoc in)
178     throws Exception JavaDoc
179   {
180     SAXParserFactory JavaDoc parserfactory = SAXParserFactory.newInstance();
181     parserfactory.setNamespaceAware(true);
182
183     XMLReader JavaDoc parser = parserfactory.newSAXParser().getXMLReader();
184
185     SAXTransformerFactory JavaDoc serializerfactory =
186       (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
187     TransformerHandler JavaDoc handler = serializerfactory.newTransformerHandler();
188     DOMResult JavaDoc result = new DOMResult JavaDoc();
189     handler.setResult(result);
190
191     parser.setContentHandler(new WhitespaceFilter(handler));
192
193     InputSource JavaDoc inputsource = new InputSource JavaDoc(in);
194     inputsource.setSystemId(filename);
195     parser.parse(inputsource);
196
197     return (Document JavaDoc)result.getNode();
198   }
199
200   public final void print(Document JavaDoc document) throws Exception JavaDoc
201   {
202     TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
203     javax.xml.transform.Transformer JavaDoc serializer = factory.newTransformer();
204     serializer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(System.out));
205     System.out.println();
206   }
207
208   /**
209    * Compare two XML documents provided as strings
210    *
211    * @param control Control document
212    * @param test Document to test
213    *
214    * @return Diff object describing differences in documents
215    */

216   public final Diff compareXML(Document JavaDoc control, Document JavaDoc test)
217   {
218     return new Diff(control, test);
219   }
220
221   /**
222    * Assert that the result of an XML comparison is similar.
223    *
224    * @param msg The assertion message
225    * @param expected The expected XML document
226    * @param actual The actual XML Document
227    */

228   public final void assertEqual(String JavaDoc msg, Document JavaDoc expected, Document JavaDoc actual)
229   {
230     expected.getDocumentElement().normalize();
231     actual.getDocumentElement().normalize();
232
233     Diff diff = compareXML(expected, actual);
234
235     if (!diff.similar())
236     {
237       try
238       {
239         System.out.println("expected:");
240         print(expected);
241         System.out.println("actual:");
242         print(actual);
243       }
244       catch (Exception JavaDoc e)
245       {
246         e.printStackTrace();
247       }
248     }
249
250     assertEquals(msg+", "+diff.toString(), true, diff.similar());
251   }
252
253   /**
254    * Assert that the result of an XML comparison is similar.
255    *
256    * @param msg The assertion message
257    * @param expected The expected XML document
258    * @param actual The actual XML Document
259    */

260   public final void assertEqual(Document JavaDoc expected, Document JavaDoc actual)
261   {
262     expected.getDocumentElement().normalize();
263     actual.getDocumentElement().normalize();
264
265     Diff diff = compareXML(expected, actual);
266
267     if (!diff.similar())
268     {
269       try
270       {
271         System.out.println("expected:");
272         print(expected);
273         System.out.println("actual:");
274         print(actual);
275       }
276       catch (Exception JavaDoc e)
277       {
278         e.printStackTrace();
279       }
280     }
281
282     assertEquals("Test if the assertion document is equal, "+diff.toString(), true, diff.similar());
283   }
284
285   public static Test suite()
286   {
287     TestSuite suite = new TestSuite("ExtendedParserProcessorTest");
288
289     try
290     {
291       File JavaDoc directory =
292         new File JavaDoc(ExtendedParserProcessorTestCase.class.getResource("extended").getFile());
293       File JavaDoc[] files = directory.listFiles();
294
295       for (int i = 0; i<files.length; i++)
296         if ((files[i].getName().endsWith(".xml"))/* && (!files[i].getName().startsWith("stress"))*/)
297         {
298           Document JavaDoc test = getDocument(files[i].toString(), new FileInputStream JavaDoc(files[i]));
299
300           NodeList JavaDoc list = test.getDocumentElement().getElementsByTagName("step");
301           for (int j = 0; j<list.getLength(); j++)
302           {
303             Element JavaDoc step = (Element JavaDoc)list.item(j);
304
305             suite.addTest(new ExtendedParserProcessorTestCase("ExtendedParserProcessorTestCase "+
306                                                               files[i].getName()+":"+
307                                                               step.getAttribute("name"), step));
308           }
309         }
310     }
311     catch (Exception JavaDoc e)
312     {
313       e.printStackTrace();
314     }
315
316     return suite;
317   }
318 }
319
Popular Tags