use of org.hibernate.test.cdi.general.nonregistrymanaged.TheFallbackBeanInstanceProducer in project hibernate-orm by hibernate.
the class NonRegistryManagedDelayedCdiSupportTest method testIt.
@Test
public void testIt() {
Monitor.reset();
final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer = new TheFallbackBeanInstanceProducer();
final NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator = new NonRegistryManagedBeanConsumingIntegrator(fallbackBeanInstanceProducer);
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(TheApplicationScopedBean.class).addBeanClasses(TheNamedApplicationScopedBean.class, TheMainNamedApplicationScopedBeanImpl.class, TheAlternativeNamedApplicationScopedBeanImpl.class).addBeanClasses(TheSharedApplicationScopedBean.class).addBeanClasses(TheDependentBean.class).addBeanClasses(TheNamedDependentBean.class, TheMainNamedDependentBeanImpl.class, TheAlternativeNamedDependentBeanImpl.class).addBeanClasses(TheNestedDependentBean.class).addBeanClasses(TheNonHibernateBeanConsumer.class);
try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
// Simulate CDI bean consumers outside of Hibernate ORM
Instance<TheNonHibernateBeanConsumer> nonHibernateBeanConsumerInstance = cdiContainer.getBeanManager().createInstance().select(TheNonHibernateBeanConsumer.class);
nonHibernateBeanConsumerInstance.get();
// Expect the shared bean to have been instantiated already, but only that one
assertEquals(0, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(1, Monitor.theNestedDependentBean().currentInstantiationCount());
try (SessionFactoryImplementor sessionFactory = buildSessionFactory(cdiContainer, beanConsumingIntegrator)) {
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been integrated and has requested beans
// See NonRegistryManagedBeanConsumingIntegrator for a detailed list of requested beans
beanConsumingIntegrator.ensureInstancesInitialized();
// Application scope: maximum 1 instance as soon as at least one was requested
assertEquals(1, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
// Dependent scope: 1 instance per bean we requested explicitly
assertEquals(2, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
// Reflection-instantiated: 1 instance per bean we requested explicitly
assertEquals(2, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(2, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(7, Monitor.theNestedDependentBean().currentInstantiationCount());
// Expect one PostConstruct call per CDI bean instance
assertEquals(1, Monitor.theApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPostConstructCount());
assertEquals(2, Monitor.theDependentBean().currentPostConstructCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPostConstructCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPostConstructCount());
// Expect no PreDestroy call yet
assertEquals(0, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been disintegrated and has released beans
// release() should have an effect on exclusively used application-scoped beans
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
// release() should have no effect on shared application-scoped beans (they will be released when they are no longer used)
assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
// release() should have an effect on dependent-scoped beans
assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
// The nested dependent bean instances should have been destroyed along with the beans that depend on them
// (the instances used in application-scoped beans should not have been destroyed)
assertEquals(6, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// After the CDI context has ended, PreDestroy should have been called on every created bean
// (see the assertions about instantiations above for an explanation of the expected counts)
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
use of org.hibernate.test.cdi.general.nonregistrymanaged.TheFallbackBeanInstanceProducer in project hibernate-orm by hibernate.
the class NonRegistryManagedExtendedCdiSupportTest method testIt.
@Test
public void testIt() {
Monitor.reset();
final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer = new TheFallbackBeanInstanceProducer();
final NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator = new NonRegistryManagedBeanConsumingIntegrator(fallbackBeanInstanceProducer);
try (SessionFactoryImplementor sessionFactory = buildSessionFactory(standIn, beanConsumingIntegrator)) {
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(TheApplicationScopedBean.class).addBeanClasses(TheNamedApplicationScopedBean.class, TheMainNamedApplicationScopedBeanImpl.class, TheAlternativeNamedApplicationScopedBeanImpl.class).addBeanClasses(TheSharedApplicationScopedBean.class).addBeanClasses(TheDependentBean.class).addBeanClasses(TheNamedDependentBean.class, TheMainNamedDependentBeanImpl.class, TheAlternativeNamedDependentBeanImpl.class).addBeanClasses(TheNestedDependentBean.class).addBeanClasses(TheNonHibernateBeanConsumer.class);
try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
// Simulate CDI bean consumers outside of Hibernate ORM
Instance<TheNonHibernateBeanConsumer> nonHibernateBeanConsumerInstance = cdiContainer.getBeanManager().createInstance().select(TheNonHibernateBeanConsumer.class);
nonHibernateBeanConsumerInstance.get();
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been integrated and has requested beans
// BUT it has not fetched instances of beans yet, so non-shared beans should not have been instantiated yet.
assertEquals(0, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(1, Monitor.theNestedDependentBean().currentInstantiationCount());
standIn.beanManagerReady(cdiContainer.getBeanManager());
beanConsumingIntegrator.ensureInstancesInitialized();
// Here the NonRegistryManagedBeanConsumingIntegrator *did* fetch an instance of each bean,
// so all beans should have been instantiated.
// See NonRegistryManagedBeanConsumingIntegrator for a detailed list of requested beans
// Application scope: maximum 1 instance as soon as at least one was requested
assertEquals(1, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
// Dependent scope: 1 instance per bean we requested explicitly
assertEquals(2, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
// Reflection-instantiated: 1 instance per bean we requested explicitly
assertEquals(2, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(2, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(7, Monitor.theNestedDependentBean().currentInstantiationCount());
// Expect one PostConstruct call per CDI bean instance
assertEquals(1, Monitor.theApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPostConstructCount());
assertEquals(2, Monitor.theDependentBean().currentPostConstructCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPostConstructCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPostConstructCount());
// Expect no PreDestroy call yet
assertEquals(0, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// After the CDI context has ended, PreDestroy should have been called on every "normal-scoped" CDI bean
// (i.e. all CDI beans excepting the dependent ones we requested explicitly and haven't released yet)
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(3, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been disintegrated and has released beans
// The dependent beans should now have been released as well.
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
use of org.hibernate.test.cdi.general.nonregistrymanaged.TheFallbackBeanInstanceProducer in project hibernate-orm by hibernate.
the class NonRegistryManagedStandardCdiSupportTest method testIt.
@Test
public void testIt() {
Monitor.reset();
final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer = new TheFallbackBeanInstanceProducer();
final NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator = new NonRegistryManagedBeanConsumingIntegrator(fallbackBeanInstanceProducer);
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(TheApplicationScopedBean.class).addBeanClasses(TheNamedApplicationScopedBean.class, TheMainNamedApplicationScopedBeanImpl.class, TheAlternativeNamedApplicationScopedBeanImpl.class).addBeanClasses(TheSharedApplicationScopedBean.class).addBeanClasses(TheDependentBean.class).addBeanClasses(TheNamedDependentBean.class, TheMainNamedDependentBeanImpl.class, TheAlternativeNamedDependentBeanImpl.class).addBeanClasses(TheNestedDependentBean.class).addBeanClasses(TheNonHibernateBeanConsumer.class);
try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
// Simulate CDI bean consumers outside of Hibernate ORM
Instance<TheNonHibernateBeanConsumer> nonHibernateBeanConsumerInstance = cdiContainer.getBeanManager().createInstance().select(TheNonHibernateBeanConsumer.class);
nonHibernateBeanConsumerInstance.get();
// Expect the shared bean to have been instantiated already, but only that one
assertEquals(0, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(0, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(1, Monitor.theNestedDependentBean().currentInstantiationCount());
try (SessionFactoryImplementor sessionFactory = buildSessionFactory(cdiContainer, beanConsumingIntegrator)) {
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been integrated and has requested beans
// See NonRegistryManagedBeanConsumingIntegrator for a detailed list of requested beans
beanConsumingIntegrator.ensureInstancesInitialized();
// Application scope: maximum 1 instance as soon as at least one was requested
assertEquals(1, Monitor.theApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
// Dependent scope: 1 instance per bean we requested explicitly
assertEquals(2, Monitor.theDependentBean().currentInstantiationCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentInstantiationCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
// Reflection-instantiated: 1 instance per bean we requested explicitly
assertEquals(2, fallbackBeanInstanceProducer.currentInstantiationCount());
assertEquals(2, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals(7, Monitor.theNestedDependentBean().currentInstantiationCount());
// Expect one PostConstruct call per CDI bean instance
assertEquals(1, Monitor.theApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPostConstructCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPostConstructCount());
assertEquals(2, Monitor.theDependentBean().currentPostConstructCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPostConstructCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPostConstructCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPostConstructCount());
// Expect no PreDestroy call yet
assertEquals(0, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// Here, the NonRegistryManagedBeanConsumingIntegrator has just been disintegrated and has released beans
// release() should have an effect on exclusively used application-scoped beans
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
// release() should have no effect on shared application-scoped beans (they will be released when they are no longer used)
assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
// release() should have an effect on dependent-scoped beans
assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
// The nested dependent bean instances should have been destroyed along with the beans that depend on them
// (the instances used in application-scoped beans should not have been destroyed)
assertEquals(6, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
// After the CDI context has ended, PreDestroy should have been called on every created CDI bean
// (see the assertions about instantiations above for an explanation of the expected counts)
assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
assertEquals(7, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
Aggregations