Author: davsclaus Date: Fri Apr 9 15:49:30 2010 New Revision: 932470 URL: http://svn.apache.org/viewvc?rev=932470&view=rev Log: Added unit test based on user forum issue.
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java (with props) camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml (contents, props changed) - copied, changed from r932460, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTryCatchFinallyAndErrorHandlerTest.xml Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java?rev=932470&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java Fri Apr 9 15:49:30 2010 @@ -0,0 +1,95 @@ +/** + * 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.spring.issues; + +import javax.sql.DataSource; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * @version $Revision$ + */ +public class SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest extends SpringTestSupport { + protected JdbcTemplate jdbc; + + protected void setUp() throws Exception { + super.setUp(); + + // create database and insert dummy data + final DataSource ds = getMandatoryBean(DataSource.class, "dataSource"); + jdbc = new JdbcTemplate(ds); + jdbc.execute("create table books (title varchar(50))"); + } + + @Override + protected void tearDown() throws Exception { + jdbc.execute("drop table books"); + super.tearDown(); + } + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml"); + } + + public void testSpringTXOnExceptionIssueCommit() throws Exception { + int count = jdbc.queryForInt("select count(*) from books"); + assertEquals("Number of books", 0, count); + + // we succeeded so no message to on exception + getMockEndpoint("mock:onException").expectedMessageCount(0); + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", "Camel in Action"); + + assertMockEndpointsSatisfied(); + + // we did commit so there should be 1 books + count = jdbc.queryForInt("select count(*) from books"); + assertEquals("Number of books", 1, count); + } + + public void testSpringTXOnExceptionIssueRollback() throws Exception { + int count = jdbc.queryForInt("select count(*) from books"); + assertEquals("Number of books", 0, count); + + getMockEndpoint("mock:onException").expectedMessageCount(1); + // we failed so no message to result + getMockEndpoint("mock:result").expectedMessageCount(0); + + try { + template.sendBody("direct:start", "Donkey in Action"); + fail("Should have thrown exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); + assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage()); + } + + assertMockEndpointsSatisfied(); + + // we did rollback so there should be 0 books + count = jdbc.queryForInt("select count(*) from books"); + assertEquals("Number of books", 0, count); + } + +} Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml (from r932460, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTryCatchFinallyAndErrorHandlerTest.xml) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTryCatchFinallyAndErrorHandlerTest.xml&r1=932460&r2=932470&rev=932470&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTryCatchFinallyAndErrorHandlerTest.xml (original) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml Fri Apr 9 15:49:30 2010 @@ -22,48 +22,55 @@ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> - <bean id="myFail" class="java.lang.IllegalArgumentException"> - <constructor-arg index="0" value="Damn"/> + <!-- the data source --> + <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> + <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> + <property name="url" value="jdbc:hsqldb:mem:camel"/> + <property name="username" value="sa"/> + <property name="password" value=""/> </bean> - <bean id="myFailAgain" class="java.lang.IllegalStateException"> - <constructor-arg index="0" value="Damn Kong"/> + <!-- spring transaction manager --> + <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> + <property name="dataSource" ref="dataSource"/> </bean> - <camelContext trace="true" id="camel" xmlns="http://camel.apache.org/schema/spring"> + <!-- the bean which inserts data in the database --> + <bean id="bookService" class="org.apache.camel.spring.interceptor.BookService"> + <property name="dataSource" ref="dataSource"/> + </bean> + + <!-- spring template is requires new --> + <bean id="PROPAGATION_REQUIRES_NEW_TEMPLATE" + class="org.springframework.transaction.support.TransactionTemplate"> + <property name="transactionManager" ref="txManager"/> + <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/> + </bean> - <errorHandler type="DeadLetterChannel" id="dlc" deadLetterUri="mock:dead"/> + <!-- the error handler we use which is transactional --> + <bean id="myErrorHandler" class="org.apache.camel.spring.spi.TransactionErrorHandlerBuilder"> + <property name="transactionTemplate" ref="PROPAGATION_REQUIRES_NEW_TEMPLATE"/> + </bean> - <route errorHandlerRef="dlc"> + <!-- use the error handler --> + <camelContext errorHandlerRef="myErrorHandler" id="camel" xmlns="http://camel.apache.org/schema/spring"> + + <!-- define a global on exception to log the exception --> + <onException> + <exception>java.lang.Exception</exception> + <log loggingLevel="ERROR" message="Damn this failed because of: ${exception.message}"/> + <to uri="mock:onException"/> + </onException> + + <!-- the route which is transacted --> + <route> <from uri="direct:start"/> - <doTry> - <filter> - <simple>${body} contains 'Donkey'</simple> - <to uri="mock:donkey"/> - <throwException ref="myFail"/> - </filter> - <to uri="mock:bar"/> - <doCatch> - <exception>java.lang.IllegalArgumentException</exception> - <to uri="mock:catch"/> - </doCatch> - <doFinally> - <to uri="mock:finally"/> - </doFinally> - </doTry> - <!-- outside try .. catch .. finally the error handler kick in again --> - <choice> - <when> - <simple>${body} contains 'Kong'</simple> - <to uri="mock:kong"/> - <throwException ref="myFailAgain"/> - </when> - <otherwise> - <to uri="mock:other"/> - </otherwise> - </choice> - <to uri="mock:end"/> + <transacted/> + <!-- if fail then this bean will thrown an exception --> + <bean ref="bookService"/> + <to uri="mock:result"/> </route> + </camelContext> </beans> Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/SpringTransactionErrorHandlerAndContextScopedOnExceptionIssueTest.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml