HHH-13309 Test both the legacy and newer ExtendedBeanManager interfaces

This commit is contained in:
Yoann Rodière 2019-03-11 09:24:30 +01:00
parent b79d0f9628
commit 69a1c2cc08
8 changed files with 162 additions and 85 deletions

View File

@ -19,6 +19,7 @@ import org.hibernate.tool.schema.Action;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.cdi.events.Monitor;
import org.hibernate.test.cdi.events.TheEntity;
import org.hibernate.test.cdi.testsupport.TestingExtendedBeanManager;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
@ -34,16 +35,27 @@ import static org.junit.Assert.fail;
*/
public class InvalidExtendedCdiSupportTest extends BaseUnitTestCase {
@Test
public void testIt() {
Monitor.reset();
public void test() {
doTest( TestingExtendedBeanManager.create() );
}
final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
/**
* NOTE : we use the deprecated one here to make sure this continues to work.
* Scott still uses this in WildFly and we need it to continue to work there
*/
@Test
public void testLegacy() {
doTest( TestingExtendedBeanManager.createLegacy() );
}
private void doTest(TestingExtendedBeanManager beanManager) {
Monitor.reset();
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr )
.applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, standIn )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, beanManager )
.build();
@ -93,10 +105,4 @@ public class InvalidExtendedCdiSupportTest extends BaseUnitTestCase {
sessionFactory.close();
}
}
public static class ExtendedBeanManagerImpl implements ExtendedBeanManager {
@Override
public void registerLifecycleListener(LifecycleListener lifecycleListener) {
}
}
}

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.cdi.events.extended;
import java.util.function.Consumer;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.BeanManager;
@ -17,13 +18,14 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.event.spi.jpa.ExtendedBeanManager;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.tool.schema.Action;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.cdi.events.Monitor;
import org.hibernate.test.cdi.events.TheEntity;
import org.hibernate.test.cdi.events.TheListener;
import org.hibernate.test.cdi.testsupport.TestingExtendedBeanManager;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
@ -40,17 +42,27 @@ import static org.junit.Assert.assertTrue;
*/
public class ValidExtendedCdiSupportTest extends BaseUnitTestCase {
@Test
public void testIt() {
public void test() {
doTest( TestingExtendedBeanManager.create() );
}
/**
* NOTE : we use the deprecated one here to make sure this continues to work.
* Scott still uses this in WildFly and we need it to continue to work there
*/
@Test
public void testLegacy() {
doTest( TestingExtendedBeanManager.createLegacy() );
}
private void doTest(TestingExtendedBeanManager beanManager) {
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 )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, beanManager )
.build();
@ -82,7 +94,7 @@ public class ValidExtendedCdiSupportTest extends BaseUnitTestCase {
.addBeanClasses( Monitor.class, TheListener.class );
try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
standIn.beanManagerReady( cdiContainer.getBeanManager() );
beanManager.notifyListenerReady( cdiContainer.getBeanManager() );
// at this point the bean should have been accessed
assertTrue( Monitor.wasInstantiated() );
@ -116,17 +128,4 @@ public class ValidExtendedCdiSupportTest extends BaseUnitTestCase {
sessionFactory.close();
}
}
public static class ExtendedBeanManagerImpl implements ExtendedBeanManager {
private LifecycleListener callback;
@Override
public void registerLifecycleListener(LifecycleListener lifecycleListener) {
this.callback = lifecycleListener;
}
public void beanManagerReady(BeanManager beanManager) {
callback.beanManagerInitialized( beanManager );
}
}
}

View File

