Author: cmueller
Date: Thu Aug 5 11:50:44 2010
New Revision: 982553
URL: http://svn.apache.org/viewvc?rev=982553&view=rev
Log:
added two new tests to be sure, that the JPA component also can handle
collections and arrays of entity beans
Modified:
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
Modified:
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java?rev=982553&r1=982552&r2=982553&view=diff
==============================================================================
---
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
(original)
+++
camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
Thu Aug 5 11:50:44 2010
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.jpa;
+import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -34,14 +35,13 @@ import org.apache.camel.impl.DefaultCame
import org.apache.camel.impl.DefaultExchange;
import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;
import static org.apache.camel.util.ServiceHelper.startServices;
import static org.apache.camel.util.ServiceHelper.stopServices;
+
/**
* @version $Revision: 931444 $
*/
@@ -54,28 +54,6 @@ public class JpaUsePersistTest extends A
protected JpaTemplate jpaTemplate;
protected Consumer consumer;
protected Exchange receivedExchange;
-
- @SuppressWarnings("unchecked")
- @Before
- public void setUp() throws Exception {
- template = camelContext.createProducerTemplate();
- startServices(template, camelContext);
-
- endpoint = camelContext.getEndpoint("jpa://" +
Customer.class.getName() + "?usePersist=true", JpaEndpoint.class);
-
- transactionStrategy = endpoint.createTransactionStrategy();
- jpaTemplate = endpoint.getTemplate();
-
- transactionStrategy.execute(new JpaCallback() {
- public Object doInJpa(EntityManager entityManager) throws
PersistenceException {
- entityManager.createQuery("delete from " +
Customer.class.getName()).executeUpdate();
- return null;
- }
- });
-
- assertEntitiesInDatabase(0, Customer.class.getName());
- assertEntitiesInDatabase(0, Address.class.getName());
- }
@After
public void tearDown() throws Exception {
@@ -85,8 +63,9 @@ public class JpaUsePersistTest extends A
@SuppressWarnings("unchecked")
@Test
public void produceNewEntity() throws Exception {
- Customer customer = createDefaultCustomer();
+ setUp("jpa://" + Customer.class.getName() + "?usePersist=true");
+ Customer customer = createDefaultCustomer();
Exchange exchange = new DefaultExchange(camelContext);
exchange.getIn().setBody(customer);
Exchange returnedExchange = template.send(endpoint, exchange);
@@ -110,42 +89,66 @@ public class JpaUsePersistTest extends A
@SuppressWarnings("unchecked")
@Test
+ public void produceNewEntitiesFromList() throws Exception {
+ setUp("jpa://" + List.class.getName() + "?usePersist=true");
+
+ List<Customer> customers = new ArrayList<Customer>();
+ customers.add(createDefaultCustomer());
+ customers.add(createDefaultCustomer());
+ Exchange exchange = new DefaultExchange(camelContext);
+ exchange.getIn().setBody(customers);
+ Exchange returnedExchange = template.send(endpoint, exchange);
+
+ List returnedCustomers = returnedExchange.getIn().getBody(List.class);
+ assertEquals(2, returnedCustomers.size());
+
+ assertEntitiesInDatabase(2, Customer.class.getName());
+ assertEntitiesInDatabase(2, Address.class.getName());
+ }
+
+ @Test
+ public void produceNewEntitiesFromArray() throws Exception {
+ setUp("jpa://" + Customer[].class.getName() + "?usePersist=true");
+
+ Customer[] customers = new Customer[] {createDefaultCustomer(),
createDefaultCustomer()};
+ Exchange exchange = new DefaultExchange(camelContext);
+ exchange.getIn().setBody(customers);
+ Exchange returnedExchange = template.send(endpoint, exchange);
+
+ Customer[] returnedCustomers =
returnedExchange.getIn().getBody(Customer[].class);
+ assertEquals(2, returnedCustomers.length);
+
+ assertEntitiesInDatabase(2, Customer.class.getName());
+ assertEntitiesInDatabase(2, Address.class.getName());
+ }
+
+ @Test
public void produceExistingEntityShouldThowAnException() throws Exception {
- final Customer customer = createDefaultCustomer();
- transactionStrategy.execute(new JpaCallback() {
- public Object doInJpa(EntityManager entityManager) throws
PersistenceException {
- entityManager.persist(customer);
- entityManager.flush();
- return null;
- }
- });
+ setUp("jpa://" + Customer.class.getName() + "?usePersist=true");
- assertEntitiesInDatabase(1, Customer.class.getName());
- assertEntitiesInDatabase(1, Address.class.getName());
+ final Customer customer = createDefaultCustomer();
+ save(customer);
customer.setName("Max Mustermann");
customer.getAddress().setAddressLine1("Musterstr. 1");
customer.getAddress().setAddressLine2("11111 Enterhausen");
-
Exchange exchange = new DefaultExchange(camelContext);
exchange.getIn().setBody(customer);
Exchange returnedExchange = template.send(endpoint, exchange);
assertTrue(returnedExchange.isFailed());
assertNotNull(returnedExchange.getException());
+
+ assertEntitiesInDatabase(1, Customer.class.getName());
+ assertEntitiesInDatabase(1, Address.class.getName());
}
-
- @SuppressWarnings("unchecked")
+
@Test
public void consumeEntity() throws Exception {
+ setUp("jpa://" + Customer.class.getName() + "?usePersist=true");
+
final Customer customer = createDefaultCustomer();
- transactionStrategy.execute(new JpaCallback() {
- public Object doInJpa(EntityManager entityManager) throws
PersistenceException {
- entityManager.persist(customer);
- entityManager.flush();
- return null;
- }
- });
+ save(customer);
final CountDownLatch latch = new CountDownLatch(1);
@@ -168,7 +171,7 @@ public class JpaUsePersistTest extends A
assertEquals(customer.getAddress().getAddressLine1(),
receivedCustomer.getAddress().getAddressLine1());
assertEquals(customer.getAddress().getAddressLine2(),
receivedCustomer.getAddress().getAddressLine2());
assertEquals(customer.getAddress().getId(),
receivedCustomer.getAddress().getId());
-
+
// give a bit tiem for consumer to delete after done
Thread.sleep(1000);
@@ -177,6 +180,41 @@ public class JpaUsePersistTest extends A
}
@SuppressWarnings("unchecked")
+ private void setUp(String endpointUri) throws Exception {
+ template = camelContext.createProducerTemplate();
+ startServices(template, camelContext);
+
+ endpoint = camelContext.getEndpoint(endpointUri, JpaEndpoint.class);
+
+ transactionStrategy = endpoint.createTransactionStrategy();
+ jpaTemplate = endpoint.getTemplate();
+
+ transactionStrategy.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws
PersistenceException {
+ entityManager.createQuery("delete from " +
Customer.class.getName()).executeUpdate();
+ return null;
+ }
+ });
+
+ assertEntitiesInDatabase(0, Customer.class.getName());
+ assertEntitiesInDatabase(0, Address.class.getName());
+ }
+
+ @SuppressWarnings("unchecked")
+ private void save(final Customer customer) {
+ transactionStrategy.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws
PersistenceException {
+ entityManager.persist(customer);
+ entityManager.flush();
+ return null;
+ }
+ });
+
+ assertEntitiesInDatabase(1, Customer.class.getName());
+ assertEntitiesInDatabase(1, Address.class.getName());
+ }
+
+ @SuppressWarnings("unchecked")
private void assertEntitiesInDatabase(int count, String entity) {
List results = jpaTemplate.find("select o from " + entity + " o");
assertEquals(count, results.size());