You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/08/09 19:33:26 UTC

svn commit: r684301 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/processor/validation/ test/java/org/apache/camel/processor/ test/resources/org/apache/camel/model/ test/resources/org/apache/camel/processor/

Author: davsclaus
Date: Sat Aug  9 10:33:25 2008
New Revision: 684301

URL: http://svn.apache.org/viewvc?rev=684301&view=rev
Log:
CAMEL-803: ValidatingProcessor was not thread safe. Added unit test to test this class. And removed a not needed file (0 size)

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java   (contents, props changed)
      - copied, changed from r684215, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidationTest.java
    activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/
    activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd   (with props)
Removed:
    activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/model/setHeaderWithChildProcessor.xml
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java?rev=684301&r1=684300&r2=684301&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java Sat Aug  9 10:33:25 2008
@@ -24,7 +24,6 @@
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
-
 import org.apache.camel.Exchange;
 import org.apache.camel.ValidationException;
 import org.apache.commons.logging.Log;

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java?rev=684301&r1=684300&r2=684301&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java Sat Aug  9 10:33:25 2008
@@ -40,15 +40,14 @@
  * @version $Revision$
  */
 public class ValidatingProcessor implements Processor {
-    private Schema schema;
-    private ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
-
     // for lazy creation of the Schema
     private String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
+    private Schema schema;
     private Source schemaSource;
     private SchemaFactory schemaFactory;
     private URL schemaUrl;
     private File schemaFile;
+    private ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
 
     public void process(Exchange exchange) throws Exception {
         Schema schema = getSchema();
@@ -60,54 +59,14 @@
         }
 
         // create a new errorHandler and set it on the validator
-        errorHandler.reset();
-        validator.setErrorHandler(errorHandler);
+        // must be a local instance to avoid problems with concurrency (to be thread safe)
+        ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
+        validator.setErrorHandler(handler);
 
         DOMResult result = new DOMResult();
         validator.validate(source, result);
 
-        errorHandler.handleErrors(exchange, schema, result);
-        /*
-         * Fault fault = exchange.createFault(); if (errorHandler.hasErrors()) { //
-         * set the schema and source document as properties on the fault
-         * fault.setProperty("org.apache.servicemix.schema", schema);
-         * fault.setProperty("org.apache.servicemix.xml", source);
-         * 
-         *//*
-             * 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 { //
-             * Retrieve the ouput of the validation // as it may have been
-             * changed by the validator out.setContent(new
-             * DOMSource(result.getNode(), result.getSystemId())); } }
-             */
+        handler.handleErrors(exchange, schema, result);
     }
 
     // Properties

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java (from r684215, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidationTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidationTest.java&r1=684215&r2=684301&rev=684301&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidationTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java Sat Aug  9 10:33:25 2008
@@ -16,78 +16,67 @@
  */
 package org.apache.camel.processor;
 
+import java.io.File;
+
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Processor;
 import org.apache.camel.ValidationException;
-import org.apache.camel.builder.Builder;
-import org.apache.camel.builder.ProcessorBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.validation.ValidatingProcessor;
 
 /**
- * @version $Revision$
+ * Unit test of ValidatingProcessor.
  */
-public class ValidationTest extends ContextTestSupport {
-    protected Processor validator = new MyValidator();
-    protected MockEndpoint validEndpoint;
-    protected MockEndpoint invalidEndpoint;
+public class ValidatingProcessorTest extends ContextTestSupport {
 
-    public void testValidMessage() throws Exception {
-        validEndpoint.expectedMessageCount(1);
-        invalidEndpoint.expectedMessageCount(0);
+    private ValidatingProcessor validating;
 
-        Object result = template.sendBodyAndHeader("direct:start", "<valid/>", "foo", "bar");
+    @Override
+    protected void setUp() throws Exception {
+        validating = new ValidatingProcessor();
+        validating.setSchemaFile(new File("src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd").getAbsoluteFile());
 
-        MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint);
-        assertEquals("validResult", result);
+        super.setUp();
     }
 
-    public void testInvalidMessage() throws Exception {
-        invalidEndpoint.expectedMessageCount(1);
-        validEndpoint.expectedMessageCount(0);
+    public void testValidMessage() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:valid");
+        mock.expectedMessageCount(1);
 
-        Object result = template.sendBodyAndHeader("direct:start", "<invalid/>", "foo", "notMatchedHeaderValue");
+        String xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" +
+                "<user xmlns=\"http://foo.com/bar\">" +
+                "  <id>1</id>" +
+                "  <username>davsclaus</username>" +
+                "</user>";
 
-        MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint);
-        assertEquals("invalidResult", result);
+        template.sendBody("direct:start", xml);
+
+        assertMockEndpointsSatisifed();
     }
 
-    public void testinvalidThenValidMessage() throws Exception {
-        validEndpoint.expectedMessageCount(2);
-        invalidEndpoint.expectedMessageCount(1);
-
-        Object result;
-        
-        result = template.sendBodyAndHeader("direct:start", "<invalid/>", "foo",  "notMatchedHeaderValue");
-        assertEquals("invalidResult", result);
-        result = template.sendBodyAndHeader("direct:start", "<valid/>", "foo",   "bar");
-        assertEquals("validResult", result);
-        result = template.sendBodyAndHeader("direct:start", "<valid/>", "foo",   "bar");
-        assertEquals("validResult", result);
+    public void testInvalidMessage() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:invalid");
+        mock.expectedMessageCount(1);
 
-        MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint);
-    }
+        String xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" +
+                "<user xmlns=\"http://foo.com/bar\">" +
+                "  <username>someone</username>" +
+                "</user>";
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
+        template.sendBody("direct:start", xml);
 
-        validEndpoint = resolveMandatoryEndpoint("mock:valid", MockEndpoint.class);
-        invalidEndpoint = resolveMandatoryEndpoint("mock:invalid", MockEndpoint.class);
-        
-        validEndpoint.whenAnyExchangeReceived(ProcessorBuilder.setOutBody(Builder.constant("validResult")));
-        invalidEndpoint.whenAnyExchangeReceived(ProcessorBuilder.setOutBody(Builder.constant("invalidResult")));
+        assertMockEndpointsSatisifed();
     }
 
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
+                exception(ValidationException.class).to("mock:invalid");
+
                 from("direct:start").
-                        tryBlock().
-                        process(validator).
-                        to("mock:valid").
-                        handle(ValidationException.class).to("mock:invalid");
+                    process(validating).
+                    to("mock:valid");
             }
         };
     }
-}
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidatingProcessorTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd?rev=684301&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd (added)
+++ activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd Sat Aug  9 10:33:25 2008
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar"
+           xmlns:tns="http://foo.com/bar"
+           xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="id" type="xs:int"/>
+        <xs:element name="username" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Propchange: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd
------------------------------------------------------------------------------
    svn:mime-type = text/xml