From: <bo...@us...> - 2008-03-07 11:52:51
|
Revision: 247 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=247&view=rev Author: bodewig Date: 2008-03-07 03:51:51 -0800 (Fri, 07 Mar 2008) Log Message: ----------- some RELAX NG experiments Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/jaxp13/Validator.java trunk/xmlunit/tests/etc/invalidBook.xml trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Validator.java Added Paths: ----------- trunk/xmlunit/tests/etc/Book.rng trunk/xmlunit/tests/etc/Book.rngc Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/jaxp13/Validator.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/jaxp13/Validator.java 2008-03-07 04:56:39 UTC (rev 246) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/jaxp13/Validator.java 2008-03-07 11:51:51 UTC (rev 247) @@ -50,10 +50,10 @@ /** * Validator class based of {@link javax.xml.validation javax.xml.validation}. * - * <p>This class currently only provides support for validating schema - * definitions. It defaults to the W3C XML Schema 1.0 but can be used - * to validate against any schema language supported by your - * SchemaFactory implementation.</p> + * <p>This class provides support for validating schema definitions as + * well as instance documents. It defaults to the W3C XML Schema 1.0 + * but can be used to validate against any schema language supported + * by your SchemaFactory implementation.</p> */ public class Validator { private final String schemaLanguage; @@ -122,7 +122,20 @@ try { parseSchema(new CollectingErrorHandler(l)); } catch (SAXException e) { - // error has been recorded in our ErrorHandler anyway + // error should have been recorded in our ErrorHandler, at + // least that's what the Javadocs say "SchemaFactory is + // not allowed to throw SAXException without first + // reporting it to ErrorHandler.". + // + // Unfortunately not all implementations seem to follow + // this rule. In particular using the setup described in + // org.custommonkey.xmlunit.jaxp13.test_Validator#XtestGoodRelaxNGCompactSyntaxIsValid() + // an exception ("SAXParseException: Content is not + // allowed in prolog.") will be thrown that never enters + // our Errorhandler. + if (l.size() == 0) { + l.add(e); + } } return l; } @@ -169,7 +182,11 @@ try { v.validate(instance); } catch (SAXException e) { - // error has been recorded in our ErrorHandler anyway + // error should have been recorded in our ErrorHandler, + // but better double-check. + if (l.size() == 0) { + l.add(e); + } } catch (java.io.IOException i) { throw new XMLUnitRuntimeException("Error reading instance source", i); Added: trunk/xmlunit/tests/etc/Book.rng =================================================================== --- trunk/xmlunit/tests/etc/Book.rng (rev 0) +++ trunk/xmlunit/tests/etc/Book.rng 2008-03-07 11:51:51 UTC (rev 247) @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<element name="Book" ns="http://www.publishing.org" + xmlns="http://relaxng.org/ns/structure/1.0"> + <element name="Title"><text/></element> + <oneOrMore> + <element name="Author"><text/></element> + </oneOrMore> + <element name="Date"><text/></element> + <element name="ISBN"><text/></element> + <element name="Publisher"><text/></element> +</element> Property changes on: trunk/xmlunit/tests/etc/Book.rng ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/xmlunit/tests/etc/Book.rngc =================================================================== --- trunk/xmlunit/tests/etc/Book.rngc (rev 0) +++ trunk/xmlunit/tests/etc/Book.rngc 2008-03-07 11:51:51 UTC (rev 247) @@ -0,0 +1,9 @@ +namespace b = "http://www.publishing.org" + +element b:Book { + element b:Title { text }, + element b:Author { text }+, + element b:Date { text }, + element b:ISBN { text }, + element b:Publisher { text } +} Property changes on: trunk/xmlunit/tests/etc/Book.rngc ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/xmlunit/tests/etc/invalidBook.xml =================================================================== --- trunk/xmlunit/tests/etc/invalidBook.xml 2008-03-07 04:56:39 UTC (rev 246) +++ trunk/xmlunit/tests/etc/invalidBook.xml 2008-03-07 11:51:51 UTC (rev 247) @@ -1,8 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <Book xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" - xmlns="http://www.publishing.org" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://www.publishing.org tests/etc/Book.xsd"> + xmlns="http://www.publishing.org"> <Title>Chicken Soup for the Soul</Title> <Author>Jack Canfield</Author> <Author>Mark Victor Hansen</Author> Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Validator.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Validator.java 2008-03-07 04:56:39 UTC (rev 246) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Validator.java 2008-03-07 11:51:51 UTC (rev 247) @@ -154,4 +154,76 @@ assertTrue(e.getCause() instanceof IOException); } } + + /** + * fails unless you manage to setup JAXP 1.3 and RELAX NG support + * + * <p>The setup that worked for Stefan when he wrote this test: + * JDK 1.5.0_09, isorelax-jaxp-bridge-1.0, together with msv.jar, + * isorelax.jar, relaxngDatatype.jar and xsdlib.jar from msv's + * latest nightly build (2008-02-13, actually). The same jars do + * not work with Java6.</p> + * + * @see http://weblogs.java.net/blog/kohsuke/archive/2006/02/validate_xml_us.html + */ + public void XtestGoodRelaxNGSchemaIsValid() throws Exception { + Validator v = new Validator(javax.xml.XMLConstants.RELAXNG_NS_URI); + v.addSchemaSource(new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/Book.rng"))); + assertTrue(v.isSchemaValid()); + } + + /** + * fails unless you manage to setup JAXP 1.3 and RELAX NG support + * @see #XtestGoodRelaxNGSchemaIsValid() + */ + public void XtestGoodInstanceIsValidRNG() throws Exception { + Validator v = new Validator(javax.xml.XMLConstants.RELAXNG_NS_URI); + v.addSchemaSource(new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/Book.rng"))); + StreamSource s = + new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/BookXsdGeneratedNoSchema.xml")); + assertTrue(v.isInstanceValid(s)); + } + + /** + * fails unless you manage to setup JAXP 1.3 and RELAX NG support + * @see #XtestGoodRelaxNGSchemaIsValid() + */ + public void XtestBadInstanceIsInvalidRNG() throws Exception { + Validator v = new Validator(javax.xml.XMLConstants.RELAXNG_NS_URI); + v.addSchemaSource(new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/Book.rng"))); + StreamSource s = + new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/invalidBook.xml")); + List l = v.getInstanceErrors(s); + for (Iterator i = l.iterator(); i.hasNext(); ) { + Object ex = i.next(); + assertTrue(ex instanceof SAXParseException); + /* + System.err.println(ex); + */ + } + assertTrue(l.size() > 0); + } + + /** + * fails even using the setup in XtestGoodRelaxNGSchemaIsValid() + * since a SAXParser is trying to read the compact syntax + * definition and chokes on it not being XML. + * @see #XtestGoodRelaxNGSchemaIsValid() + */ + public void XtestGoodRelaxNGCompactSyntaxIsValid() throws Exception { + Validator v = new Validator(javax.xml.XMLConstants.RELAXNG_NS_URI); + v.addSchemaSource(new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/Book.rngc"))); + assertTrue(v.isSchemaValid()); + StreamSource s = + new StreamSource(new File(test_Constants.BASEDIR + + "/tests/etc/BookXsdGeneratedNoSchema.xml")); + assertTrue(v.isInstanceValid(s)); + } + } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |