[jboss-svn-commits] JBL Code SVN: r8389 - in labs/jbossesb/trunk/product/core/listeners: src/org/jboss/soa/esb/listeners/config tests/src/org/jboss/soa/esb/listeners/config
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 18 15:50:26 EST 2006
Author: kurt.stam at jboss.com
Date: 2006-12-18 15:50:24 -0500 (Mon, 18 Dec 2006)
New Revision: 8389
Modified:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidatorImpl.java
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/listeners/config/ConfigurationControlerUnitTest.java
Log:
fixing validation
Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidatorImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidatorImpl.java 2006-12-18 19:33:10 UTC (rev 8388)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/config/XmlValidatorImpl.java 2006-12-18 20:50:24 UTC (rev 8389)
@@ -1,24 +1,24 @@
/*
-* JBoss, Home of Professional Open Source
-* Copyright 2006, 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.
-*/
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, 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.soa.esb.listeners.config;
import java.io.File;
@@ -26,25 +26,20 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.io.StringReader;
import java.util.Collection;
import java.util.HashSet;
+import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
-import javax.xml.validation.Validator;
-import javax.xml.XMLConstants;
import org.apache.log4j.Logger;
-
import org.w3c.dom.Document;
-
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -52,61 +47,64 @@
/**
* A Helper classes used to validate xml files against supplied schemas.
- *
- * @author $Revision$
- * $Id$
+ *
+ * @author $Revision$ $Id$
*/
@SuppressWarnings("unchecked")
public class XmlValidatorImpl implements XmlValidator {
- /**
- * Our Logger
- */
- private Logger log = Logger.getLogger(this.getClass());
-
- @SuppressWarnings("unused")
+ /**
+ * Our Logger
+ */
+ private Logger log = Logger.getLogger(this.getClass());
+
+ @SuppressWarnings("unused")
private InputSource xmlSource, validationSource;
- private String defaultSchema = "etc/schemas/xml/jbossesb-1.0.xsd";
- /**
- * The path separator
- */
- @SuppressWarnings("unused")
+ private String defaultSchema = "etc/schemas/xml/jbossesb-1.0.xsd";
+ /**
+ * The path separator
+ */
+ @SuppressWarnings("unused")
private static String SEP = System.getProperty("file.separator");
-
- private boolean isValid = true;
-
- private Collection<String> validationResults = new HashSet();
- private Document xmlDocument;
-
- public XmlValidatorImpl() {
- File defaultSchemaFile = new File (defaultSchema);
- if (defaultSchemaFile.exists()) {
- log.debug("defaultSchema "+defaultSchema+" exists");
- }
- }
-
- public XmlValidatorImpl(InputSource xmlSource) {
+ private boolean isValid = true;
+
+ private Collection<String> validationResults = new HashSet();
+
+ private Document xmlDocument;
+
+ public XmlValidatorImpl() {
+ File defaultSchemaFile = new File (defaultSchema);
+ if (defaultSchemaFile.exists()) {
+ log.debug("defaultSchema "+defaultSchema+" exists");
+ }
+ }
+
+ public XmlValidatorImpl(InputSource xmlSource) {
setXMLDocument(xmlSource);
File defaultSchemaFile = new File (defaultSchema);
- if (defaultSchemaFile.exists()) {
- log.debug("defaultSchema "+defaultSchema+" exists");
- }
- }
-
- /**
- * Validation method used to validate an xml file against a default xsd (jbossesb-1.0.xsd).
- * @param xmlSource The xml input source to be validated.
- * @return Boolean true/false indicating successful validation.
- * @throws XmlValidatorException Failure during validation.
- */
+ if (defaultSchemaFile.exists()) {
+ log.debug("defaultSchema "+defaultSchema+" exists");
+ }
+ }
+
+ /**
+ * Validation method used to validate an xml file against a default xsd
+ * (jbossesb-1.0.xsd).
+ *
+ * @param xmlSource
+ * The xml input source to be validated.
+ * @return Boolean true/false indicating successful validation.
+ * @throws XmlValidatorException
+ * Failure during validation.
+ */
public boolean validate(InputSource xmlSource) throws XmlValidatorException {
StreamSource validationInputSource = null;
-
+
InputStream validationInputStream = this.getClass().getResourceAsStream(defaultSchema);
- //if this fails try using the
+ // if this fails try using the
if (validationInputStream==null) {
File validationFile = new File(defaultSchema);
log.debug("Validation file " + defaultSchema + " exists?:" + validationFile.exists());
@@ -122,121 +120,109 @@
log.debug("Reading validation info from " + defaultSchema);
validationInputSource = new StreamSource(validationInputStream);
}
- return validate(xmlSource,validationInputSource);
- }
+ return validate(xmlSource,validationInputSource);
+ }
- /**
- * Validation method used to validate an xml input source against an xsd input source.
- * @param xmlSource The xml input source to be validated.
- * @param validationSource The schema input source to validate against.
- * @return Boolean true/false indicating successful validation.
- * @throws Exception Failure during validation.
- */
- public boolean validate(InputSource xmlSource, StreamSource validationSource) throws XmlValidatorException {
-
- Schema schema = null;
-
- try {
- SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
- if (validationSource == null) {
- Source schemaFile = new StreamSource(defaultSchema);
+ /**
+ * Validation method used to validate an xml input source against an xsd
+ * input source.
+ *
+ * @param xmlSource
+ * The xml input source to be validated.
+ * @param validationSource
+ * The schema input source to validate against.
+ * @return Boolean true/false indicating successful validation.
+ * @throws Exception
+ * Failure during validation.
+ */
+ public boolean validate(InputSource xmlSource, StreamSource validationSource) throws XmlValidatorException
+ {
try {
- schema = factory.newSchema(schemaFile);
- } catch (SAXException e) {
- log.error("Failed to validate xml",e);
- isValid = false;
- }
- }
- else {
- schema = factory.newSchema(validationSource);
- }
-
- // Request validation
- Validator validator = schema.newValidator();
-
- // Register the error handler
- validator.setErrorHandler(new XmlErrorHandler());
-
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
-// builderFactory.setValidating(true);
-// builderFactory.setNamespaceAware(true);
- DocumentBuilder parser = null;
-
- try {
- parser = builderFactory.newDocumentBuilder();
-// parser.setErrorHandler(new XmlErrorHandler());
- } catch (ParserConfigurationException e) {
- log.error("ParserConfigurationException occurred: " + e.getMessage());
- isValid = false;
- }
-
- try {
- xmlDocument = parser.parse(xmlSource);
- } catch (IOException e) {
- log.error("parser.parse(): IOException occurred: " + e.getMessage());
- log.error(e);
- isValid = false;
- }
-
- try {
- validator.validate(new DOMSource(xmlDocument));
- } catch (IOException e) {
- log.error("validator.validate(): IOException occurred: " + e.getMessage());
- log.error(e);
- isValid = false;
- }
- } catch (SAXException e) {
- log.error("SAXException occurred: " + e.getMessage());
- isValid = false;
- }
+ Schema schema = null;
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ if (validationSource == null) {
+ Source schemaFile = new StreamSource(defaultSchema);
+ try {
+ schema = factory.newSchema(schemaFile);
+ } catch (SAXException e) {
+ System.out.println("Failed to validate xml");
+ isValid = false;
+ }
+ }
+ else {
+ schema = factory.newSchema(validationSource);
+ }
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ builderFactory.setNamespaceAware(true);
+ builderFactory.setSchema(schema);
+ DocumentBuilder parser = null;
+ try {
+ parser = builderFactory.newDocumentBuilder();
+ parser.setErrorHandler(new XmlErrorHandler());
+ } catch (ParserConfigurationException e) {
+ log.error("ParserConfigurationException occurred: " + e.getMessage(),e);
+ isValid = false;
+ }
+ try {
+ xmlDocument = parser.parse(xmlSource);
+ } catch (IOException e) {
+ log.error("parser.parse(): IOException occurred: " + e.getMessage(),e);
+ isValid = false;
+ }
+ } catch (SAXException e) {
+ log.error("SAXException occurred: " + e.getMessage());
+ isValid = false;
+ }
+ return isValid;
+ }
- return isValid;
- }
+ class XmlErrorHandler implements ErrorHandler
+ {
+ public void warning(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("WARNING-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ log.warn("WARNING-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ }
- class XmlErrorHandler implements ErrorHandler {
- public void warning(SAXParseException exception) throws SAXException {
- // Bring things to a crashing halt
- validationResults.add("WARNING-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- log.error("WARNING-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- }
-
- public void error(SAXParseException exception) throws SAXException {
- // Bring things to a crashing halt
- validationResults.add("ERROR-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- log.error(validationResults.size()+"-ERROR-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- isValid = false;
- }
+ public void error(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("ERROR-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ log.error(validationResults.size()+"-ERROR-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ isValid = false;
+ }
- public void fatalError(SAXParseException exception) throws SAXException {
- // Bring things to a crashing halt
- validationResults.add("FATAL-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- log.error("FATAL-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
- isValid = false;
- }
- }
+ public void fatalError(SAXParseException exception) throws SAXException {
+ // Bring things to a crashing halt
+ validationResults.add("FATAL-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ log.fatal("FATAL-"+exception.getSystemId()+"-"+exception.getLineNumber()+":"+exception.getMessage());
+ isValid = false;
+ }
+ }
- public Collection<String> getValidationResults() {
+ public Collection<String> getValidationResults()
+ {
return validationResults;
}
- public void setXMLDocument(InputSource xmlSource) {
- try {
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder parser = builderFactory.newDocumentBuilder();
-
- xmlDocument = parser.parse(xmlSource);
- log.info(xmlDocument.toString());
- } catch (SAXException e) {
- System.err.println(e);
- } catch (ParserConfigurationException e) {
- System.err.println(e);
- } catch (IOException e) {
- System.err.println(e);
- }
+ public void setXMLDocument(InputSource xmlSource)
+ {
+ try {
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder parser = builderFactory.newDocumentBuilder();
+
+ xmlDocument = parser.parse(xmlSource);
+ log.info(xmlDocument.toString());
+ } catch (SAXException e) {
+ System.err.println(e);
+ } catch (ParserConfigurationException e) {
+ System.err.println(e);
+ } catch (IOException e) {
+ System.err.println(e);
+ }
}
-
+
public Document getXMLDocument() {
return xmlDocument;
}
-
+
}
Modified: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/listeners/config/ConfigurationControlerUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/listeners/config/ConfigurationControlerUnitTest.java 2006-12-18 19:33:10 UTC (rev 8388)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/listeners/config/ConfigurationControlerUnitTest.java 2006-12-18 20:50:24 UTC (rev 8389)
@@ -21,10 +21,15 @@
*/
package org.jboss.soa.esb.listeners.config;
+import java.io.File;
+
import junit.framework.JUnit4TestAdapter;
+import static org.junit.Assert.assertTrue;
+import org.apache.log4j.Logger;
import org.jboss.soa.esb.testutils.TestEnvironmentUtil;
import org.junit.Test;
+
/**
* Testing configuration controller.
*
@@ -33,23 +38,72 @@
*/
public class ConfigurationControlerUnitTest
{
- //private static Logger logger = Logger.getLogger(ConfigurationControlerUnitTest.class);
+ private static Logger logger = Logger.getLogger(ConfigurationControlerUnitTest.class);
/**
* Testing the Content Based Router.
*/
@Test
- public void readConfig() throws Exception
+ public void readAndGenerateConfig() throws Exception
{
String configFileName = TestEnvironmentUtil.getUserDir("product")
+ "core/listeners/tests/src/org/jboss/soa/esb/listeners/config/jbossesb_config_01.xml";
+ //Make sure this file exists
+ File configFile = new File(configFileName);
+ if (!configFile.exists()){
+ assertTrue(false);
+ }
+ //Remove the listener and gateway configuration files if the exist
+ File currentDir = configFile.getParentFile();
+ File listenerFile = new File(currentDir + File.separator + "jbossesb-listener.xml");
+ if (listenerFile.exists()) {
+ listenerFile.delete();
+ }
+ File gatewayFile = new File(currentDir + File.separator + "jbossesb-gateway.xml");
+ if (gatewayFile.exists()) {
+ gatewayFile.delete();
+ }
String validationFileName = TestEnvironmentUtil.getUserDir("product")
+ "etc/schemas/xml/jbossesb-1.0.xsd";
+// Make sure this file exists
+ File validationFile = new File(validationFileName);
+ if (!validationFile.exists()){
+ assertTrue(false);
+ }
ConfigurationController configurationController = new ConfigurationController(configFileName, validationFileName);
Thread controller = new Thread(configurationController);
controller.start();
- Thread.sleep(4000);
+ int deciSeconds=0;
+ //wait till the file files are there, or timeout after 8 seconds
+ while (true) {
+ if (listenerFile.exists() && (gatewayFile.exists())){
+ break;
+ } else {
+ if (deciSeconds++>80) {
+ break;
+ } else {
+ Thread.sleep(100);
+ }
+ }
+ }
configurationController.requestEnd();
+ for (int i=0; i<currentDir.listFiles().length;i++){
+ logger.info(currentDir.listFiles()[i].getName());
+ }
+ if (listenerFile.exists()) {
+ listenerFile.delete();
+ } else {
+ //Listener file should exist if all was successful
+ System.out.println("file=" + listenerFile.getAbsolutePath() + " does not exist, while it should have.");
+ assertTrue(false);
+ }
+ if (gatewayFile.exists()) {
+ gatewayFile.delete();
+ } else {
+ //Gateway file should exist if all was successful
+ System.out.println("file=" + gatewayFile.getAbsolutePath() + " does not exist, while it should have.");
+ assertTrue(false);
+ }
}
public static junit.framework.Test suite() {
More information about the jboss-svn-commits
mailing list