servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r430194 - in /incubator/servicemix/trunk/servicemix-components/src: main/java/org/apache/servicemix/components/validation/ test/java/org/apache/servicemix/components/validation/ test/resources/org/apache/servicemix/components/validation/
Date Thu, 10 Aug 2006 00:20:48 GMT
Author: gnodet
Date: Wed Aug  9 17:20:48 2006
New Revision: 430194

URL: http://svn.apache.org/viewvc?rev=430194&view=rev
Log:
SM-525: Enhancement to ValidateComponent to implement a complete and flexible error handling
scheme for schema validation
Patch provided by Grant McDonald

Added:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAggregatingErrorHandler.java
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAwareErrorHandler.java
Modified:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/CountingErrorHandler.java
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/ValidateComponent.java
    incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/validation/ValidationTest.java
    incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/validation/example.xml

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/CountingErrorHandler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/CountingErrorHandler.java?rev=430194&r1=430193&r2=430194&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/CountingErrorHandler.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/CountingErrorHandler.java
Wed Aug  9 17:20:48 2006
@@ -16,6 +16,8 @@
  */
 package org.apache.servicemix.components.validation;
 
+import javax.jbi.messaging.MessagingException;
+
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.SAXException;
@@ -25,37 +27,79 @@
  *
  * @version $Revision$
  */
