Search in sources :

Example 1 with PersistenceContextProvider

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) PersistenceContextProvider(org.apache.aries.jpa.container.context.PersistenceContextProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) EntityManager(javax.persistence.EntityManager) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 UserTransaction (javax.transaction.UserTransaction)1 PersistenceContextProvider (org.apache.aries.jpa.container.context.PersistenceContextProvider)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 Bundle (org.osgi.framework.Bundle)1