@ -19,6 +19,7 @@ import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.tool.schema.Action;
import org.hibernate.test.cdi.testsupport.TestingExtendedBeanManager;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
@ -32,8 +33,19 @@ import static org.hamcrest.MatcherAssert.assertThat;
public class ExtendedMixedAccessTest implements BeanContainer.LifecycleOptions {
@Test
public void testExtendedMixedAccess() {
final Helper.TestingExtendedBeanManager extendedBeanManager = Helper.createExtendedBeanManager();
doTest( TestingExtendedBeanManager.create() );
}
/**
* NOTE : we use the deprecated one here to make sure this continues to work.
* Scott still uses this in WildFly and we need it to continue to work there
*/
@Test
public void testLegacyExtendedMixedAccess() {
doTest( TestingExtendedBeanManager.createLegacy() );
}
private void doTest(TestingExtendedBeanManager extendedBeanManager) {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, extendedBeanManager )

View File

@ -22,37 +22,4 @@ public class Helper {
.addBeanClasses( HostedBean.class, InjectedHostedBean.class );
return cdiInitializer.initialize();
}
/**
* NOTE : we use the deprecated one here to make sure this continues to work.
* Scott still uses this in WildFly and we need it to continue to work there
*/
public static TestingExtendedBeanManager createExtendedBeanManager() {
return new TestingExtendedBeanManager() {
private ExtendedBeanManager.LifecycleListener lifecycleListener;
@Override
public void registerLifecycleListener(LifecycleListener lifecycleListener) {
if ( this.lifecycleListener != null ) {
throw new RuntimeException( "LifecycleListener already registered" );
}
this.lifecycleListener = lifecycleListener;
}
@Override
public void notifyListenerReady(BeanManager beanManager) {
lifecycleListener.beanManagerInitialized( beanManager );
}
@Override
public void notifyListenerShuttingDown(BeanManager beanManager) {
lifecycleListener.beforeBeanManagerDestroyed( beanManager );
}
};
}
public interface TestingExtendedBeanManager extends ExtendedBeanManager {
void notifyListenerReady(BeanManager beanManager);
void notifyListenerShuttingDown(BeanManager beanManager);
}
}

View File

