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.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 ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.XMLReader ; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 36 import javax.xml.parsers.SAXParserFactory ; 37 import javax.xml.transform.Transformer ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMResult ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.sax.SAXResult ; 42 import javax.xml.transform.sax.SAXTransformerFactory ; 43 import javax.xml.transform.sax.TransformerHandler ; 44 import javax.xml.transform.stream.StreamResult ; 45 46 public class ExtendedParserProcessorTestCase extends TestCase 47 { 48 private String name; 49 private Element step; 50 51 public ExtendedParserProcessorTestCase(String name) 52 { 53 super(name); 54 this.name = name; 55 } 56 57 public ExtendedParserProcessorTestCase(String name, Element step) 58 { 59 super(name); 60 this.name = name; 61 this.step = step; 62 } 63 64 public void runTest() throws Throwable 65 { 66 System.out.println("======================= Test "+name+" ======================="); 67 68 Document 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 e) 82 { 83 e.printStackTrace(); 84 85 NodeList list = step.getElementsByTagName("exception"); 86 87 Element exception = (Element )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 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 process(ExtendedDirectParserProcessor processor, Element step) 118 throws Exception 119 { 120 NodeList list = 121 step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/text/1.0", "text"); 122 Node node = list.item(0); 123 124 TransformerFactory streamerfactory = TransformerFactory.newInstance(); 125 Transformer streamer = streamerfactory.newTransformer(); 126 127 SAXTransformerFactory serializerfactory = 128 (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 129 TransformerHandler serializer = serializerfactory.newTransformerHandler(); 130 DOMResult result = new DOMResult (); 131 serializer.setResult(result); 132 133 processor.setContentHandler(new WhitespaceFilter(serializer, new ConsoleLog(ConsoleLog.INFO))); 134 135 streamer.transform(new DOMSource (node), new SAXResult (processor)); 136 137 return (Document )result.getNode(); 138 } 139 140 private ExtendedGrammar getExtendedGrammar(Element step) 141 throws Exception 142 { 143 NodeList list = 144 step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/grammar/2.0", "grammar"); 145 Node node = list.item(0); 146 147 Mapping mapping = new Mapping(); 148 mapping.loadMapping(new InputSource (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 getResult(Element step) throws Exception 159 { 160 NodeList list = 161 step.getElementsByTagNameNS("http://chaperon.sourceforge.net/schema/syntaxtree/2.0", "output"); 162 Node node = list.item(0); 163 164 if (node==null) 165 return null; 166 167 TransformerFactory streamerfactory = TransformerFactory.newInstance(); 168 Transformer streamer = streamerfactory.newTransformer(); 169 170 DOMResult result = new DOMResult (); 171 172 streamer.transform(new DOMSource (node), result); 173 174 return (Document )result.getNode(); 175 } 176 177 private static Document getDocument(String filename, InputStream in) 178 throws Exception 179 { 180 SAXParserFactory parserfactory = SAXParserFactory.newInstance(); 181 parserfactory.setNamespaceAware(true); 182 183 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 184 185 SAXTransformerFactory serializerfactory = 186 (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 187 TransformerHandler handler = serializerfactory.newTransformerHandler(); 188 DOMResult result = new DOMResult (); 189 handler.setResult(result); 190 191 parser.setContentHandler(new WhitespaceFilter(handler)); 192 193 InputSource inputsource = new InputSource (in); 194 inputsource.setSystemId(filename); 195 parser.parse(inputsource); 196 197 return (Document )result.getNode(); 198 } 199 200 public final void print(Document document) throws Exception 201 { 202 TransformerFactory factory = TransformerFactory.newInstance(); 203 javax.xml.transform.Transformer serializer = factory.newTransformer(); 204 serializer.transform(new DOMSource (document), new StreamResult (System.out)); 205 System.out.println(); 206 } 207 208 216 public final Diff compareXML(Document control, Document test) 217 { 218 return new Diff(control, test); 219 } 220 221 228 public final void assertEqual(String msg, Document expected, Document 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 e) 245 { 246 e.printStackTrace(); 247 } 248 } 249 250 assertEquals(msg+", "+diff.toString(), true, diff.similar()); 251 } 252 253 260 public final void assertEqual(Document expected, Document 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 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 directory = 292 new File (ExtendedParserProcessorTestCase.class.getResource("extended").getFile()); 293 File [] files = directory.listFiles(); 294 295 for (int i = 0; i<files.length; i++) 296 if ((files[i].getName().endsWith(".xml"))) 297 { 298 Document test = getDocument(files[i].toString(), new FileInputStream (files[i])); 299 300 NodeList list = test.getDocumentElement().getElementsByTagName("step"); 301 for (int j = 0; j<list.getLength(); j++) 302 { 303 Element step = (Element )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 e) 312 { 313 e.printStackTrace(); 314 } 315 316 return suite; 317 } 318 } 319 | Popular Tags |