1 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 ; 24 25 import org.xml.sax.InputSource ; 26 import org.xml.sax.XMLReader ; 27 28 import javax.xml.parsers.SAXParserFactory ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.dom.DOMResult ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.sax.SAXTransformerFactory ; 33 import javax.xml.transform.sax.TransformerHandler ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 public class GeneralParserProcessorTestCase extends TestCase 37 { 38 public GeneralParserProcessorTestCase(String name) 39 { 40 super(name); 41 } 42 43 public void testTest01() throws Exception 44 { 45 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test1.xgrm"); 46 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 47 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test1.in"); 48 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test1.out"); 49 50 assertEqual(expected, result); 51 } 52 53 63 public void testTest03() throws Exception 64 { 65 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test3.xgrm"); 66 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 67 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test3.in"); 68 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test3.out"); 69 70 assertEqual(expected, result); 71 } 72 73 public void testTest04() throws Exception 74 { 75 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test4.xgrm"); 76 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 77 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test4.in"); 78 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test4.out"); 79 80 assertEqual(expected, result); 81 } 82 83 public void testTest04_2() throws Exception 84 { 85 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test15.xgrm"); 86 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 87 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test15.in"); 88 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test15.out"); 89 90 assertEqual(expected, result); 91 } 92 93 102 111 120 129 138 169 170 public void testTest13() throws Exception 171 { 172 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test13.xgrm"); 173 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 174 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test13.in"); 175 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test13.out"); 176 177 assertEqual(expected, result); 178 } 179 180 public void testTest14() throws Exception 181 { 182 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test14.xgrm"); 183 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 184 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test14.in"); 185 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test14.out"); 186 187 assertEqual(expected, result); 188 } 189 190 public void testTest16() throws Exception 191 { 192 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test16.xgrm"); 193 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 194 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test16.in"); 195 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test16.out"); 196 197 assertEqual(expected, result); 198 } 199 200 public void testTest17() throws Exception 201 { 202 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test17.xgrm"); 203 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 204 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test17.in"); 205 Document expected = getDocument("net/sourceforge/chaperon/test/parser/test17.out"); 206 207 assertEqual(expected, result); 208 } 209 210 public void testTest18() throws Exception 211 { 212 Grammar grammar = getGrammar("net/sourceforge/chaperon/test/parser/test18.xgrm"); 213 GeneralParserProcessor processor = getGeneralParserProcessor(grammar); 214 Document result = process(processor, "net/sourceforge/chaperon/test/parser/test18.in"); 215 Document 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 227 228 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 process(GeneralParserProcessor processor, String in) 239 throws Exception 240 { 241 SAXParserFactory parserfactory = SAXParserFactory.newInstance(); 242 parserfactory.setNamespaceAware(true); 243 244 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 245 246 SAXTransformerFactory transformerfactory = 247 (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 248 TransformerHandler handler = transformerfactory.newTransformerHandler(); 249 DOMResult result = new DOMResult (); 250 handler.setResult(result); 251 252 processor.setContentHandler(new WhitespaceFilter(handler)); 253 254 parser.setContentHandler(processor); 255 parser.parse(new InputSource (getClass().getClassLoader().getResourceAsStream(in))); 256 257 return (Document )result.getNode(); 258 } 259 260 private Grammar getGrammar(String in) throws Exception 261 { 262 SAXParserFactory parserfactory = SAXParserFactory.newInstance(); 263 parserfactory.setNamespaceAware(true); 264 265 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 266 267 GrammarFactory grammarfactory = new GrammarFactory(); 268 parser.setContentHandler(grammarfactory); 269 parser.parse(new InputSource (getClass().getClassLoader().getResourceAsStream(in))); 270 271 return grammarfactory.getGrammar(); 273 } 274 275 private Document getDocument(String in) throws Exception 276 { 277 SAXParserFactory parserfactory = SAXParserFactory.newInstance(); 278 parserfactory.setNamespaceAware(true); 279 280 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 281 282 SAXTransformerFactory transformerfactory = 283 (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 284 TransformerHandler handler = transformerfactory.newTransformerHandler(); 285 DOMResult result = new DOMResult (); 286 handler.setResult(result); 287 288 parser.setContentHandler(new WhitespaceFilter(handler)); 289 parser.parse(new InputSource (getClass().getClassLoader().getResourceAsStream(in))); 290 291 return (Document )result.getNode(); 292 } 293 294 public final void print(Document document) throws Exception 295 { 296 TransformerFactory factory = TransformerFactory.newInstance(); 297 javax.xml.transform.Transformer serializer = factory.newTransformer(); 298 serializer.transform(new DOMSource (document), new StreamResult (System.out)); 299 System.out.println(); 300 } 301 302 310 public final Diff compareXML(Document control, Document test) 311 { 312 return new Diff(control, test); 313 } 314 315 322 public final void assertEqual(String msg, Document expected, Document 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 339 public final void assertEqual(Document expected, Document 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 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 |