Search in sources :

Example 6 with JpaEntityManagerFactory

use of org.eclipse.persistence.jpa.JpaEntityManagerFactory in project eclipselink by eclipse-ee4j.

the class JPAPerformanceServerTestSuite method testPerformance.

public void testPerformance() throws Throwable {
    TestExecutor executor = new TestExecutor();
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("performance");
    executor.setSession(((JpaEntityManagerFactory) factory).getServerSession());
    executor.setEntityManagerFactory(factory);
    JPAPerformanceRegressionModel test = new JPAPerformanceRegressionModel();
    executor.runTest(test);
    executor.logResultForTestEntity(test);
    factory.close();
}
Also used : JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory)

Example 7 with JpaEntityManagerFactory

use of org.eclipse.persistence.jpa.JpaEntityManagerFactory in project eclipselink by eclipse-ee4j.

the class EmfRunnerInjector method inject.

/**
 * This method will create / inject EntityManagerFactory and SQLListeners into test instances
 */
public void inject(Object testInstance) throws Exception {
    if (testInstance == null) {
        return;
    }
    Class<?> cls = testInstance.getClass();
    // Check for duplicate injected EMFs
    Set<EntityManagerFactory> injectedEmfs = new HashSet<EntityManagerFactory>();
    Set<Field> injectedSqlListeneFields = new HashSet<Field>();
    // PU name -> Field
    Map<String, Field> annotatedSqlListenerFields = getSqlListenerFieldMap(cls);
    Map<String, Field> annotatedSqlCallListenerFields = getSqlCallListenerFieldMap(cls);
    for (Field emfField : cls.getDeclaredFields()) {
        Emf emfAnno = emfField.getAnnotation(Emf.class);
        if (emfAnno == null) {
            continue;
        }
        String emfName = emfAnno.name();
        SqlCollector sqlCollector = null;
        Field sqlListenerField = annotatedSqlListenerFields.get(emfName);
        if (sqlListenerField != null) {
            sqlCollector = new SqlCollector();
        }
        SqlCallCollector sqlCallCollector = null;
        Field sqlCallListenerField = annotatedSqlCallListenerFields.get(emfName);
        if (sqlCallListenerField != null) {
            sqlCallCollector = new SqlCallCollector();
        }
        Map<String, Object> props = null;
        if (testInstance instanceof PUPropertiesProvider) {
            props = ((PUPropertiesProvider) testInstance).getAdditionalPersistenceProperties(emfName);
        }
        EntityManagerFactory factory = getEntityManagerFactory(emfAnno, props);
        if (!injectedEmfs.add(factory)) {
            throw new RuntimeException("Attempted to inject the same EntityManagerFactory multiple times into " + testInstance + ". Please remove the duplicate @Emf annotation, or change the name so that each " + "annotation is for a distinct EntityManagerFactory.");
        }
        emfField.setAccessible(true);
        emfField.set(testInstance, factory);
        d("Injected " + factory + " into " + testInstance);
        if (sqlCollector != null) {
            ((JpaEntityManagerFactory) factory).getServerSession().getEventManager().addListener(sqlCollector);
            sqlCollector.inject(testInstance, sqlListenerField);
            injectedSqlListeneFields.add(sqlListenerField);
            d("Injected " + sqlCollector + " into " + sqlListenerField);
        }
        if (sqlCallCollector != null) {
            ((JpaEntityManagerFactory) factory).getServerSession().getEventManager().addListener(sqlCallCollector);
            sqlCallCollector.inject(testInstance, sqlCallListenerField);
            injectedSqlListeneFields.add(sqlCallListenerField);
            d("Injected " + sqlCallCollector + " into " + sqlCallListenerField);
        }
    }
    // Check for @SQLListeners that weren't injected
    validateSQLListenerAnnotations(annotatedSqlListenerFields, injectedSqlListeneFields);
}
Also used : Field(java.lang.reflect.Field) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) HashSet(java.util.HashSet)

