[jboss-cvs] JBossAS SVN: r82188 - in projects/metadata/trunk/src/test/java/org/jboss/test/metadata: xml and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Dec 10 09:02:03 EST 2008


Author: alex.loubyansky at jboss.com
Date: 2008-12-10 09:02:02 -0500 (Wed, 10 Dec 2008)
New Revision: 82188

Added:
   projects/metadata/trunk/src/test/java/org/jboss/test/metadata/xml/
   projects/metadata/trunk/src/test/java/org/jboss/test/metadata/xml/XmlValidationUnitTestCase.java
Log:
xml resources validation testcase

Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/xml/XmlValidationUnitTestCase.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/xml/XmlValidationUnitTestCase.java	                        (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/xml/XmlValidationUnitTestCase.java	2008-12-10 14:02:02 UTC (rev 82188)
@@ -0,0 +1,190 @@
+/*
+  * 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.metadata.xml;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.jboss.util.xml.JBossEntityResolver;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+import junit.framework.TestCase;
+
+/**
+ * A XmlValidationUnitTestCase.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class XmlValidationUnitTestCase extends TestCase
+{
+   private static final SAXParserFactory FACTORY;
+   private static final Set<String> IGNORE = new HashSet<String>();
+   
+   static
+   {
+      FACTORY = SAXParserFactory.newInstance();
+      FACTORY.setNamespaceAware(true);
+      FACTORY.setValidating(true);
+      
+      IGNORE.add("log4j.xml");
+   }
+   
+   private static final EntityResolver RESOLVER = new JBossEntityResolver();
+   
+   private int total;
+   private int invalid;
+   
+   public void testValidation() throws Exception
+   {
+      String resourcesPath = System.getProperty("user.dir") + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar + "resources";
+      File resourcesDir = new File(resourcesPath);
+      
+      final Set<String> names = new HashSet<String>();
+      
+      resourcesDir.listFiles(new FileFilter()
+      {
+         public boolean accept(File pathname)
+         {
+            if(".svn".equals(pathname.getName()))
+               return false;
+
+            if(pathname.isDirectory())
+               pathname.listFiles(this);
+            
+            if(pathname.getName().endsWith(".xml") && !IGNORE.contains(pathname.getName()))
+            {
+               names.add(pathname.getName());
+               try
+               {
+                  validate(pathname);
+               }
+               catch(SAXException e)
+               {
+                  ++invalid;
+               }
+               ++total;
+            }
+
+            return false;
+         }
+      });
+   
+      assertEquals("Zero invalid files among total of " + total, 0, invalid);
+   }
+   
+   private static boolean validate(File xmlFile) throws SAXException
+   {
+      SAXParser parser;      
+      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());
+      }
+
+      
+      InputStream is;
+      try
+      {
+         is = xmlFile.toURL().openStream();
+      }
+      catch (Exception e)
+      {
+         throw new IllegalStateException("Failed to open file: " + xmlFile.getAbsolutePath(), e);
+      }
+
+      try
+      {
+         parser.parse(is, new DefaultHandler()
+         {
+            public void warning(SAXParseException e)
+            {
+            }
+
+            public void error(SAXParseException e) throws SAXException
+            {
+               throw e;
+            }
+
+            public void fatalError(SAXParseException e) throws SAXException
+            {
+               throw e;
+            }
+
+            public InputSource resolveEntity(String publicId, String systemId) throws SAXException
+            {
+               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)
+               {
+                  throw new SAXException("Failed to resolve entity: publicId=" + publicId + " systemId=" + systemId);
+               }
+
+               return is;
+            }
+         });
+      }
+      catch (IOException e)
+      {
+         throw new IllegalStateException("Failed to read file: " + xmlFile.getAbsolutePath(), e);
+      }
+
+      return true;
+   }
+}




More information about the jboss-cvs-commits mailing list