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