-public class CountingErrorHandler implements ErrorHandler {
+public class CountingErrorHandler implements MessageAwareErrorHandler {
     private int warningCount;
     private int errorCount;
     private int fatalErrorCount;
 
-
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#hasErrors()
+     */
     public boolean hasErrors() {
         return getErrorCount() > 0 || getFatalErrorCount() > 0;
     }
 
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getWarningCount()
+     */
     public int getWarningCount() {
         return warningCount;
     }
 
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getErrorCount()
+     */
     public int getErrorCount() {
         return errorCount;
     }
 
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getFatalErrorCount()
+     */
     public int getFatalErrorCount() {
         return fatalErrorCount;
     }
 
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
+     */
     public void warning(SAXParseException e) throws SAXException {
         ++warningCount;
     }
 
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
+     */
     public void error(SAXParseException e) throws SAXException {
         ++errorCount;
     }
 
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
+     */
     public void fatalError(SAXParseException e) throws SAXException {
         ++fatalErrorCount;
     }
+    
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#capturesMessages()
+     */
+    public boolean capturesMessages() {
+    	return false;
+    }
+
+	/* (non-Javadoc)
+	 * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getMessagesAs(java.lang.Class)
+	 */
+	public Object getMessagesAs(Class format) throws MessagingException {
+		throw new MessagingException("Unsupported message format: " + format.getName());
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#supportsMessageFormat(java.lang.Class)
+	 */
+	public boolean supportsMessageFormat(Class format) {
+		return false;
+	}
+    
 }

Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAggregatingErrorHandler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAggregatingErrorHandler.java?rev=430194&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAggregatingErrorHandler.java
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAggregatingErrorHandler.java
Wed Aug  9 17:20:48 2006
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.components.validation;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.jbi.messaging.MessagingException;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.SAXException;
+
+/**
+ * An implementation of {@link ErrorHandler} which aggregates all warnings and
+ * error messages into a StringBuffer.
+ *
+ * @version $Revision: 359186 $
+ */
+public class MessageAggregatingErrorHandler implements MessageAwareErrorHandler {
+    
+	private static final String openCDATA = "<![CDATA[";
+	private static final String closeCDATA = "]]>";
+	private static final String openError = "<error>";
+	private static final String closeError = "</error>";
+	private static final String openFatalError = "<fatalError>";
+	private static final String closeFatalError = "</fataError>";
+	private static final String openWarning = "<warning>";
+	private static final String closeWarning = "</warning>";
+	
+	private String openRootElement;
+	private String closeRootElement;
+
+	/**
+     * Number of warnings.
+     */
+	private int warningCount;
+	
+	/**
+	 * Number of errors.
+	 */
+    private int errorCount;
+    
+    /**
+     * Number of fatal errors.
+     */
+    private int fatalErrorCount;
+    
+    /**
+     * The root element name for the fault xml message
+     */
+    private String rootPath;
+    
+    /**
+     * The namespace for the fault xml message
+     */
+    private String namespace;
+    
+    /**
+     * Determines whether or not to include stacktraces in the fault xml message
+     */
+    private boolean includeStackTraces;
+    
+    /**
+     * Variable to hold the warning/error messages from the validator
+     */
+    private StringBuffer messages = new StringBuffer();
+    
+    private SourceTransformer sourceTransformer = new SourceTransformer();
+    
+	/**
+     * Constructor.
+     * 
+     * @param rootElement
+     * 		The root element name of the fault xml message 
+     * @param namespace
+     * 		The namespace for the fault xml message
+     */
+    public MessageAggregatingErrorHandler(String rootPath, String namespace) throws IllegalArgumentException
{
+    	if (rootPath == null || rootPath.trim().length() == 0) {
+    		throw new IllegalArgumentException("rootPath must not be null or an empty string");
+    	}
+    	this.rootPath = rootPath;
+    	this.namespace = namespace;
+    	createRootElementTags();
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param rootElement
+     * 		The root element name of the fault xml message 
+     * @param namespace
+     * 		The namespace for the fault xml message
+     * @param includeStackTraces
+     * 		Include stracktraces in the final output
+     */
+    public MessageAggregatingErrorHandler(String rootPath, String namespace, boolean includeStackTraces)
throws IllegalArgumentException {
+    	this(rootPath, namespace);
+    	this.includeStackTraces = includeStackTraces;
+    }
+
+    /**
+	 * @return Returns the sourceTransformer.
+	 */
+	public SourceTransformer getSourceTransformer() {
+		return sourceTransformer;
+	}
+
+	/**
+	 * @param sourceTransformer The sourceTransformer to set.
+	 */
+	public void setSourceTransformer(SourceTransformer sourceTransformer) {
+		this.sourceTransformer = sourceTransformer;
+	}
+
+	/**
+     * Creates the root element tags for later use down to n depth.
+     * Note: the rootPath here is of the form:
+     * 
+     * 		<code>rootElementName/elementName-1/../elementName-n</code>
+     * 
+     * The namespace will be appended to the root element if it is not
+     * null or empty.
+     */
+	private void createRootElementTags() {
+		/* 
+		 * since the rootPath is constrained to be not null or empty
+		 * then we have at least one path element.
+		 */
+		String[] pathElements = rootPath.split("/");
+
+		StringBuffer openRootElementSB = new StringBuffer().append("<").append(pathElements[0]);
+		StringBuffer closeRootElementSB = new StringBuffer();
+		
+		if (namespace != null && namespace.trim().length() > 0) {
+			openRootElementSB.append(" xmlns=\"").append(namespace).append("\">"); 
+		} else {
+			openRootElementSB.append(">");
+		}
+		
+		if (pathElements.length > 0) {
+			for (int i = 1, j = pathElements.length - 1; i < pathElements.length; i++, j--) {
+				openRootElementSB.append("<").append(pathElements[i]).append(">");
+				closeRootElementSB.append("</").append(pathElements[j]).append(">");
+			}
+		}
+    	
+    	// create the closing root element tag
+    	closeRootElementSB.append("</").append(pathElements[0]).append(">");
+    	
+    	openRootElement = openRootElementSB.toString();
+    	closeRootElement = closeRootElementSB.toString();
+    }
+    
+	/*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#hasErrors()
+     */
+    public boolean hasErrors() {
+        return getErrorCount() > 0 || getFatalErrorCount() > 0;
+    }
+
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getWarningCount()
+     */
+    public int getWarningCount() {
+        return warningCount;
+    }
+
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getErrorCount()
+     */
+    public int getErrorCount() {
+        return errorCount;
+    }
+
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getFatalErrorCount()
+     */
+    public int getFatalErrorCount() {
+        return fatalErrorCount;
+    }
+
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
+     */
+    public void warning(SAXParseException e) throws SAXException {
+        ++warningCount;
+
+        // open warning and CDATA tags
+        messages.append(openWarning).append(openCDATA);
+    	
+        // append the fatal error message
+        appendErrorMessage(e);
+        
+        // close CDATA and warning tags
+        messages.append(closeCDATA).append(closeWarning);
+    }
+
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
+     */
+    public void error(SAXParseException e) throws SAXException {
+        ++errorCount;
+
+        // open fatal error and CDATA tags
+        messages.append(openError).append(openCDATA);
+    	
+        // append the error message
+        appendErrorMessage(e);
+        
+        // close CDATA and error tags
+        messages.append(closeCDATA).append(closeError);
+    }
+
+    /*  (non-Javadoc)
+     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
+     */
+    public void fatalError(SAXParseException e) throws SAXException {
+        ++fatalErrorCount;
+    	
+        // open fatal error and CDATA tags
+        messages.append(openFatalError).append(openCDATA);
+    	
+        // append the fatal error message
+        appendErrorMessage(e);
+        
+        // close CDATA and fatal error tags
+        messages.append(closeCDATA).append(closeFatalError);
+    }
+
+    /**
+     * Append the error message or stacktrace to the messages attribute.
+     * 
+     * @param e
+     */
+    private void appendErrorMessage(SAXParseException e) {
+        if (includeStackTraces) {
+            StringWriter sw = new StringWriter();
+            e.printStackTrace(new PrintWriter(sw));
+            messages.append(sw.toString());
+        } else {
+        	messages.append(e.getLocalizedMessage());
+        }
+    }
+    
+    /*  (non-Javadoc)
+     * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#capturesMessages()
+     */
+    public boolean capturesMessages() {
+    	return true;
+    }
+
+	/* (non-Javadoc)
+	 * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#getMessagesAs(java.lang.Class)
+	 */
+	public Object getMessagesAs(Class format) throws MessagingException {
+		if (format == DOMSource.class) {
+			return getDOMSource();
+		} else if (format == StringSource.class) {
+			return getStringSource();
+		} else if (format == String.class) {
+			return getMessagesWithRootElement();
+		}
+		throw new MessagingException("Unsupported message format: " + format.getName());
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.servicemix.components.validation.MessageAwareErrorHandler#supportsMessageFormat(java.lang.Class)
+	 */
+	public boolean supportsMessageFormat(Class format) {
+		if (format == DOMSource.class) {
+			return true;
+		} else if (format == StringSource.class) {
+			return true;
+		} else if (format == String.class) {
+			return true;
+		}
+		return false;
+	}
+    
+	/**
+	 * Return the messages encapsulated with the root element.
+	 * 
+	 * @return
+	 */
+	private String getMessagesWithRootElement() {
+		return new StringBuffer().append(openRootElement).append(messages).append(closeRootElement).toString();
+	}
+	
+	/**
+	 * Get the error messages as a String Source.
+	 * 
+	 * @return
+	 */
+	private StringSource getStringSource() {
+		return new StringSource(getMessagesWithRootElement());
+	}
+	
+	/**
+	 * Get the error messages as a DOMSource.
+	 * 
+	 * @return
+	 * @throws MessagingException
+	 */
+	private DOMSource getDOMSource() throws MessagingException {
+		try {
+			return sourceTransformer.toDOMSource(getStringSource());
+		} catch (ParserConfigurationException e) {
+			throw new MessagingException("Failed to create DOMSource for Schema validation messages:
" + e, e);
+		} catch (IOException e) {
+			throw new MessagingException("Failed to create DOMSource for Schema validation messages:
" + e, e);
+		} catch (SAXException e) {
+			throw new MessagingException("Failed to create DOMSource for Schema validation messages:
" + e, e);
+		} catch (TransformerException e) {
+			throw new MessagingException("Failed to create DOMSource for Schema validation messages:
" + e, e);
+		}
+	}
+    
+}

Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAwareErrorHandler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAwareErrorHandler.java?rev=430194&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAwareErrorHandler.java
(added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/MessageAwareErrorHandler.java
Wed Aug  9 17:20:48 2006
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.components.validation;
+
+import javax.jbi.messaging.MessagingException;
+
+import org.xml.sax.ErrorHandler;
+
+public interface MessageAwareErrorHandler extends ErrorHandler {
+	
+    /**
+     * Does the error handler contain any error messages?
+     * 
+     * @return
+     */
+	public boolean hasErrors();
+
+    /**
+     * Return the number of warnings.
+     * 
+     * @return
+     */
+	public int getWarningCount();
+
+    /**
+     * Return the number of errors.
+     * 
+     * @return
+     */
+	public int getErrorCount();
+
+    /**
+     * Return the number of fatal errors.
+     * 
+     * @return
+     */
+    public int getFatalErrorCount();
+
+	/**
+	 * Does this ErrorHandler implementation capture error messages?
+	 * 
+	 * @return
+	 */
+	public boolean capturesMessages();
+	
+	/**
+	 * Get the messages from this ErrorHandler in a particular format.
+	 * 
+	 * @return
+	 */
+	public Object getMessagesAs(Class format) throws MessagingException;
+	
+	/**
+	 * Does the message support a given format?
+	 * 
+	 * @param clazz
+	 * 		The Class of the format for which support is required.
+	 * @return
+	 */
+	public boolean supportsMessageFormat(Class format);
+	
+}

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/ValidateComponent.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/ValidateComponent.java?rev=430194&r1=430193&r2=430194&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/ValidateComponent.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/validation/ValidateComponent.java
Wed Aug  9 17:20:48 2006
@@ -19,6 +19,7 @@
 import org.apache.servicemix.components.util.TransformComponentSupport;
 import org.apache.servicemix.jbi.FaultException;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.springframework.core.io.Resource;
 import org.xml.sax.SAXException;
 
@@ -50,6 +51,7 @@
     private String schemaLanguage = "http://www.w3.org/2001/XMLSchema";
     private Source schemaSource;
     private Resource schemaResource;
+    private MessageAwareErrorHandler errorHandler = new CountingErrorHandler();
 
     public Schema getSchema() {
         return schema;
@@ -83,6 +85,14 @@
         this.schemaResource = schemaResource;
     }
    
+    public MessageAwareErrorHandler getErrorHandler() {
+		return errorHandler;
+	}
+
+	public void setErrorHandler(MessageAwareErrorHandler errorHandler) {
+		this.errorHandler = errorHandler;
+	}
+
     protected void init() throws JBIException {
         super.init();
 
@@ -94,8 +104,11 @@
                     if (schemaResource == null) {
                         throw new JBIException("You must specify a schema, schemaSource or
schemaResource property");
                     }
-                    schemaSource = new StreamSource(schemaResource.getInputStream(),
-                                                    schemaResource.getURL().toExternalForm());
+                    if (schemaResource.getURL() == null) {
+                    	schemaSource = new StreamSource(schemaResource.getInputStream());
+                    } else {
+                    	schemaSource = new StreamSource(schemaResource.getInputStream(), schemaResource.getURL().toExternalForm());

+                    }
                 }
                 schema = factory.newSchema(schemaSource);
             }
@@ -111,23 +124,60 @@
     protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage
out) throws MessagingException {
         Validator validator = schema.newValidator();
 
-        CountingErrorHandler errorHandler = new CountingErrorHandler();
         validator.setErrorHandler(errorHandler);
         DOMResult result = new DOMResult();
         // Transform first so that the input source will be parsed only once
         // if it is a StreamSource
         getMessageTransformer().transform(exchange, in, out);
         try {
+        	SourceTransformer sourceTransformer = new SourceTransformer();
         	// Only DOMSource and SAXSource are allowed for validating
         	// See http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/Validator.html#validate(javax.xml.transform.Source,%20javax.xml.transform.Result)
         	// As we expect a DOMResult as output, we must ensure that the input is a 
         	// DOMSource
-        	DOMSource src = new SourceTransformer().toDOMSource(out.getContent());
+        	DOMSource src = sourceTransformer.toDOMSource(out.getContent());
         	doValidation(validator,src,result);
             if (errorHandler.hasErrors()) {
                 Fault fault = exchange.createFault();
+                
+                // set the schema and source document as properties on the fault
                 fault.setProperty("org.apache.servicemix.schema", schema);
-                fault.setContent(new DOMSource(result.getNode(), result.getSystemId()));
+                fault.setProperty("org.apache.servicemix.xml", src);
+                
+                /* 
+                 * check if this error handler supports the capturing of
+                 * error messages.
+                 */
+                if (errorHandler.capturesMessages()) {
+
+                	/* 
+                	 * In descending order of preference select a format to use. If
+                	 * neither DOMSource, StringSource or String are supported throw
+                	 * a messaging exception.
+                	 */
+                	if (errorHandler.supportsMessageFormat(DOMSource.class)) {
+                		fault.setContent(
+                				(DOMSource)errorHandler.getMessagesAs(DOMSource.class));
+                	} else if (errorHandler.supportsMessageFormat(StringSource.class)) {
+                		fault.setContent(sourceTransformer.toDOMSource(
+                				(StringSource)errorHandler.getMessagesAs(StringSource.class)));
+                	} else if (errorHandler.supportsMessageFormat(String.class)) {
+                		fault.setContent(
+                				sourceTransformer.toDOMSource(
+                						new StringSource(
+                								(String)errorHandler.getMessagesAs(String.class))));
+                	} else {
+                		throw new MessagingException("MessageAwareErrorHandler implementation "
+ 
+                				errorHandler.getClass().getName() +
+                				" does not support a compatible error message format.");
+                	}
+                } else {
+                	/* 
+                	 * we can't do much here if the ErrorHandler implementation does
+                	 * not support capturing messages
+                	 */
+                    fault.setContent(new DOMSource(result.getNode(), result.getSystemId()));
+                }
                 throw new FaultException("Failed to validate against schema: " + schema,
exchange, fault);
             }
             else {

Modified: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/validation/ValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/validation/ValidationTest.java?rev=430194&r1=430193&r2=430194&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/validation/ValidationTest.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/validation/ValidationTest.java
Wed Aug  9 17:20:48 2006
@@ -20,16 +20,29 @@
 import javax.jbi.messaging.InOut;
 import javax.jbi.messaging.NormalizedMessage;
 
-import org.apache.servicemix.tck.TestSupport;
-import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.jbi.container.SpringJBIContainer;
+import org.apache.servicemix.tck.Receiver;
+import org.apache.servicemix.tck.SpringTestSupport;
 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
 
 /**
  * @version $Revision$
  */
-public class ValidationTest extends TestSupport {
+public class ValidationTest extends SpringTestSupport {
+    protected ServiceMixClient client;
+    protected Receiver receiver;
+
+	protected void setUp() throws Exception {
+		super.setUp();
+        SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
+        receiver = (Receiver) jbi.getBean("receiver");
+    }
 
     public void testValidMessage() throws Exception {
+    	client = (ServiceMixClient) getBean("defaultErrorHandlerClient");
+    	
         InOut exchange = client.createInOutExchange();
         exchange.getInMessage().setContent(getSourceFromClassPath("requestValid.xml"));
         client.sendSync(exchange);
@@ -45,7 +58,32 @@
     }
 
     public void testInvalidMessage() throws Exception {
-        InOut exchange = client.createInOutExchange();
+    	client = (ServiceMixClient) getBean("defaultErrorHandlerClient");
+
+    	InOut exchange = client.createInOutExchange();
+        exchange.getInMessage().setContent(getSourceFromClassPath("requestInvalid.xml"));
+        client.sendSync(exchange);
+
+        NormalizedMessage out = exchange.getOutMessage();
+        Fault fault = exchange.getFault();
+        Exception error = exchange.getError();
+
+        assertEquals("out", null, out);
+        assertNotNull("Should have a fault", fault);
+
+        System.out.println("error is: " + error);
+
+        System.out.println("Fault is...");
+        System.out.println(transformer.toString(fault.getContent()));
+
+        // TODO?
+        //assertEquals("error", null, error);
+    }
+
+    public void testInvalidMessageWithMessageAwareErrorHandler() throws Exception {
+    	client = (ServiceMixClient) getBean("messageAwareErrorHandlerClient");
+    	
+    	InOut exchange = client.createInOutExchange();
         exchange.getInMessage().setContent(getSourceFromClassPath("requestInvalid.xml"));
         client.sendSync(exchange);
 

Modified: incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/validation/example.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/validation/example.xml?rev=430194&r1=430193&r2=430194&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/validation/example.xml
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/test/resources/org/apache/servicemix/components/validation/example.xml
Wed Aug  9 17:20:48 2006
@@ -7,11 +7,21 @@
     <sm:activationSpecs>
 
       <!-- START SNIPPET: example -->
-      <sm:activationSpec componentName="myService" service="foo:myService" endpoint="myService"
destinationService="foo:myServiceImpl">
+      <sm:activationSpec componentName="myDefaultErrorHandlerService" service="foo:myDefaultErrorHandlerService"
endpoint="myDefaultErrorHandlerService" destinationService="foo:myServiceImpl">
         <sm:component><bean class="org.apache.servicemix.components.validation.ValidateComponent">
           <property name="schemaResource" value="classpath:org/apache/servicemix/components/validation/schema.xsd"/>
         </bean></sm:component>
       </sm:activationSpec>
+
+      <sm:activationSpec componentName="myMessageAwareErrorHandlerService" service="foo:myMessageAwareErrorHandlerService"
endpoint="myMessageAwareErrorHandlerService" destinationService="foo:myServiceImpl">
+        <sm:component>
+        	<bean class="org.apache.servicemix.components.validation.ValidateComponent">
+          		<property name="schemaResource" value="classpath:org/apache/servicemix/components/validation/schema.xsd"/>
+          		<property name="errorHandler" ref="messageAggregatingErrorHandler"/>
+           	</bean>
+        </sm:component>
+      </sm:activationSpec>
+
       <!-- END SNIPPET: example -->
 
       <!-- START SNIPPET: groovy -->
@@ -40,12 +50,25 @@
     </sm:activationSpecs>
   </sm:container>
 
+  <bean id="messageAggregatingErrorHandler" class="org.apache.servicemix.components.validation.MessageAggregatingErrorHandler">
+    <constructor-arg index="0" value="Fault/payload/messages"/>
+    <constructor-arg index="1" value="http://www.servicemix.org/fault"/>
+  </bean>
 
-  <bean id="client" class="org.apache.servicemix.client.DefaultServiceMixClient">
+  <bean id="defaultErrorHandlerClient" class="org.apache.servicemix.client.DefaultServiceMixClient">
     <constructor-arg ref="jbi"/>
     <constructor-arg>
       <!-- lets hardwire this client to talk to instances of a service by default -->
-      <sm:activationSpec destinationService="foo:myService"/>
+      <sm:activationSpec destinationService="foo:myDefaultErrorHandlerService"/>
     </constructor-arg>
   </bean>
+
+  <bean id="messageAwareErrorHandlerClient" class="org.apache.servicemix.client.DefaultServiceMixClient">
+    <constructor-arg ref="jbi"/>
+    <constructor-arg>
+      <!-- lets hardwire this client to talk to instances of a service by default -->
+      <sm:activationSpec destinationService="foo:myMessageAwareErrorHandlerService"/>
+    </constructor-arg>
+  </bean>
+
 </beans>



Mime
View raw message