[jboss-cvs] JBossAS SVN: r58642 - trunk/testsuite/src/main/org/jboss/test/xml
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Nov 20 12:12:31 EST 2006
Author: alex.loubyansky at jboss.com
Date: 2006-11-20 12:12:24 -0500 (Mon, 20 Nov 2006)
New Revision: 58642
Added:
trunk/testsuite/src/main/org/jboss/test/xml/DDValidatorUnitTestCase.java
Removed:
trunk/testsuite/src/main/org/jboss/test/xml/Validator.java
Log:
a testcase to validate the DDs in the testsuite
Added: trunk/testsuite/src/main/org/jboss/test/xml/DDValidatorUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/xml/DDValidatorUnitTestCase.java 2006-11-20 11:32:11 UTC (rev 58641)
+++ trunk/testsuite/src/main/org/jboss/test/xml/DDValidatorUnitTestCase.java 2006-11-20 17:12:24 UTC (rev 58642)
@@ -0,0 +1,274 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.xml;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.SAXParser;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.SAXException;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.helpers.DefaultHandler;
+import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.test.JBossTestCase;
+
+
+/**
+ * Validates all the descriptors in the testsuite resources.
+ *
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: $</tt>
+ */
+public class DDValidatorUnitTestCase
+ extends JBossTestCase
+{
+ private static final SAXParserFactory FACTORY = SAXParserFactory.newInstance();
+ static
+ {
+ FACTORY.setValidating(true);
+ FACTORY.setNamespaceAware(true);
+ }
+
+ private int total;
+ private int invalid;
+
+ public DDValidatorUnitTestCase(String localName)
+ {
+ super(localName);
+ }
+
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ total = 0;
+ invalid = 0;
+ }
+
+ public void testEjbJar() throws Exception
+ {
+ validate("ejb-jar.xml");
+ }
+
+ public void testJBossXml() throws Exception
+ {
+ validate("jboss.xml");
+ }
+
+ public void testJBossCmpJdbc() throws Exception
+ {
+ validate("jbosscmp-jdbc.xml");
+ }
+
+ public void testWebXml() throws Exception
+ {
+ validate("web.xml");
+ }
+
+ public void testJBossWeb() throws Exception
+ {
+ validate("jboss-web.xml");
+ }
+
+ public void testApplicationXml() throws Exception
+ {
+ validate("application.xml");
+ }
+
+ public void testJBossApp() throws Exception
+ {
+ validate("jboss-app.xml");
+ }
+
+ // private
+
+ private void validate(String ddName)
+ {
+ File resources = new File("resources");
+ scan(resources, Arrays.asList(ddName), false);
+ assertEquals(invalid + " from " + total + " are invalid", 0, invalid);
+ }
+
+ private void scan(java.io.File f, final List names, final boolean failIfInvalid)
+ {
+ f.listFiles(new FileFilter()
+ {
+ public boolean accept(File pathname)
+ {
+ if (pathname.isDirectory())
+ {
+ scan(pathname, names, failIfInvalid);
+ return true;
+ }
+
+ if (names.contains(pathname.getName()))
+ {
+ ++total;
+ if (!validate(pathname, failIfInvalid))
+ {
+ ++invalid;
+ }
+ return false;
+ }
+
+ return false;
+ }
+ });
+ }
+
+ private boolean validate(File file, boolean failIfInvalid)
+ {
+ InputStream is;
+ try
+ {
+ is = file.toURL().openStream();
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException("Failed to open file: " + file.getAbsolutePath(), e);
+ }
+
+ boolean valid;
+ try
+ {
+ parse(is, new JBossEntityResolver());
+ valid = true;
+ }
+ catch (JBossXBRuntimeException e)
+ {
+ valid = false;
+ if (e.getCause() instanceof SAXException)
+ {
+ SAXException sax = (SAXException) e.getCause();
+
+ StringBuffer msg = new StringBuffer();
+ msg.append("Failed to parse: ").append(file.getAbsolutePath()).append(": ").append(sax.getMessage());
+
+ if (sax instanceof SAXParseException)
+ {
+ SAXParseException parseException = (SAXParseException) sax;
+ msg.append(" [").append(parseException.getLineNumber()).append(",").append(
+ parseException.getColumnNumber()).append("]");
+ }
+
+ if (failIfInvalid)
+ {
+ fail(msg.toString());
+ }
+ else
+ {
+ getLog().debug(msg.toString());
+ }
+ }
+ else
+ {
+ throw e;
+ }
+ }
+
+ return valid;
+ }
+
+ private static void parse(InputStream xmlIs, final EntityResolver resolver)
+ {
+ SAXParser parser = null;
+ try
+ {
+ parser = FACTORY.newSAXParser();
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException("Failed to instantiate a SAX parser: " + e.getMessage());
+ }
+
+ try
+ {
+ parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
+ }
+ catch (SAXException e)
+ {
+ throw new IllegalStateException("Schema validation feature is not supported by the parser: " + e.getMessage());
+ }
+
+ try
+ {
+ parser.parse(xmlIs, new DefaultHandler()
+ {
+ public void warning(SAXParseException e)
+ {
+ }
+
+ public void error(SAXParseException e)
+ {
+ throw new JBossXBRuntimeException("Error", e);
+ }
+
+ public void fatalError(SAXParseException e)
+ {
+ throw new JBossXBRuntimeException("Fatal error", e);
+ }
+
+ public InputSource resolveEntity(String publicId, String systemId)
+ {
+ InputSource is = null;
+ if (resolver != null)
+ {
+ try
+ {
+ is = resolver.resolveEntity(publicId, systemId);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException("Failed to resolveEntity " + systemId + ": " + systemId);
+ }
+ }
+
+ if(is == null)
+ {
+ fail("Failed to resolve entity: publicId=" + publicId + " systemId=" + systemId);
+ }
+
+ return is;
+ }
+ });
+ }
+ catch(JBossXBRuntimeException e)
+ {
+ throw e;
+ }
+ catch (SAXException e)
+ {
+ throw new JBossXBRuntimeException("Parsing failed.", e);
+ }
+ catch (IOException e)
+ {
+ throw new JBossXBRuntimeException("Parsing failed.", e);
+ }
+ }
+}
Deleted: trunk/testsuite/src/main/org/jboss/test/xml/Validator.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/xml/Validator.java 2006-11-20 11:32:11 UTC (rev 58641)
+++ trunk/testsuite/src/main/org/jboss/test/xml/Validator.java 2006-11-20 17:12:24 UTC (rev 58642)
@@ -1,120 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.test.xml;
-
-import java.io.ByteArrayInputStream;
-import java.io.StringReader;
-import javax.xml.parsers.SAXParserFactory;
-import javax.xml.parsers.SAXParser;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.InputSource;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.helpers.DefaultHandler;
-import org.jboss.xb.binding.JBossXBRuntimeException;
-
-/**
- * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
- * @version <tt>$Revision$</tt>
- */
-public class Validator
-{
- public static void assertValidXml(final String xsd, String xml)
- {
- assertValidXml(xsd, xml, null);
- }
-
- public static void assertValidXml(String xml, final EntityResolver resolver)
- {
- assertValidXml(null, xml, resolver);
- }
-
- private static void assertValidXml(final String xsd, String xml, final EntityResolver resolver)
- {
- SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setValidating(true);
- factory.setNamespaceAware(true);
- SAXParser parser = null;
- try
- {
- parser = factory.newSAXParser();
- }
- catch(Exception e)
- {
- throw new IllegalStateException("Failed to instantiate a SAX parser: " + e.getMessage());
- }
-
- try
- {
- parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
- }
- catch(SAXException e)
- {
- throw new IllegalStateException("Schema validation feature is not supported by the parser: " + e.getMessage());
- }
-
- try
- {
- parser.parse(new ByteArrayInputStream(xml.getBytes()),
- new DefaultHandler()
- {
- public void warning(SAXParseException e)
- {
- }
-
- public void error(SAXParseException e)
- {
- throw new JBossXBRuntimeException("Error", e);
- }
-
- public void fatalError(SAXParseException e)
- {
- throw new JBossXBRuntimeException("Fatal error", e);
- }
-
- public InputSource resolveEntity(String publicId, String systemId)
- {
- if(resolver != null)
- {
- try
- {
- return resolver.resolveEntity(publicId, systemId);
- }
- catch(Exception e)
- {
- throw new IllegalStateException("Failed to resolveEntity " + systemId + ": " + systemId);
- }
- }
- else
- {
- return new InputSource(new StringReader(xsd));
- }
- }
- }
- );
- }
- catch(Exception e)
- {
- throw new JBossXBRuntimeException("Parsing failed.", e);
- }
- }
-}
More information about the jboss-cvs-commits
mailing list