Source Code Cross Referenced for ExtendedParserProcessorTestCase.java in  » Parser » chaperon-3.0 » net » sourceforge » chaperon » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » chaperon 3.0 » net.sourceforge.chaperon.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.Test;
012:        import junit.framework.TestCase;
013:        import junit.framework.TestSuite;
014:
015:        import net.sourceforge.chaperon.common.ConsoleLog;
016:        import net.sourceforge.chaperon.model.extended.ExtendedGrammar;
017:        import net.sourceforge.chaperon.process.extended.ExtendedDirectParserProcessor;
018:
019:        import org.custommonkey.xmlunit.Diff;
020:
021:        import org.exolab.castor.mapping.Mapping;
022:        import org.exolab.castor.xml.Unmarshaller;
023:
024:        import org.w3c.dom.Document;
025:        import org.w3c.dom.Element;
026:        import org.w3c.dom.Node;
027:        import org.w3c.dom.NodeList;
028:
029:        import org.xml.sax.InputSource;
030:        import org.xml.sax.XMLReader;
031:
032:        import java.io.File;
033:        import java.io.FileInputStream;
034:        import java.io.InputStream;
035:
036:        import javax.xml.parsers.SAXParserFactory;
037:        import javax.xml.transform.Transformer;
038:        import javax.xml.transform.TransformerFactory;
039:        import javax.xml.transform.dom.DOMResult;
040:        import javax.xml.transform.dom.DOMSource;
041:        import javax.xml.transform.sax.SAXResult;
042:        import javax.xml.transform.sax.SAXTransformerFactory;
043:        import javax.xml.transform.sax.TransformerHandler;
044:        import javax.xml.transform.stream.StreamResult;
045:
046:        public class ExtendedParserProcessorTestCase extends TestCase {
047:            private String name;
048:            private Element step;
049:
050:            public ExtendedParserProcessorTestCase(String name) {
051:                super (name);
052:                this .name = name;
053:            }
054:
055:            public ExtendedParserProcessorTestCase(String name, Element step) {
056:                super (name);
057:                this .name = name;
058:                this .step = step;
059:            }
060:
061:            public void runTest() throws Throwable {
062:                System.out.println("======================= Test " + name
063:                        + " =======================");
064:
065:                Document result = null;
066:                try {
067:                    ExtendedGrammar grammar = getExtendedGrammar(step);
068:
069:                    System.out.println("Grammar:\n" + grammar);
070:
071:                    ExtendedDirectParserProcessor processor = getExtendedParserProcessor(grammar);
072:
073:                    long time = System.currentTimeMillis();
074:                    result = process(processor, step);
075:                    System.out.println("process time = "
076:                            + (System.currentTimeMillis() - time) + " ms");
077:                } catch (Exception e) {
078:                    e.printStackTrace();
079:
080:                    NodeList list = step.getElementsByTagName("exception");
081:
082:                    Element exception = (Element) list.item(0);
083:                    if (exception != null)
084:                        assertEquals("Test if exception messages are equal",
085:                                exception.getAttribute("message"), e
086:                                        .getMessage());
087:                    else
088:                        fail("Doesn't expect exception: " + e.getMessage());
089:                }
090:
091:                Document expected = getResult(step);
092:
093:                if (expected != null)
094:                    assertEqual(expected, result);
095:                else if (result != null) {
096:                    System.out.println("actual:");
097:                    print(result);
098:                    fail("Result wasn't expected");
099:                }
100:            }
101:
102:            private ExtendedDirectParserProcessor getExtendedParserProcessor(
103:                    ExtendedGrammar grammar) {
104:                ExtendedDirectParserProcessor processor = new ExtendedDirectParserProcessor();
105:
106:                processor.setLog(new ConsoleLog(ConsoleLog.INFO));
107:                processor.setExtendedGrammar(grammar);
108:
109:                return processor;
110:            }
111:
112:            private Document process(ExtendedDirectParserProcessor processor,
113:                    Element step) throws Exception {
114:                NodeList list = step.getElementsByTagNameNS(
115:                        "http://chaperon.sourceforge.net/schema/text/1.0",
116:                        "text");
117:                Node node = list.item(0);
118:
119:                TransformerFactory streamerfactory = TransformerFactory
120:                        .newInstance();
121:                Transformer streamer = streamerfactory.newTransformer();
122:
123:                SAXTransformerFactory serializerfactory = (SAXTransformerFactory) SAXTransformerFactory
124:                        .newInstance();
125:                TransformerHandler serializer = serializerfactory
126:                        .newTransformerHandler();
127:                DOMResult result = new DOMResult();
128:                serializer.setResult(result);
129:
130:                processor.setContentHandler(new WhitespaceFilter(serializer,
131:                        new ConsoleLog(ConsoleLog.INFO)));
132:
133:                streamer.transform(new DOMSource(node),
134:                        new SAXResult(processor));
135:
136:                return (Document) result.getNode();
137:            }
138:
139:            private ExtendedGrammar getExtendedGrammar(Element step)
140:                    throws Exception {
141:                NodeList list = step.getElementsByTagNameNS(
142:                        "http://chaperon.sourceforge.net/schema/grammar/2.0",
143:                        "grammar");
144:                Node node = list.item(0);
145:
146:                Mapping mapping = new Mapping();
147:                mapping.loadMapping(new InputSource(ExtendedGrammar.class
148:                        .getResource("mapping.xml").openStream()));
149:
150:                Unmarshaller unmarshaller = new Unmarshaller(
151:                        ExtendedGrammar.class);
152:                unmarshaller.setMapping(mapping);
153:
154:                ExtendedGrammar grammar = (ExtendedGrammar) unmarshaller
155:                        .unmarshal(node);
156:
157:                return grammar;
158:            }
159:
160:            private Document getResult(Element step) throws Exception {
161:                NodeList list = step
162:                        .getElementsByTagNameNS(
163:                                "http://chaperon.sourceforge.net/schema/syntaxtree/2.0",
164:                                "output");
165:                Node node = list.item(0);
166:
167:                if (node == null)
168:                    return null;
169:
170:                TransformerFactory streamerfactory = TransformerFactory
171:                        .newInstance();
172:                Transformer streamer = streamerfactory.newTransformer();
173:
174:                DOMResult result = new DOMResult();
175:
176:                streamer.transform(new DOMSource(node), result);
177:
178:                return (Document) result.getNode();
179:            }
180:
181:            private static Document getDocument(String filename, InputStream in)
182:                    throws Exception {
183:                SAXParserFactory parserfactory = SAXParserFactory.newInstance();
184:                parserfactory.setNamespaceAware(true);
185:
186:                XMLReader parser = parserfactory.newSAXParser().getXMLReader();
187:
188:                SAXTransformerFactory serializerfactory = (SAXTransformerFactory) SAXTransformerFactory
189:                        .newInstance();
190:                TransformerHandler handler = serializerfactory
191:                        .newTransformerHandler();
192:                DOMResult result = new DOMResult();
193:                handler.setResult(result);
194:
195:                parser.setContentHandler(new WhitespaceFilter(handler));
196:
197:                InputSource inputsource = new InputSource(in);
198:                inputsource.setSystemId(filename);
199:                parser.parse(inputsource);
200:
201:                return (Document) result.getNode();
202:            }
203:
204:            public final void print(Document document) throws Exception {
205:                TransformerFactory factory = TransformerFactory.newInstance();
206:                javax.xml.transform.Transformer serializer = factory
207:                        .newTransformer();
208:                serializer.transform(new DOMSource(document), new StreamResult(
209:                        System.out));
210:                System.out.println();
211:            }
212:
213:            /**
214:             * Compare two XML documents provided as strings
215:             *
216:             * @param control Control document
217:             * @param test Document to test
218:             *
219:             * @return Diff object describing differences in documents
220:             */
221:            public final Diff compareXML(Document control, Document test) {
222:                return new Diff(control, test);
223:            }
224:
225:            /**
226:             * Assert that the result of an XML comparison is similar.
227:             *
228:             * @param msg The assertion message
229:             * @param expected The expected XML document
230:             * @param actual The actual XML Document
231:             */
232:            public final void assertEqual(String msg, Document expected,
233:                    Document actual) {
234:                expected.getDocumentElement().normalize();
235:                actual.getDocumentElement().normalize();
236:
237:                Diff diff = compareXML(expected, actual);
238:
239:                if (!diff.similar()) {
240:                    try {
241:                        System.out.println("expected:");
242:                        print(expected);
243:                        System.out.println("actual:");
244:                        print(actual);
245:                    } catch (Exception e) {
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 expected, Document actual) {
261:                expected.getDocumentElement().normalize();
262:                actual.getDocumentElement().normalize();
263:
264:                Diff diff = compareXML(expected, actual);
265:
266:                if (!diff.similar()) {
267:                    try {
268:                        System.out.println("expected:");
269:                        print(expected);
270:                        System.out.println("actual:");
271:                        print(actual);
272:                    } catch (Exception e) {
273:                        e.printStackTrace();
274:                    }
275:                }
276:
277:                assertEquals("Test if the assertion document is equal, "
278:                        + diff.toString(), true, diff.similar());
279:            }
280:
281:            public static Test suite() {
282:                TestSuite suite = new TestSuite("ExtendedParserProcessorTest");
283:
284:                try {
285:                    File directory = new File(
286:                            ExtendedParserProcessorTestCase.class.getResource(
287:                                    "extended").getFile());
288:                    File[] files = directory.listFiles();
289:
290:                    for (int i = 0; i < files.length; i++)
291:                        if ((files[i].getName().endsWith(".xml"))/* && (!files[i].getName().startsWith("stress"))*/) {
292:                            Document test = getDocument(files[i].toString(),
293:                                    new FileInputStream(files[i]));
294:
295:                            NodeList list = test.getDocumentElement()
296:                                    .getElementsByTagName("step");
297:                            for (int j = 0; j < list.getLength(); j++) {
298:                                Element step = (Element) list.item(j);
299:
300:                                suite
301:                                        .addTest(new ExtendedParserProcessorTestCase(
302:                                                "ExtendedParserProcessorTestCase "
303:                                                        + files[i].getName()
304:                                                        + ":"
305:                                                        + step
306:                                                                .getAttribute("name"),
307:                                                step));
308:                            }
309:                        }
310:                } catch (Exception e) {
311:                    e.printStackTrace();
312:                }
313:
314:                return suite;
315:            }
316:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.