Author: cschneider
Date: Mon Jul 27 18:03:50 2015
New Revision: 1692923

URL: http://svn.apache.org/r1692923
Log:
[ARIES-1362] Add test for coordinations

Modified:
    
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarLifeCycle.java
    
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceImpl.java
    
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java

Modified: 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarLifeCycle.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarLifeCycle.java?rev=1692923&r1=1692922&r2=1692923&view=diff
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarLifeCycle.java
 (original)
+++ 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarLifeCycle.java
 Mon Jul 27 18:03:50 2015
@@ -15,10 +15,15 @@
  */
 package org.apache.aries.jpa.container.itest.bundle.blueprint.impl;
 
+import java.util.Map;
 import java.util.UUID;
 
+import javax.persistence.EntityManager;
+import javax.transaction.Transactional;
+
 import org.apache.aries.jpa.container.itest.entities.Car;
 import org.apache.aries.jpa.container.itest.entities.CarService;
+import org.osgi.service.coordinator.Coordination;
 import org.osgi.service.coordinator.Coordinator;
 
 /**
@@ -29,26 +34,24 @@ public class CarLifeCycle implements Run
     CarService carService;
     Coordinator coordinator;
     
+    @Transactional(Transactional.TxType.REQUIRED)
     @Override
     public void run() {
         Car car = new Car();
         UUID uuid = UUID.randomUUID();
         String id = "blue " + uuid.toString();
+        car.setEngineSize(1);
         car.setNumberPlate(id);
         carService.addCar(car);
-       
-//        try {
-//            readAndUpdate(id);
-//            throw new IllegalStateException("This should not work with an 
active coordination");
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-
-        
-        coordinator.begin("jpa", 0);
+        EntityManager em = getEmFromCoord();
+        if (!em.contains(car)) {
+            throw new IllegalStateException("Transaction should case 
EntityManager to be kept open");
+        }
         readAndUpdate(id);
-        coordinator.pop().end();
-        
+        Car car3 = carService.getCar(id);
+        if (car3.getEngineSize() != 100) {
+            throw new IllegalStateException("Engine size should have been 
changed to 100");
+        }
         carService.deleteCar(id);
         Car car2 = carService.getCar(id);
         if (car2 != null) {
@@ -56,19 +59,30 @@ public class CarLifeCycle implements Run
         }
     }
 
-    /**
-     * These operations only work if the EntityManager stays open
-     * @param id 
-     */
-    private void readAndUpdate(String id) {
+    public void readAndUpdate(String id) {
         Car car = carService.getCar(id);
         if (car == null) {
             throw new IllegalStateException("Expected a car with id " + id);
         }
         car.setEngineSize(100);
-        carService.updateCar(car);
     }
     
+    @SuppressWarnings("unchecked")
+    private EntityManager getEmFromCoord() {
+        Coordination coord = coordinator.peek();
+        if (coord == null) {
+            throw new IllegalStateException("No coordination found");
+        }
+        while (coord != null) {
+            Map<String, EntityManager> emMap = (Map<String, 
EntityManager>)coord.getVariables().get(EntityManager.class);
+            if (emMap != null) {
+                return emMap.values().iterator().next();
+            }
+            coord = coord.getEnclosingCoordination();
+        }
+        throw new IllegalStateException("No EntityManager found in 
coordinations");
+    }
+
     public void setCarService(CarService carService) {
         this.carService = carService;
     }

Modified: 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceImpl.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceImpl.java?rev=1692923&r1=1692922&r2=1692923&view=diff
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceImpl.java
 (original)
+++ 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceImpl.java
 Mon Jul 27 18:03:50 2015
@@ -42,7 +42,7 @@ public class CarServiceImpl extends Abst
 
     @Override
     public void updateCar(Car car) {
-        em.persist(car);
+        em.merge(car);
     }
 
     @Override

Modified: 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java?rev=1692923&r1=1692922&r2=1692923&view=diff
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java
 (original)
+++ 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java
 Mon Jul 27 18:03:50 2015
@@ -42,7 +42,9 @@ public class BlueprintTest extends Abstr
     
     @Test
     public void testCoordination() {
+        assertNoCoordination();
         CarService carService = getCarService("em");
+        assertNoCars(carService);
         for (int c=0; c<100; c++) {
             System.out.println(c);
             Coordination coord = null;
@@ -97,7 +99,10 @@ public class BlueprintTest extends Abstr
     
     @Test
     public void testCoordinationLifecycle() throws InterruptedException, 
ExecutionException {
+        CarService carService = getCarService("em");
+        assertNoCars(carService);
         Runnable carLifeCycle = getService(Runnable.class, 
"(type=carCoordinated)");
+        carLifeCycle.run();
         ExecutorService exec = Executors.newFixedThreadPool(20);
         List<Future<?>> futures = new ArrayList<>();
         for (int c=0; c<100; c++) {
@@ -108,6 +113,7 @@ public class BlueprintTest extends Abstr
         for (Future<?> future : futures) {
             future.get();
         }
+        assertNoCars(carService);
     }
 
     private CarService getCarService(String type) {
@@ -115,8 +121,7 @@ public class BlueprintTest extends Abstr
     }
 
     private void carLifecycle(CarService carService) {
-        Coordination coord = coordinator.peek();
-        Assert.assertNull("There should not be a coordination on this thread", 
coord);
+        assertNoCoordination();
         if (carService.getCar(BLACK_CAR_PLATE) != null) {
             carService.deleteCar(BLUE_CAR_PLATE);
         }
@@ -125,6 +130,15 @@ public class BlueprintTest extends Abstr
         carService.deleteCar(BLUE_CAR_PLATE);
     }
 
+    private void assertNoCoordination() {
+        Coordination coord = coordinator.peek();
+        Assert.assertNull("There should not be a coordination on this thread", 
coord);
+    }
+    
+    private void assertNoCars(CarService carService) {
+        Assert.assertEquals("Invalid number of cars", 0, 
carService.getCars().size());
+    }
+
     @Configuration
     public Option[] configuration() {
         return new Option[] {


Reply via email to