camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [02/12] camel git commit: CAMEL-10490 Wrote unit test to illustrate concurrency problem
Date Wed, 30 Nov 2016 14:49:31 GMT
CAMEL-10490 Wrote unit test to illustrate concurrency problem


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a972befa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a972befa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a972befa

Branch: refs/heads/master
Commit: a972befaa7b27c6af49e40e0e5147939c66a189e
Parents: 4774c9f
Author: Bob Gaudaen <bob.gaudaen@gmail.com>
Authored: Fri Nov 18 17:01:12 2016 +0100
Committer: Bob Gaudaen <bob.gaudaen@gmail.com>
Committed: Wed Nov 30 14:28:27 2016 +0100

----------------------------------------------------------------------
 .../org/apache/camel/examples/Customer.java     |  9 ++++
 .../processor/jpa/JpaPollingConsumerTest.java   | 46 +++++++++++++++++++-
 .../camel/processor/jpa/springJpaRouteTest.xml  | 16 +++----
 3 files changed, 61 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a972befa/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java b/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
index 62ccdf2..efe21fa 100644
--- a/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
+++ b/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
@@ -37,6 +37,7 @@ public class Customer {
     private String name;
     @OneToOne(cascade = CascadeType.ALL)
     private Address address;
+    private int orders;
 
     public Long getId() {
         return id;
@@ -62,6 +63,14 @@ public class Customer {
         this.address = address;
     }
 
+    public int getOrders() {
+        return orders;
+    }
+
+    public void setOrders(int orders) {
+        this.orders = orders;
+    }
+
     @Override
     public String toString() {
         // OpenJPA warns about fields being accessed directly in methods if NOT using the
corresponding getters.

http://git-wip-us.apache.org/repos/asf/camel/blob/a972befa/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java
index 5b72211..c3bdc9a 100644
--- a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java
+++ b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.camel.processor.jpa;
 
-import java.util.List;
-
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.examples.Customer;
@@ -26,6 +24,10 @@ import org.junit.Test;
 import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.support.TransactionCallback;
 
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 public class JpaPollingConsumerTest extends AbstractJpaTest {
     protected static final String SELECT_ALL_STRING = "select x from " + Customer.class.getName()
+ " x";
 
@@ -64,6 +66,33 @@ public class JpaPollingConsumerTest extends AbstractJpaTest {
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testPollingConsumerHandlesLockedEntity() throws Exception {
+        Customer customer = new Customer();
+        customer.setName("Donald Duck");
+        save(customer);
+
+        Customer customer2 = new Customer();
+        customer2.setName("Goofy");
+        save(customer2);
+
+        assertEntitiesInDatabase(2, Customer.class.getName());
+
+        MockEndpoint mock = getMockEndpoint("mock:result2");
+        mock.expectedBodiesReceived(
+            "orders: 1",
+            "orders: 2"
+        );
+
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("name", "Donald%");
+
+        template.asyncRequestBodyAndHeaders("direct:start2", "message", headers);
+        template.asyncRequestBodyAndHeaders("direct:start2", "message", headers);
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new SpringRouteBuilder() {
@@ -77,6 +106,19 @@ public class JpaPollingConsumerTest extends AbstractJpaTest {
                             return a;
                         })
                     .to("mock:result");
+
+                from("direct:start2")
+                    .transacted()
+                    .pollEnrich().simple("jpa://" + Customer.class.getName() + "?joinTransaction=true&consumeLockEntity=true&query=select
c from Customer c where c.name like '${header.name}'")
+                    .aggregationStrategy((originalExchange, jpaExchange) -> {
+                        Customer customer = jpaExchange.getIn().getBody(Customer.class);
+                        customer.setOrders(customer.getOrders()+1);
+
+                        return jpaExchange;
+                    })
+                    .to("jpa://" + Customer.class.getName() + "?joinTransaction=true&usePassedInEntityManager=true")
+                    .setBody().simple("orders: ${body.orders}")
+                    .to("mock:result2");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a972befa/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/springJpaRouteTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/springJpaRouteTest.xml
b/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/springJpaRouteTest.xml
index 358fb7b..b8af124 100644
--- a/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/springJpaRouteTest.xml
+++ b/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/springJpaRouteTest.xml
@@ -19,16 +19,16 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
-    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
-        <property name="transactionManager">
-            <bean class="org.springframework.orm.jpa.JpaTransactionManager">
-                <property name="entityManagerFactory" ref="entityManagerFactory"/>
-            </bean>
-        </property>
-    </bean>
-
     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
         <property name="persistenceUnitName" value="camel"/>
     </bean>
 
+    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
+        <property name="entityManagerFactory" ref="entityManagerFactory"/>
+    </bean>
+
+    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
+        <property name="transactionManager" ref="transactionManager"/>
+    </bean>
+
 </beans>


Mime
View raw message