Search in sources :

Example 31 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory 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 32 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory 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 33 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.

the class SessionFactoryObserverTest method testSessionFactoryObserverProperty.

@Test
public void testSessionFactoryObserverProperty() {
    EntityManagerFactoryBuilder builder = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitInfoAdapter(), Collections.singletonMap(AvailableSettings.SESSION_FACTORY_OBSERVER, GoofySessionFactoryObserver.class.getName()));
    try {
        final EntityManagerFactory entityManagerFactory = builder.build();
        entityManagerFactory.close();
        Assert.fail("GoofyException should have been thrown");
    } catch (GoofyException e) {
    //success
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerFactoryBuilder(org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder) PersistenceUnitInfoAdapter(org.hibernate.jpa.test.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Example 34 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.

the class IdentifierGeneratorStrategyProviderTest method testIdentifierGeneratorStrategyProvider.

@Test
@SuppressWarnings("unchecked")
public void testIdentifierGeneratorStrategyProvider() {
    Map settings = new HashMap();
    settings.put(AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER, FunkyIdentifierGeneratorProvider.class.getName());
    settings.put(AvailableSettings.LOADED_CLASSES, Collections.singletonList(Cable.class));
    final EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitInfoAdapter(), settings).build();
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {
        entityManager.persist(new Cable());
        entityManager.flush();
        Assert.fail("FunkyException should have been thrown when the id is generated");
    } catch (FunkyException e) {
        entityManager.close();
        entityManagerFactory.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) Map(java.util.Map) HashMap(java.util.HashMap) PersistenceUnitInfoAdapter(org.hibernate.jpa.test.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Example 35 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.

the class OrmVersionTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/jpa/test/jee/invalid-orm-1.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)165 Test (org.junit.Test)72 EntityManager (javax.persistence.EntityManager)55 HashMap (java.util.HashMap)21 EJBException (javax.ejb.EJBException)19 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 JMSException (javax.jms.JMSException)15 Map (java.util.Map)14 RemoteException (java.rmi.RemoteException)11 InitialContext (javax.naming.InitialContext)11 EntityTransaction (javax.persistence.EntityTransaction)10 PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)10 NamingException (javax.naming.NamingException)8 PersistenceProvider (javax.persistence.spi.PersistenceProvider)7 BeanContext (org.apache.openejb.BeanContext)7 Item (org.hibernate.jpa.test.Item)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6