@ -9,7 +9,6 @@ package org.hibernate.test.cdi.general.nonregistrymanaged.extended;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.BeanManager;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
@ -18,7 +17,6 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.event.spi.jpa.ExtendedBeanManager;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.tool.schema.Action;
@ -38,6 +36,7 @@ import org.hibernate.test.cdi.general.nonregistrymanaged.TheNamedDependentBean;
import org.hibernate.test.cdi.general.nonregistrymanaged.TheNestedDependentBean;
import org.hibernate.test.cdi.general.nonregistrymanaged.TheNonHibernateBeanConsumer;
import org.hibernate.test.cdi.general.nonregistrymanaged.TheSharedApplicationScopedBean;
import org.hibernate.test.cdi.testsupport.TestingExtendedBeanManager;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -52,16 +51,28 @@ import static org.junit.Assert.assertEquals;
*/
public class NonRegistryManagedExtendedCdiSupportTest extends BaseUnitTestCase {
@Test
public void testIt() {
public void test() {
doTest( TestingExtendedBeanManager.create() );
}
/**
* NOTE : we use the deprecated one here to make sure this continues to work.
* Scott still uses this in WildFly and we need it to continue to work there
*/
@Test
public void testLegacy() {
doTest( TestingExtendedBeanManager.createLegacy() );
}
private void doTest(TestingExtendedBeanManager beanManager) {
Monitor.reset();
final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer =
new TheFallbackBeanInstanceProducer();
final NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator =
new NonRegistryManagedBeanConsumingIntegrator( fallbackBeanInstanceProducer );
try (SessionFactoryImplementor sessionFactory = buildSessionFactory( standIn, beanConsumingIntegrator )) {
try (SessionFactoryImplementor sessionFactory = buildSessionFactory( beanManager, beanConsumingIntegrator )) {
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
.disableDiscovery()
.addBeanClasses( TheApplicationScopedBean.class )
@ -93,7 +104,7 @@ public class NonRegistryManagedExtendedCdiSupportTest extends BaseUnitTestCase {
// Nested dependent bean: 1 instance per bean that depends on it
assertEquals( 1, Monitor.theNestedDependentBean().currentInstantiationCount() );
standIn.beanManagerReady( cdiContainer.getBeanManager() );
beanManager.notifyListenerReady( cdiContainer.getBeanManager() );
beanConsumingIntegrator.ensureInstancesInitialized();
@ -164,7 +175,7 @@ public class NonRegistryManagedExtendedCdiSupportTest extends BaseUnitTestCase {
assertEquals( 7, Monitor.theNestedDependentBean().currentPreDestroyCount() );
}
private SessionFactoryImplementor buildSessionFactory(ExtendedBeanManagerImpl standIn,
private SessionFactoryImplementor buildSessionFactory(TestingExtendedBeanManager beanManager,
NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator) {
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder()
.applyIntegrator( beanConsumingIntegrator )
@ -172,7 +183,7 @@ public class NonRegistryManagedExtendedCdiSupportTest extends BaseUnitTestCase {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr )
.applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, standIn )
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, beanManager )
.build();
try {
@ -187,17 +198,4 @@ public class NonRegistryManagedExtendedCdiSupportTest extends BaseUnitTestCase {
throw e;
}
}
public static class ExtendedBeanManagerImpl implements ExtendedBeanManager {
private LifecycleListener callback;
@Override
public void registerLifecycleListener(LifecycleListener lifecycleListener) {
this.callback = lifecycleListener;
}
public void beanManagerReady(BeanManager beanManager) {
callback.beanManagerInitialized( beanManager );
}
}
}

View File

@ -0,0 +1,25 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.cdi.testsupport;
import javax.enterprise.inject.spi.BeanManager;
public interface TestingExtendedBeanManager {
void notifyListenerReady(BeanManager beanManager);
void notifyListenerShuttingDown(BeanManager beanManager);
static TestingExtendedBeanManager create() {
return new TestingExtendedBeanManagerImpl();
}
static TestingExtendedBeanManager createLegacy() {
return new TestingLegacyExtendedBeanManagerImpl();
}
}

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.cdi.testsupport;
import javax.enterprise.inject.spi.BeanManager;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
class TestingExtendedBeanManagerImpl
implements TestingExtendedBeanManager, ExtendedBeanManager {
private ExtendedBeanManager.LifecycleListener lifecycleListener;
@Override
public void registerLifecycleListener(ExtendedBeanManager.LifecycleListener lifecycleListener) {
if ( this.lifecycleListener != null ) {
throw new RuntimeException( "LifecycleListener already registered" );
}
this.lifecycleListener = lifecycleListener;
}
@Override
public void notifyListenerReady(BeanManager beanManager) {
lifecycleListener.beanManagerInitialized( beanManager );
}
@Override
public void notifyListenerShuttingDown(BeanManager beanManager) {
lifecycleListener.beforeBeanManagerDestroyed( beanManager );
}
}

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.cdi.testsupport;
import javax.enterprise.inject.spi.BeanManager;
import org.hibernate.jpa.event.spi.jpa.ExtendedBeanManager;
class TestingLegacyExtendedBeanManagerImpl
implements TestingExtendedBeanManager, org.hibernate.jpa.event.spi.jpa.ExtendedBeanManager {
private ExtendedBeanManager.LifecycleListener lifecycleListener;
@Override
public void registerLifecycleListener(ExtendedBeanManager.LifecycleListener lifecycleListener) {
if ( this.lifecycleListener != null ) {
throw new RuntimeException( "LifecycleListener already registered" );
}
this.lifecycleListener = lifecycleListener;
}
@Override
public void notifyListenerReady(BeanManager beanManager) {
lifecycleListener.beanManagerInitialized( beanManager );
}
@Override
public void notifyListenerShuttingDown(BeanManager beanManager) {
lifecycleListener.beforeBeanManagerDestroyed( beanManager );
}
}