/* **************************************************************************** * Main_Test.java * * To Do: * * [jgrandy 10/16/2006] validate against DTD for es4doc * * ****************************************************************************/ /* J_LZ_COPYRIGHT_BEGIN ******************************************************* * Copyright 2006 Laszlo Systems, Inc. All Rights Reserved. * * Use is subject to license terms. * * J_LZ_COPYRIGHT_END *********************************************************/ package org.openlaszlo.es4doc; import java.io.*; import java.util.*; import junit.framework.*; import org.w3c.dom.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.custommonkey.xmlunit.*; import com.thaiopensource.util.PropertyMap; import com.thaiopensource.util.PropertyMapBuilder; import com.thaiopensource.validate.ValidateProperty; import com.thaiopensource.validate.ValidationDriver; import com.thaiopensource.xml.sax.ErrorHandlerImpl; public class ES4Doc_Test extends XMLTestCase { public ES4Doc_Test (String name) { super(name); } public void setUp () { setupXMLValidator(); } public void testVariableDeclarations () { String[] tests = { // Each case is input, expected-output // "" is automatically prepended to output "", "", // simple variable declaration without comment "var foo;", "", // simple multiple variable declaration without comment "var foo, bar;", "", // simple multiple variable declaration without comment "var foo; var bar;", "", // comment applying to variable statement (var foo) "/* this is a comment */ var foo;", "/* this is a comment */", // alternative comment syntax "// this is a comment\n var foo;", "// this is a comment", // comment applying to all decls in a multiple-decl var statement "/* this is a comment */ var foo, bar;", "/* this is a comment *//* this is a comment */", // comment applying to variable declaration ("foo") "var /* this is a comment */ foo;", "/* this is a comment */", // separate comments applying to each variable decl in a statement "var /* comment A */ foo, /* comment B */ bar;", "/* comment A *//* comment B */", }; iterateTests(tests); } public void testFunctionDefinitions () { String[] tests = { // Each case is input, expected-output // "" is automatically prepended to output // simple function definition "function foo () { }", "", // simple function definition with comment "/* this is a comment */ function foo () { }", "/* this is a comment */", // alternative comment syntax "// this is a comment\n function foo () { }", "// this is a comment", // function definition with one parameter "function foo (bar) {}", "", // function definition with three parameters "function foo (bar, bazzle, batchelder) {}", "", }; iterateTests(tests); } public void testClassDefinitions () { String[] tests = { // Each case is input, expected-output // "" is automatically prepended to output // simple class declaration, no comment "class foo {};", "", // simple class declaration with comment "/* this is a comment */ class foo {};", "/* this is a comment */", "// this is a comment\n class foo {};", "// this is a comment", // class declaration with superclass, no comment "class foo extends bar {};", "", // class declaration with superclass, with comment "/* this is a comment */ class foo extends bar {};", "/* this is a comment */", "// this is a comment\n class foo extends bar {};", "// this is a comment", // no comment, but an attribute declaration "class foo { var bar; };", "", // no comment, but an attribute declaration "class foo { var bar, baz; };", "", // no comment, but an attribute declaration "class foo { var bar; var baz; };", "", // no comment, but an attribute declaration "class foo { function bar () {}; };", "", // no comment, but an attribute declaration "class foo { function bar (baz) {}; };", "", }; iterateTests(tests); } public void testIfDirective () { String[] tests = { // Each case is input, expected-output // "" is automatically prepended to output "if (false) { var foo; }", "", "if (true) { var foo; }", "", "if ($dhtml) { var foo; }", "", "if ($swf7) { var foo; }", "", "if ($swf7) { // this is a comment\n var foo; }", "// this is a comment", "if ($swf7) { /* this is a comment */ var foo; }", "/* this is a comment */", "if ($swf7) { /* this is a comment */ var foo; // another comment\n var bar; }", "/* this is a comment */// another comment", "if ($swf7) { /* this is a comment */ var foo; function bar (baz) {} }", "/* this is a comment */", }; iterateTests(tests); } private void iterateTests(String[] tests) { for (Iterator iter = Arrays.asList(tests).iterator(); iter.hasNext();) { String source = (String) iter.next(); assertTrue(iter.hasNext()); String result = "" + (String) iter.next(); try { Document control = XMLUnit.buildControlDocument(result); Document test = ES4Doc.toXML(source); Diff diff = new Diff(control, test); String testString = xmlToString(test); Boolean testValid = xmlValidates(testString), expectValid = xmlValidates(result); if (diff.identical() == false || testValid == false || expectValid == false) { System.out.println("identical: " + diff.identical()); System.out.println("test valid: " + testValid); System.out.println("expect valid: " + expectValid); System.out.println("input: " + source); System.out.println("output: " + testString); System.out.println("expect: " + result); } assertTrue("ES4Doc.toXML(\"" + source + "\") valid", testValid); assertTrue("ES4Doc.toXML(\"" + source + "\") expect valid", expectValid); assertXMLIdentical(diff, true, "ES4Doc.toXML(\"" + source + "\")"); } catch (org.xml.sax.SAXException exc) { fail("ES4Doc.toXML(\"" + source + "\")"); } catch (java.io.IOException exc) { fail("ES4Doc.toXML(\"" + source + "\")"); } catch (javax.xml.parsers.ParserConfigurationException exc) { fail("ES4Doc.toXML(\"" + source + "\")"); } } } private ValidationDriver validationDriver; private StringWriter errorBuffer; private void setupXMLValidator() { PropertyMapBuilder props = new PropertyMapBuilder(); errorBuffer = new StringWriter(); ErrorHandler eh = new ErrorHandlerImpl(errorBuffer); props.put(ValidateProperty.ERROR_HANDLER, eh); validationDriver = new ValidationDriver(props.toPropertyMap()); try { FileInputStream rncFileStream = new FileInputStream(System.getProperty("ES4DOC_RNG")); InputSource rngInputSource = new InputSource(rncFileStream); validationDriver.loadSchema(rngInputSource); } catch (FileNotFoundException e) { System.out.println("couldn't find schema: " + e); return; } catch (IOException e) { System.out.println("error opening schema: " + e); return; } catch (SAXException e) { System.out.println("error loading schema: " + e); return; } } private Boolean xmlValidates(String source) { Boolean valid = false; StringReader xmlStringReader = new StringReader(source); InputSource xmlStringSource = new InputSource(xmlStringReader); try { valid = validationDriver.validate(xmlStringSource); } catch (SAXException e) { System.out.println("sax exception: " + e); } catch (IOException e) { System.out.println("io exception: " + e); } return valid; } private static String xmlToString(Node node) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; } }