Example 8 with JpaEntityManagerFactory

use of org.eclipse.persistence.jpa.JpaEntityManagerFactory in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testEMFTargetServerEnforcing.

public void testEMFTargetServerEnforcing() {
    // the test requires passing properties to createEMF or createContainerEMF method.
    if (isOnServer()) {
        return;
    }
    EntityManagerFactory emf = null;
    try {
        System.setProperty(SystemProperties.ENFORCE_TARGET_SERVER, "true");
        Map<String, String> properties = new HashMap<>(JUnitTestCaseHelper.getDatabaseProperties());
        properties.put(PersistenceUnitProperties.TARGET_SERVER, Platform.class.getName());
        properties.put(PersistenceUnitProperties.SESSION_NAME, "dummy-default-session");
        PersistenceProvider provider = new PersistenceProvider();
        JPAInitializer initializer = provider.getInitializer(getPersistenceUnitName(), properties);
        SEPersistenceUnitInfo sePUImpl = initializer.findPersistenceUnitInfo(getPersistenceUnitName(), properties);
        emf = provider.createContainerEntityManagerFactory(sePUImpl, properties);
        ServerPlatform server = ((JpaEntityManagerFactory) emf).getServerSession().getServerPlatform();
        assertNotNull(server);
        assertFalse("server should be overridden", server instanceof Platform);
    } finally {
        System.clearProperty(SystemProperties.ENFORCE_TARGET_SERVER);
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : WebLogic_12_Platform(org.eclipse.persistence.platform.server.wls.WebLogic_12_Platform) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform) WebSphere_7_Platform(org.eclipse.persistence.platform.server.was.WebSphere_7_Platform) HashMap(java.util.HashMap) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform)

Example 9 with JpaEntityManagerFactory

use of org.eclipse.persistence.jpa.JpaEntityManagerFactory in project eclipselink by eclipse-ee4j.

the class TestSessionCustomizer method customizerInvoked.

@Test
public void customizerInvoked() {
    Customizer customizerInstance = new Customizer();
    props.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, customizerInstance);
    try (JpaEntityManagerFactory emf = (JpaEntityManagerFactory) Persistence.createEntityManagerFactory(PU_NAME, props)) {
        emf.createEntityManager();
        Assert.assertTrue(customizerInstance.customized);
    }
}
Also used : JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) SessionCustomizer(org.eclipse.persistence.config.SessionCustomizer) Test(org.junit.Test)

Example 10 with JpaEntityManagerFactory

use of org.eclipse.persistence.jpa.JpaEntityManagerFactory in project eclipselink by eclipse-ee4j.

the class TestSessionCustomizer method stringCustomizerInvoked.

@Test
public void stringCustomizerInvoked() {
    props.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, Customizer.class.getName());
    try (JpaEntityManagerFactory emf = (JpaEntityManagerFactory) Persistence.createEntityManagerFactory(PU_NAME, props)) {
        emf.createEntityManager();
        Assert.assertTrue(Customizer.staticCustomized);
    }
}
Also used : JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) SessionCustomizer(org.eclipse.persistence.config.SessionCustomizer) Test(org.junit.Test)

Aggregations

JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)20 EntityManager (jakarta.persistence.EntityManager)14 HashMap (java.util.HashMap)13 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)13 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)13 PersistenceException (jakarta.persistence.PersistenceException)9 EntityExistsException (jakarta.persistence.EntityExistsException)7 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)7 OptimisticLockException (jakarta.persistence.OptimisticLockException)7 RollbackException (jakarta.persistence.RollbackException)7 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)7 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)7 QueryException (org.eclipse.persistence.exceptions.QueryException)7 ValidationException (org.eclipse.persistence.exceptions.ValidationException)7 TestProblemException (org.eclipse.persistence.testing.framework.TestProblemException)7 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)5 SessionBroker (org.eclipse.persistence.sessions.broker.SessionBroker)5 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)5 NoResultException (jakarta.persistence.NoResultException)4