[Practicalxml-commits] SF.net SVN: practicalxml:[29] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-08 01:43:47
|
Revision: 29 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=29&view=rev Author: kdgregory Date: 2008-10-08 01:43:42 +0000 (Wed, 08 Oct 2008) Log Message: ----------- ParseUtil: rename "parseWithDTD" to "validatingParse" Modified Paths: -------------- trunk/src/main/java/net/sf/practicalxml/ParseUtil.java trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java Modified: trunk/src/main/java/net/sf/practicalxml/ParseUtil.java =================================================================== --- trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-08 00:08:37 UTC (rev 28) +++ trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-08 01:43:42 UTC (rev 29) @@ -84,7 +84,7 @@ * * @throws XmlException for any configuration or fatal execution error. */ - public static Document parseWithDTD( + public static Document validatingParse( InputSource source, EntityResolver resolver, ErrorHandler errHandler) { @@ -116,10 +116,10 @@ * * @throws XmlException for any configuration or fatal execution error. */ - public static Document parseWithDTD( + public static Document validatingParse( InputSource source, ErrorHandler errHandler) { - return parseWithDTD(source, null, errHandler); + return validatingParse(source, null, errHandler); } Modified: trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java =================================================================== --- trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-08 00:08:37 UTC (rev 28) +++ trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-08 01:43:42 UTC (rev 29) @@ -1,6 +1,5 @@ package net.sf.practicalxml; -import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; @@ -8,7 +7,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; -import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -22,15 +20,24 @@ // Support Code //---------------------------------------------------------------------------- + /** A basic DTD, shared between validating parser tests */ + private final static String BASIC_DTD + = "<!ELEMENT foo (bar*,baz+)>" + + "<!ELEMENT bar (#PCDATA)>" + + "<!ELEMENT baz EMPTY>" + + "<!ATTLIST foo name CDATA #REQUIRED>"; + + /** - * An error handler that records its invocations. + * An ErrorHandler that records its invocations, and provides asserts + * on them. */ - private static class MockErrorHandler + private static class TestErrorHandler implements ErrorHandler { - public List<SAXParseException> warnings = new ArrayList<SAXParseException>(); - public List<SAXParseException> errors = new ArrayList<SAXParseException>(); public List<SAXParseException> fatalErrors = new ArrayList<SAXParseException>(); + public List<SAXParseException> errors = new ArrayList<SAXParseException>(); + public List<SAXParseException> warnings = new ArrayList<SAXParseException>(); public void error(SAXParseException exception) throws SAXException { @@ -46,6 +53,13 @@ { warnings.add(exception); } + + public void assertResults(int numFatal, int numErrors, int numWarnings) + { + assertEquals("TestErrorHandler fatal errors", numFatal, fatalErrors.size()); + assertEquals("TestErrorHandler errors", numErrors, errors.size()); + assertEquals("TestErrorHandler warnings", numWarnings, warnings.size()); + } } @@ -86,23 +100,6 @@ } - public void testMalformedStringParseWithErrorHandler() throws Exception - { - String xml = "<foo><bar>baz</foo>"; - MockErrorHandler handler = new MockErrorHandler(); - try - { - ParseUtil.parse(new InputSource(new StringReader(xml)), handler); - fail("able to parse malformed XML"); - } - catch (XmlException e) - { - // even though we're handling the error, the parser will abend - } - assertEquals(1, handler.fatalErrors.size()); - } - - public void testNamespacedStringParse() throws Exception { String xml = "<foo xmlns:me=\"argle\">" @@ -136,103 +133,39 @@ } - public void testValidatingParseWithMissingDoctype() throws Exception + public void testValidDocumentWithInternalDoctype() throws Exception { - InputSource input = new InputSource(new StringReader( - "<foo>bar</foo>")); - MockErrorHandler errHandler = new MockErrorHandler(); + String xml + = "<!DOCTYPE foo [" + BASIC_DTD + "]>" + + "<foo name='zippy'>" + + "<bar>something here</bar>" + + "<baz/>" + + "</foo>"; - Document dom = ParseUtil.parseWithDTD(input, errHandler); - assertEquals("foo", dom.getDocumentElement().getNodeName()); - assertTrue(errHandler.errors.size() > 0); - assertTrue(errHandler.fatalErrors.size() == 0); - assertTrue(errHandler.warnings.size() == 0); + TestErrorHandler errHandler = new TestErrorHandler(); + Document doc = ParseUtil.validatingParse( + new InputSource(new StringReader(xml)), + errHandler); + + assertEquals("foo", doc.getDocumentElement().getTagName()); + errHandler.assertResults(0, 0, 0); } - public void testValidatingParseWithInternalDTD() throws Exception + public void testInvalidDocumentWithInternalDoctype() throws Exception { - String dtd - = "<!ELEMENT foo (bar*)>" - + "<!ELEMENT bar (#PCDATA)>" - + "<!ATTLIST foo baz CDATA #REQUIRED>"; + String xml + = "<!DOCTYPE foo [" + BASIC_DTD + "]>" + + "<foo>" + + "<bar>something here</bar>" + + "</foo>"; - // first test will be valid - InputSource in1 = new InputSource(new StringReader( - "<!DOCTYPE foo [" + dtd + "]>" - + "<foo baz=\"argle\">" - + "<bar>argle</bar>" - + "</foo>")); - MockErrorHandler eh1 = new MockErrorHandler(); + TestErrorHandler errHandler = new TestErrorHandler(); + Document doc = ParseUtil.validatingParse( + new InputSource(new StringReader(xml)), + errHandler); - Document dom1 = ParseUtil.parseWithDTD(in1, eh1); - assertEquals("foo", dom1.getDocumentElement().getNodeName()); - assertTrue(eh1.errors.size() == 0); - assertTrue(eh1.fatalErrors.size() == 0); - assertTrue(eh1.warnings.size() == 0); - - // second won't -- missing required attribute - InputSource in2 = new InputSource(new StringReader( - "<!DOCTYPE foo [" + dtd + "]>\n" - + "<foo>\n" - + "<bar>argle</bar>\n" - + "</foo>")); - MockErrorHandler eh2 = new MockErrorHandler(); - - Document dom2 = ParseUtil.parseWithDTD(in2, eh2); - assertEquals("foo", dom2.getDocumentElement().getNodeName()); - assertTrue(eh2.errors.size() > 0); - assertTrue(eh2.fatalErrors.size() == 0); - assertTrue(eh2.warnings.size() == 0); - - assertEquals(2, eh2.errors.get(0).getLineNumber()); -// assertEquals(4, eh2.errors.get(0).getColumnNumber()); + assertEquals("foo", doc.getDocumentElement().getTagName()); + errHandler.assertResults(0, 2, 0); } - - - // note: this will fail if you don't have Internet access - // ... also, unit tests shouldn't reach outside the JVM -// public void testValidatingParseWithExternalDTD() throws Exception -// { -// InputSource input = new InputSource(new StringReader( -// "<!DOCTYPE web-app SYSTEM \"http://java.sun.com/dtd/web-app_2_3.dtd\">" -// + "<web-app>" -// + "<display-name>" -// + "Simple Web-App" -// + "</display-name>" -// + "</web-app>")); -// MockErrorHandler errHandler = new MockErrorHandler(); -// -// Document dom = ParseUtil.parseWithDTD(input, null, errHandler); -// assertEquals("web-app", dom.getDocumentElement().getNodeName()); -// assertTrue(errHandler.errors.size() == 0); -// assertTrue(errHandler.fatalErrors.size() == 0); -// assertTrue(errHandler.warnings.size() == 0); -// } - - - public void testValidatingParseWithLocalResolver() throws Exception - { - InputSource input = new InputSource(new StringReader( - "<!DOCTYPE foo SYSTEM \"http://java.sun.com/dtd/web-app_2_3.dtd\">" - + "<foo><bar>baz</bar></foo>")); - MockErrorHandler errHandler = new MockErrorHandler(); - EntityResolver resolver = new EntityResolver() - { - public InputSource resolveEntity(String publicId, String systemId) - throws SAXException, IOException - { - // we don't care what they want; return what we want - return new InputSource(new StringReader( - "<!ELEMENT foo (bar*)>" - + "<!ELEMENT bar (#PCDATA)>")); - } - }; - - Document dom = ParseUtil.parseWithDTD(input, resolver, errHandler); - assertEquals("foo", dom.getDocumentElement().getNodeName()); - assertTrue(errHandler.errors.size() == 0); - assertTrue(errHandler.fatalErrors.size() == 0); - assertTrue(errHandler.warnings.size() == 0); - } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |