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/11/20 18:59:12 UTC

svn commit: r719309 - in /activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception: ./ MyFunctionalException.java MyOwnHandlerBean.java MyServiceBean.java MyTechnicalException.java OnExceptionRouteTest.java

Author: davsclaus
Date: Thu Nov 20 09:59:11 2008
New Revision: 719309

URL: http://svn.apache.org/viewvc?rev=719309&view=rev
Log:
Unit test for onException based on user forum

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyFunctionalException.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyOwnHandlerBean.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyServiceBean.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyTechnicalException.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRouteTest.java   (with props)

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyFunctionalException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyFunctionalException.java?rev=719309&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyFunctionalException.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyFunctionalException.java Thu Nov 20 09:59:11 2008
@@ -0,0 +1,24 @@
+/**
+ * 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.camel.processor.onexception;
+
+public class MyFunctionalException extends Exception {
+
+    public MyFunctionalException(String message) {
+        super(message);
+    }
+}
\ No newline at end of file

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

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

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyOwnHandlerBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyOwnHandlerBean.java?rev=719309&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyOwnHandlerBean.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyOwnHandlerBean.java Thu Nov 20 09:59:11 2008
@@ -0,0 +1,35 @@
+/**
+ * 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.camel.processor.onexception;
+
+import java.io.IOException;
+
+public class MyOwnHandlerBean {
+
+    private String payload;
+
+    public void handleFailure(String payload) throws Exception {
+        if (payload.indexOf("Error") > -1) {
+            throw new IOException("Damm something did not work");
+        }
+        this.payload = payload;
+    }
+
+    public String getPayload() {
+        return payload;
+    }
+}

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

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

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyServiceBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyServiceBean.java?rev=719309&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyServiceBean.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyServiceBean.java Thu Nov 20 09:59:11 2008
@@ -0,0 +1,30 @@
+/**
+ * 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.camel.processor.onexception;
+
+public class MyServiceBean {
+
+    public String validateOrder(String payload) throws Exception {
+        if (payload.indexOf("Tech") > -1) {
+            throw new MyTechnicalException("Damm a tech problem");
+        } else if (payload.indexOf("Func") > -1) {
+            throw new MyFunctionalException("Damm a func problem");
+        }
+
+        return "James";
+    }
+}

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

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

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyTechnicalException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyTechnicalException.java?rev=719309&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyTechnicalException.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/MyTechnicalException.java Thu Nov 20 09:59:11 2008
@@ -0,0 +1,24 @@
+/**
+ * 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.camel.processor.onexception;
+
+public class MyTechnicalException extends Exception {
+
+    public MyTechnicalException(String message) {
+        super(message);
+    }
+}

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

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

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRouteTest.java?rev=719309&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRouteTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRouteTest.java Thu Nov 20 09:59:11 2008
@@ -0,0 +1,132 @@
+/**
+ * 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.camel.processor.onexception;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * Unit test inspired by user forum.
+ */
+public class OnExceptionRouteTest extends ContextTestSupport {
+
+    private MyOwnHandlerBean myOwnHandlerBean;
+    private MyServiceBean myServiceBean;
+
+    public void testNoError() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "<order><type>myType</type><user>James</user></order>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testFunctionalError() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(0);
+
+        template.sendBody("direct:start", "<order><type>myType</type><user>Func</user></order>");
+
+        assertMockEndpointsSatisfied();
+        assertEquals("<order><type>myType</type><user>Func</user></order>", myOwnHandlerBean.getPayload());
+    }
+
+    public void testTechnicalError() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(1);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(0);
+
+        template.sendBody("direct:start", "<order><type>myType</type><user>Tech</user></order>");
+
+        assertMockEndpointsSatisfied();
+        // should not handle it
+        assertNull(myOwnHandlerBean.getPayload());
+    }
+
+    public void testErrorWhileHandlingException() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", "<order><type>myType</type><user>FuncError</user></order>");
+            fail("Should throw a RuntimeCamelException");
+        } catch (RuntimeCamelException e) {
+            assertEquals("Damm something did not work", e.getCause().getMessage());
+        }
+
+        assertMockEndpointsSatisfied();
+        // should not handle it
+        assertNull(myOwnHandlerBean.getPayload());
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        myOwnHandlerBean = new MyOwnHandlerBean();
+        myServiceBean = new MyServiceBean();
+        super.setUp();
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myOwnHandler", myOwnHandlerBean);
+        jndi.bind("myServiceBean", myServiceBean);
+        return jndi;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // default should errors go to mock:error
+                errorHandler(deadLetterChannel("mock:error"));
+                
+                // START SNIPPET: e1
+                // if a MyTechnicalException is thrown we will not try to redeliver and we mark it as handled
+                // so the caller does not get a failure
+                // since we have no to then the exchange will continue to be routed to the normal error handler
+                // destination that is mock:error as defined above
+                onException(MyTechnicalException.class).maximumRedeliveries(0).handled(true);
+
+                // if a MyFunctionalException is thrown we do not want Camel to redelivery but handle it our self using
+                // our bean myOwnHandler, then the exchange is not routed to the default error (mock:error)
+                onException(MyFunctionalException.class).maximumRedeliveries(0).handled(true).to("bean:myOwnHandler");
+
+                // here we route message to our service bean
+                from("direct:start").choice()
+                        .when().xpath("//type = 'myType'")
+                        .to("bean:myServiceBean")
+                // END SNIPPET: e1
+                        .to("mock:result");
+            }
+        };
+    }
+
+
+}

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

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