EntityManagerFactory emf = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("
+ PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))");
EntityManager em = emf.createEntityManager();
QuiesceParticipant participant = getParticipant("org.apache.aries.jpa.container");
TestQuiesceCallback callback = new TestQuiesceCallback();
participant.quiesce(callback, Collections.singletonList(context().getBundleByName(
"org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle")));
Thread.sleep(1000);
assertFalse("Quiesce finished", callback.bundleClearedUp());
emf = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("
+ PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))");
em.close();
assertTrue("Quiesce not finished", callback.bundleClearedUp());
ServiceReference[] refs = bundleContext.getAllServiceReferences(EntityManagerFactory.class.getName(), "(&(osgi.unit.name=test-unit)("
+ PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))");
assertNull("No context should exist",refs);
//Restart the bundle to check the unit gets re-registered and is not immediately unregistered
Bundle b = context().getBundleByName("org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle");
b.stop();
b.start();
emf = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("
+ PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))");
em = emf.createEntityManager();
em.close();
emf = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=test-unit)("
+ PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true))", 100);
//Test a second time to make sure state isn't held
em = emf.createEntityManager();
callback = new TestQuiesceCallback();
participant.quiesce(callback, Collections.singletonList(context().getBundleByName(
"org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle")));
Thread.sleep(1000);
assertFalse("Quiesce finished", callback.bundleClearedUp());