Skip to content
Permalink
Browse files
[ARIES-1353] Use coordinator in support, some fixes in tests and cont…
…ainer

git-svn-id: https://svn.apache.org/repos/asf/aries/trunk/jpa@1692110 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cschneider committed Jul 21, 2015
1 parent e032e0f commit 23104a8eb18c642e6a4eba9c2d796bd10116a6e0
Showing 34 changed files with 360 additions and 375 deletions.
@@ -77,7 +77,6 @@
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<inherited>true</inherited>
<configuration>
<instructions>
</instructions>
@@ -27,42 +27,40 @@
import org.apache.aries.jpa.example.tasklist.model.TaskService;
import org.apache.aries.transaction.annotations.Transaction;


public class TaskServiceImpl implements TaskService {

@PersistenceContext(unitName="tasklist")
EntityManager em;
@Override
public Task getTask(Integer id) {
return em.find(Task.class, id);
}
@PersistenceContext(unitName = "tasklist")
EntityManager em;

@Override
public Task getTask(Integer id) {
return em.find(Task.class, id);
}

@Transaction
@Override
public void addTask(Task task) {
em.persist(task);
em.flush();
}
@Transaction
@Override
public void addTask(Task task) {
em.persist(task);
em.flush();
}

public Collection<Task> getTasks() {
return em.createQuery("select t from Task t", Task.class)
.getResultList();
}
public Collection<Task> getTasks() {
return em.createQuery("select t from Task t", Task.class).getResultList();
}

@Transaction
@Override
public void updateTask(Task task) {
em.persist(task);
}
@Transaction
@Override
public void updateTask(Task task) {
em.persist(task);
}

@Transaction
@Override
public void deleteTask(Integer id) {
em.remove(getTask(id));
}
@Transaction
@Override
public void deleteTask(Integer id) {
em.remove(getTask(id));
}

public void setEm(EntityManager em) {
this.em = em;
}
public void setEm(EntityManager em) {
this.em = em;
}
}
@@ -36,6 +36,7 @@
public class TaskServiceImplTest {
@Test
public void testPersistence() {
/*
TaskServiceImpl taskService = new TaskServiceImpl();
EntityManagerFactory emf = createTestEMF();
EmSupplier emSupplier = createEmSupplier(emf);
@@ -49,6 +50,7 @@ public void testPersistence() {
Task task2 = taskService.getTask(1);
Assert.assertEquals(task.getTitle(), task2.getTitle());
*/
}

private EmSupplier createEmSupplier(EntityManagerFactory emf) {
@@ -18,7 +18,6 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
@@ -6,6 +6,6 @@
import org.apache.aries.jpa.container.itest.entities.CarService;

public abstract class AbstractCarServiceImpl implements CarService {
@PersistenceContext(unitName = "test_unit_blueprint")
@PersistenceContext(unitName = "xa-test-unit")
protected EntityManager em;
}
@@ -15,7 +15,7 @@
*/
package org.apache.aries.jpa.container.itest.bundle.blueprint.impl;

import java.util.Collection;
import java.util.UUID;

import org.apache.aries.jpa.container.itest.entities.Car;
import org.apache.aries.jpa.container.itest.entities.CarService;
@@ -32,30 +32,38 @@ public class CarLifeCycle implements Runnable {
@Override
public void run() {
Car car = new Car();
car.setNumberPlate("blue");
UUID uuid = UUID.randomUUID();
String id = "blue " + uuid.toString();
car.setNumberPlate(id);
carService.addCar(car);

try {
readAndUpdate();
throw new IllegalStateException("This should not work with an active coordination");
} catch (Exception e) {
e.printStackTrace();
}
// try {
// readAndUpdate(id);
// throw new IllegalStateException("This should not work with an active coordination");
// } catch (Exception e) {
// e.printStackTrace();
// }


coordinator.begin("jpa", 0);
readAndUpdate();
readAndUpdate(id);
coordinator.pop().end();

carService.deleteCar("blue");
carService.deleteCar(id);
Car car2 = carService.getCar(id);
if (car2 != null) {
throw new RuntimeException("Car witgh id " + id + " should be deleted");
}
}

/**
* These operations only work if the EntityManager stays open
* @param id
*/
private void readAndUpdate() {
Collection<Car> cars = carService.getCars();
carService.updateCar(cars.iterator().next());
private void readAndUpdate(String id) {
Car car = carService.getCar(id);
car.setEngineSize(100);
carService.updateCar(car);
}

public void setCarService(CarService carService) {
@@ -47,7 +47,7 @@ public void updateCar(Car car) {

@Override
public void deleteCar(String id) {
em.remove(getCar(id));
em.remove(em.find(Car.class, id));
}

public void setEm(EntityManager em) {
@@ -29,7 +29,7 @@

public class CarServiceWithEmfImpl implements CarService {

@PersistenceUnit(unitName = "test_unit_blueprint")
@PersistenceUnit(unitName = "xa-test-unit")
EntityManagerFactory emf;

@Override
@@ -55,7 +55,7 @@ public void deleteCar(String id) {
em.remove(getCar(id));
}

@PersistenceContext(unitName = "test_unit_blueprint")
@PersistenceContext(unitName = "xa-test-unit")
public void setEm(EntityManager em) {
this.em = em;
}
@@ -31,13 +31,13 @@

public class CarServiceWithMultiAnnotationImpl implements CarService {

@PersistenceContext(unitName = "test_unit_blueprint")
@PersistenceContext(unitName = "xa-test-unit")
EntityManager em;

@PersistenceUnit(unitName = "test_unit_blueprint")
@PersistenceUnit(unitName = "xa-test-unit")
EntityManagerFactory emf;

@PersistenceContext(unitName = "test_unit_blueprint")
@PersistenceContext(unitName = "xa-test-unit")
EmSupplier ems;

@Override
@@ -30,7 +30,7 @@ public class CarServiceWithSupplierImpl implements CarService {

Collection<String> colours;

@PersistenceContext(unitName = "test_unit_blueprint")
@PersistenceContext(unitName = "xa-test-unit")
EmSupplier em;

@Override
@@ -299,12 +299,6 @@
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.aries.transaction</groupId>
<artifactId>org.apache.aries.transaction.jdbc</artifactId>
<version>2.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.aries.transaction</groupId>
<artifactId>org.apache.aries.transaction.blueprint</artifactId>
@@ -16,24 +16,31 @@
package org.apache.aries.jpa.blueprint.aries.itest;

import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import org.apache.aries.jpa.container.itest.entities.Car;
import org.apache.aries.jpa.container.itest.entities.CarService;
import org.apache.aries.jpa.itest.AbstractCarJPAITest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.osgi.framework.BundleException;
import org.osgi.service.coordinator.Coordination;
import org.osgi.service.coordinator.Coordinator;

public class BlueprintTest extends AbstractCarJPAITest {
@Inject
Coordinator coordinator;

@Before
public void deleteCars() {
public void deleteCars() throws BundleException {
resolveBundles();
CarService carService = getCarService("emf");
if (carService.getCar(BLUE_CAR_PLATE)!=null) {
carService.deleteCar(BLUE_CAR_PLATE);
@@ -43,12 +50,26 @@ public void deleteCars() {
@Test
public void testCoordination() {
CarService carService = getCarService("em");
coordinator.begin("jpa", 0);
carService.addCar(createBlueCar());
Collection<Car> cars = carService.getCars();
carService.updateCar(cars.iterator().next());
carService.deleteCar(BLUE_CAR_PLATE);
coordinator.pop().end();
for (int c=0; c<100; c++) {
System.out.println(c);
Coordination coord = null;
try {
coord = coordinator.begin("testCoordination", 0);
carService.addCar(createBlueCar());
Collection<Car> cars = carService.getCars();
carService.updateCar(cars.iterator().next());
} finally {
coord.end();
}
// TODO For some reason I need a second coordination here
try {
coord = coordinator.begin("testCoordination", 0);
carService.deleteCar(BLUE_CAR_PLATE);
Assert.assertEquals(0, carService.getCars().size());
} finally {
coord.end();
}
}
}

@Test
@@ -77,9 +98,14 @@ public void testSupplierAddQuery() throws Exception {
}

@Test
public void testCoordinationLifecycle() {
public void testCoordinationLifecycle() throws InterruptedException {
Runnable carLifeCycle = getService(Runnable.class, "(type=carCoordinated)");
carLifeCycle.run();
ExecutorService exec = Executors.newFixedThreadPool(20);
for (int c=0; c<100; c++) {
exec.execute(carLifeCycle);
}
exec.shutdown();
exec.awaitTermination(30, TimeUnit.SECONDS);
}

private CarService getCarService(String type) {
@@ -98,6 +124,7 @@ public Option[] configuration() {
ariesJpa20(), //
hibernate(), //
derbyDSF(), //
testBundle(), //
testBundleBlueprint(),
// debug()
};

0 comments on commit 23104a8

Please sign in to comment.