Search in sources :

Example 91 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project syndesis by syndesisio.

the class ConfiguredConnectionTest method setUp.

@BeforeClass
public static void setUp() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("io.syndesis.runtime.db");
    em = factory.createEntityManager();
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) BeforeClass(org.junit.BeforeClass)

Example 92 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method startView.

/**
 * Useful to start an EntityManager-In-View. This allows test operations that want to read directly from the database
 * to work without lazy init exceptions, etc... This is not needed for MockMVC#perform operations, since those
 * requests will include the OpenEntityManagerInView filter as part of their flow. At the completion of the test
 * operation, the {@link #endView()} method should be called to end the scope of the view.
 * </p>
 * This view scope is also aware of nested views against the same persistence unit, so you don't need to worry
 * about coding carefully to avoid nesting calls to startView.
 *
 * @param siteId
 * @param sandBoxName
 */
public void startView(Long siteId, String sandBoxName) {
    EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
    boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
    if (!isEntityManagerInView) {
        EntityManager em = emf.createEntityManager();
        em.clear();
        EntityManagerHolder emHolder = new EntityManagerHolder(em);
        TransactionSynchronizationManager.bindResource(emf, emHolder);
        Stack<String> stack = new Stack<>();
        TransactionSynchronizationManager.bindResource("emStack", stack);
        if (siteId != null) {
            Site site = siteService.retrievePersistentSiteById(siteId);
            ThreadLocalManager.remove();
            BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
            context.setSite(site);
            context.setDeployBehavior(DeployBehavior.CLONE_PARENT);
            context.setAdmin(true);
            if (!StringUtils.isEmpty(sandBoxName)) {
                SandBox sb = findSandBox(siteId, sandBoxName, false);
                context.setSandBox(sb);
            }
        }
    }
    Stack<String> stack = (Stack<String>) TransactionSynchronizationManager.getResource("emStack");
    RuntimeException trace = new RuntimeException();
    StringWriter writer = new StringWriter();
    PrintWriter pw = new PrintWriter(writer);
    trace.printStackTrace(pw);
    stack.push(writer.toString());
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Stack(java.util.Stack) EntityManager(javax.persistence.EntityManager) StringWriter(java.io.StringWriter) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) PrintWriter(java.io.PrintWriter)

Example 93 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method findSandBox.

private SandBox findSandBox(Long siteId, String sandBoxName, boolean initialize) {
    if (initialize) {
        startView(siteId, sandBoxName);
    }
    try {
        if (DEFAULT_SB.equals(sandBoxName)) {
            return getDefaultSandBox(siteId);
        } else {
            EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
            return findSandBox(sandBoxName, emHolder.getEntityManager());
        }
    } finally {
        if (initialize) {
            endView();
        }
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 94 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method endView.

/**
 * Complete the scope of the EntityManager-In-View operation. See {@link #startView(Long, String)}.
 */
public void endView() {
    EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
    boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
    if (isEntityManagerInView) {
        Stack<Integer> stack = (Stack<Integer>) TransactionSynchronizationManager.getResource("emStack");
        if (stack.size() <= 1) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf);
            EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
            TransactionSynchronizationManager.unbindResource("emStack");
            ThreadLocalManager.remove();
        } else {
            stack.pop();
        }
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) Stack(java.util.Stack)

Example 95 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class CorrelationPersistenceTest method testCreateCorrelation.

@Test
public void testCreateCorrelation() throws Exception {
    EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY);
    EntityManager em = emf.createEntityManager();
    Query query = em.createNamedQuery("GetProcessInstanceIdByCorrelation");
    query.setParameter("ckey", "test123");
    List<Long> processInstances = query.getResultList();
    em.close();
    assertNotNull(processInstances);
    assertEquals(1, processInstances.size());
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)302 EntityManager (javax.persistence.EntityManager)103 Test (org.junit.Test)90 HashMap (java.util.HashMap)48 EntityTransaction (javax.persistence.EntityTransaction)30 EJBException (javax.ejb.EJBException)22 Map (java.util.Map)17 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 ArrayList (java.util.ArrayList)15 JMSException (javax.jms.JMSException)15 KieSession (org.kie.api.runtime.KieSession)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 Properties (java.util.Properties)13 InitialContext (javax.naming.InitialContext)13 List (java.util.List)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 RemoteException (java.rmi.RemoteException)11 Query (javax.persistence.Query)11 PrintWriter (java.io.PrintWriter)10