1 8 9 package net.sourceforge.chaperon.test; 10 11 import junit.framework.*; 12 13 import net.sourceforge.chaperon.build.ParserAutomatonBuilder; 14 import net.sourceforge.chaperon.common.ConsoleLog; 15 import net.sourceforge.chaperon.model.grammar.Grammar; 16 import net.sourceforge.chaperon.model.grammar.GrammarFactory; 17 import net.sourceforge.chaperon.process.*; 18 19 import org.custommonkey.xmlunit.Diff; 20 21 import org.w3c.dom.*; 22 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.XMLReader ; 25 26 import java.io.*; 27 28 import javax.xml.parsers.*; 29 import javax.xml.transform.*; 30 import javax.xml.transform.dom.*; 31 import javax.xml.transform.sax.*; 32 import javax.xml.transform.stream.*; 33 34 public class ParserProcessorTestCase extends TestCase 35 { 36 private String name; 37 private Element step; 38 39 public ParserProcessorTestCase(String name) 40 { 41 super(name); 42 this.name = name; 43 } 44 45 public ParserProcessorTestCase(String name, Element step) 46 { 47 super(name); 48 this.name = name; 49 this.step = step; 50 } 51 52 public void runTest() throws Throwable 53 { 54 System.out.println("======================= "+name+" ======================="); 55 56 Grammar grammar = getGrammar(step); 57 58 ParserProcessor processor = getParserProcessor(grammar); 60 61 Document result = null; 62 63 result = process(processor, step); 64 65 Document expected = getResult(step); 66 67 assertEqual(expected, result); 68 } 69 70 private ParserProcessor getParserProcessor(Grammar grammar) 71 { 72 ParserProcessor processor = new ParserProcessor(); 73 processor.setLog(new ConsoleLog()); 74 75 ParserAutomaton automaton = 76 (new ParserAutomatonBuilder(grammar, new ConsoleLog())).getParserAutomaton(); 77 78 assertNotNull("Test if automaton is generated", automaton); 80 81 processor.setParserAutomaton(automaton); 83 84 NodeList list = step.getElementsByTagName("parameter"); 85 for (int i = 0; i<list.getLength(); i++) 86 { 87 Element element = (Element)list.item(i); 88 if (element.getAttribute("name").equals("flatten")) 89 processor.setFlatten(Boolean.valueOf(element.getAttribute("value")).booleanValue()); 90 } 91 92 return processor; 93 } 94 95 private Document process(ParserProcessor processor, Element step) 96 throws Exception 97 { 98 NodeList list = 99 step.getElementsByTagNameNS(LexicalProcessor.NS_OUTPUT, LexicalProcessor.OUTPUT); 100 Node node = list.item(0); 101 102 TransformerFactory streamerfactory = TransformerFactory.newInstance(); 103 Transformer streamer = streamerfactory.newTransformer(); 104 105 SAXTransformerFactory serializerfactory = 106 (SAXTransformerFactory)SAXTransformerFactory.newInstance(); 107 TransformerHandler serializer = serializerfactory.newTransformerHandler(); 108 DOMResult result = new DOMResult(); 109 serializer.setResult(result); 110 111 processor.setContentHandler(new WhitespaceFilter(serializer)); 112 113 streamer.transform(new DOMSource(node), new SAXResult(processor)); 114 115 return (Document)result.getNode(); 116 } 117 118 private Grammar getGrammar(Element step) throws Exception 119 { 120 NodeList list = 121 step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/grammar/1.0", "grammar"); 122 Node node = list.item(0); 123 124 TransformerFactory streamerfactory = TransformerFactory.newInstance(); 125 Transformer streamer = streamerfactory.newTransformer(); 126 127 GrammarFactory grammarfactory = new GrammarFactory(); 128 129 streamer.transform(new DOMSource(node), new SAXResult(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 { 140 NodeList list = step.getElementsByTagNameNS(ParserProcessor.NS_OUTPUT, ParserProcessor.OUTPUT); 141 Node node = list.item(0); 142 143 TransformerFactory streamerfactory = TransformerFactory.newInstance(); 144 Transformer streamer = streamerfactory.newTransformer(); 145 146 DOMResult result = new DOMResult(); 148 149 streamer.transform(new DOMSource(node), result); 150 151 return (Document)result.getNode(); 152 } 153 154 private static Document getDocument(String filename, InputStream in) 155 throws Exception 156 { 157 SAXParserFactory parserfactory = SAXParserFactory.newInstance(); 158 parserfactory.setNamespaceAware(true); 159 160 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 161 162 SAXTransformerFactory serializerfactory = 163 (SAXTransformerFactory)SAXTransformerFactory.newInstance(); 164 TransformerHandler handler = serializerfactory.newTransformerHandler(); 165 DOMResult result = new DOMResult(); 166 handler.setResult(result); 167 168 parser.setContentHandler(new WhitespaceFilter(handler)); 169 170 InputSource inputsource = new InputSource (in); 171 inputsource.setSystemId(filename); 172 parser.parse(inputsource); 173 174 return (Document)result.getNode(); 175 } 176 177 public final void print(Document document) throws Exception 178 { 179 TransformerFactory factory = TransformerFactory.newInstance(); 180 javax.xml.transform.Transformer serializer = factory.newTransformer(); 181 serializer.transform(new DOMSource(document), new StreamResult(System.out)); 182 System.out.println(); 183 } 184 185 193 public final Diff compareXML(Document control, Document test) 194 { 195 return new Diff(control, test); 196 } 197 198 205 public final void assertEqual(String msg, Document expected, Document actual) 206 { 207 expected.getDocumentElement().normalize(); 208 actual.getDocumentElement().normalize(); 209 210 Diff diff = compareXML(expected, actual); 211 212 if (!diff.similar()) 213 { 214 try 215 { 216 System.out.println("expected:"); 217 print(expected); 218 System.out.println("actual:"); 219 print(actual); 220 } 221 catch (Exception e) 222 { 223 e.printStackTrace(); 224 } 225 } 226 227 assertEquals(msg+", "+diff.toString(), true, diff.similar()); 228 } 229 230 237 public final void assertEqual(Document expected, Document actual) 238 { 239 expected.getDocumentElement().normalize(); 240 actual.getDocumentElement().normalize(); 241 242 Diff diff = compareXML(expected, actual); 243 244 if (!diff.similar()) 245 { 246 try 247 { 248 System.out.println("expected:"); 249 print(expected); 250 System.out.println("actual:"); 251 print(actual); 252 } 253 catch (Exception e) 254 { 255 e.printStackTrace(); 256 } 257 } 258 259 assertEquals("Test if the assertion document is equal, "+diff.toString(), true, diff.similar()); 260 } 261 262 public static Test suite() 263 { 264 TestSuite suite = new TestSuite("ParserProcessorTest"); 265 266 try 267 { 268 File directory = new File(ParserProcessorTestCase.class.getResource("parser").getFile()); 269 File[] files = directory.listFiles(); 270 271 for (int i = 0; i<files.length; i++) 272 if ((files[i].getName().endsWith(".xml")) ) 273 { 274 Document test = getDocument(files[i].toString(), new FileInputStream(files[i])); 275 276 NodeList list = test.getDocumentElement().getElementsByTagName("step"); 277 for (int j = 0; j<list.getLength(); j++) 278 { 279 Element step = (Element)list.item(j); 280 281 suite.addTest(new ParserProcessorTestCase("ParserProcessorTest "+files[i].getName()+":"+ 282 step.getAttribute("name"), step)); 283 } 284 } 285 } 286 catch (Exception e) 287 { 288 e.printStackTrace(); 289 } 290 291 return suite; 292 } 293 } 294 | Popular Tags |