use of org.apache.aries.jpa.container.context.PersistenceContextProvider in project aries by apache.
the class AdvancedEJBBundleTest method testJPAContextSharing.
@Test
@Ignore
public void testJPAContextSharing() throws Exception {
System.setProperty("openejb.validation.output.level", "VERBOSE");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
addToZip(zos, "JPA_MANIFEST.MF", "META-INF/MANIFEST.MF");
addToZip(zos, "persistence.xml", "META-INF/persistence.xml");
addToZip(zos, "beans/integration/impl/JPASingleton.class");
addToZip(zos, "beans/jpa/Laptop.class");
zos.close();
Bundle test = context().installBundle("", new ByteArrayInputStream(baos.toByteArray()));
try {
test.start();
PersistenceContextProvider provider = context().getService(PersistenceContextProvider.class);
HashMap<String, Object> props = new HashMap<String, Object>();
props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
provider.registerContext("ejb-test", context().getBundle(), props);
Object bean = context().getService(context().getServiceReference(JPASingleton.class.getName()));
UserTransaction ut = context().getService(UserTransaction.class);
Method m = bean.getClass().getMethod("editEntity", String.class);
EntityManager em = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=ejb-test)(" + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true)" + "(" + PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE + "=*))").createEntityManager();
ut.begin();
try {
Object e = test.loadClass(Laptop.class.getName()).newInstance();
e.getClass().getMethod("setSerialNumber", String.class).invoke(e, "ABC123");
e.getClass().getMethod("setNumberOfCores", int.class).invoke(e, 1);
em.persist(e);
m.invoke(bean, "ABC123");
assertEquals(4, e.getClass().getMethod("getNumberOfCores").invoke(e));
assertEquals(Integer.MAX_VALUE, e.getClass().getMethod("getHardDiskSize").invoke(e));
} finally {
ut.commit();
}
test.stop();
} finally {
test.uninstall();
}
}
Aggregations