Search in sources :

Example 1 with TheEntity

use of org.hibernate.test.cdi.events.TheEntity in project hibernate-orm by hibernate.

the class ValidExtendedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, standIn).build();
    final SessionFactoryImplementor sessionFactory;
    try {
        sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
    } catch (Exception e) {
        StandardServiceRegistryBuilder.destroy(ssr);
        throw e;
    }
    try {
        // The CDI bean should not be built immediately...
        assertFalse(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        // But now lets initialize CDI and do the callback
        final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, TheListener.class);
        try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
            standIn.beanManagerReady(cdiContainer.getBeanManager());
            // at this point the bean should have been accessed
            assertTrue(Monitor.wasInstantiated());
            assertEquals(0, Monitor.currentCount());
            try {
                inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
                inTransaction(sessionFactory, session -> {
                    TheEntity it = session.find(TheEntity.class, 1);
                    assertNotNull(it);
                });
            } finally {
                inTransaction(sessionFactory, session -> {
                    session.createQuery("delete TheEntity").executeUpdate();
                });
            }
        }
    } finally {
        sessionFactory.close();
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) Test(org.junit.Test)

Example 2 with TheEntity

use of org.hibernate.test.cdi.events.TheEntity in project hibernate-orm by hibernate.

the class InvalidExtendedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, standIn).build();
    final SessionFactoryImplementor sessionFactory;
    try {
        sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
    } catch (Exception e) {
        StandardServiceRegistryBuilder.destroy(ssr);
        throw e;
    }
    try {
        // The CDI bean should not be built immediately...
        assertFalse(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            fail("Expecting failure");
        } catch (IllegalStateException expected) {
        }
    } finally {
        sessionFactory.close();
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 3 with TheEntity

use of org.hibernate.test.cdi.events.TheEntity in project hibernate-orm by hibernate.

the class DelayedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, TheListener.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should not be built immediately...
        assertFalse(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
            // The CDI bean should have been built on first use
            assertTrue(Monitor.wasInstantiated());
            assertEquals(1, Monitor.currentCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 4 with TheEntity

use of org.hibernate.test.cdi.events.TheEntity in project hibernate-orm by hibernate.

the class StandardCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, TheListener.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should have been built immediately...
        assertTrue(Monitor.wasInstantiated());
        assertEquals(0, Monitor.currentCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1)));
            assertEquals(1, Monitor.currentCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.events.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

MetadataSources (org.hibernate.boot.MetadataSources)4 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)4 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 TheEntity (org.hibernate.test.cdi.events.TheEntity)4 Test (org.junit.Test)4 SeContainer (javax.enterprise.inject.se.SeContainer)3 SeContainerInitializer (javax.enterprise.inject.se.SeContainerInitializer)3