Search in sources :

Example 1 with PersistenceUnitDescriptorAdapter

use of org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter in project hibernate-orm by hibernate.

the class InterceptorTest method testConfiguredInterceptor.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test Interceptor settings
@Test
public void testConfiguredInterceptor() {
    Map settings = basicSettings();
    settings.put(org.hibernate.cfg.AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 2 with PersistenceUnitDescriptorAdapter

use of org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter in project hibernate-orm by hibernate.

the class InterceptorTest method testDeprecatedConfiguredInterceptor.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test deprecated Interceptor settings
@Test
public void testDeprecatedConfiguredInterceptor() {
    Map settings = basicSettings();
    settings.put(AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 3 with PersistenceUnitDescriptorAdapter

use of org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter in project hibernate-orm by hibernate.

the class PersisterClassProviderTest method testPersisterClassProvider.

@Test
@SuppressWarnings("unchecked")
public void testPersisterClassProvider() {
    Map settings = SettingsGenerator.generateSettings(PersisterClassResolverInitiator.IMPL_NAME, GoofyPersisterClassProvider.class, AvailableSettings.LOADED_CLASSES, Arrays.asList(Bell.class));
    try {
        EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
        entityManagerFactory.close();
    } catch (PersistenceException e) {
        Assert.assertNotNull(e.getCause());
        Assert.assertNotNull(e.getCause().getCause());
        Assert.assertEquals(GoofyException.class, e.getCause().getCause().getClass());
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 4 with PersistenceUnitDescriptorAdapter

use of org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter in project hibernate-orm by hibernate.

the class ColumnWithExplicitReferenceToPrimaryTableTest method testColumnAnnotationWithExplicitReferenceToPrimaryTable.

@Test
@TestForIssue(jiraKey = "HHH-8539")
public void testColumnAnnotationWithExplicitReferenceToPrimaryTable() {
    final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {

        @Override
        public List<String> getManagedClassNames() {
            return Arrays.asList(AnEntity.class.getName());
        }
    };
    final Map settings = new HashMap();
    settings.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(pu, settings).build();
    emf.close();
}
Also used : HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with PersistenceUnitDescriptorAdapter

use of org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter in project hibernate-orm by hibernate.

the class TransactionCommitFailureTest method testConfiguredInterceptor.

@Test
public void testConfiguredInterceptor() {
    Map settings = basicSettings();
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    try {
        em.getTransaction().begin();
        transactionFailureTrigger.set(true);
        em.getTransaction().commit();
    } catch (RollbackException e) {
        assertEquals(COMMIT_FAILURE, e.getCause().getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) RollbackException(javax.persistence.RollbackException) Map(java.util.Map) Test(org.junit.Test)

Aggregations

PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)11 Map (java.util.Map)10 EntityManagerFactory (javax.persistence.EntityManagerFactory)10 Test (org.junit.Test)10 EntityManager (javax.persistence.EntityManager)7 Item (org.hibernate.jpa.test.Item)6 HashMap (java.util.HashMap)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Properties (java.util.Properties)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)1 FailureExpected (org.hibernate.testing.FailureExpected)1