diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java new file mode 100644 index 0000000000..e0debf6ab3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java @@ -0,0 +1,122 @@ +/* + * 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.orm.test.cdi.lifecycle; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.resource.beans.container.spi.ExtendedBeanManager; + +import org.hibernate.testing.TestForIssue; +import org.junit.jupiter.api.Test; + +import jakarta.enterprise.inject.spi.BeanManager; + +@TestForIssue(jiraKey = "HHH-15068") +public class ExtendedBeanManagerNotAvailableDuringTypeResolutionTest { + + @Test + public void tryIt() throws IOException { + final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( + AvailableSettings.JAKARTA_CDI_BEAN_MANAGER, + new ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.ExtendedBeanManagerImpl() + ) + .build(); + + try { + // this will trigger trying to locate MyEnumType as a managed-bean + try (InputStream mappingInputStream = + new ByteArrayInputStream( TheEntity.ENTITY_DEFINITION.getBytes( StandardCharsets.UTF_8 ) ) ) { + new MetadataSources( ssr ) + .addInputStream( mappingInputStream ) + .buildMetadata() + .buildSessionFactory(); + } + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + public static class TheEntity { + private static final String ENTITY_NAME = "TheEntity"; + private static final String TABLE_NAME = "TheEntity"; + public static final String ENTITY_DEFINITION = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " " + TABLE_NAME + "_GENERATOR\n" + + " " + TABLE_NAME + "_GENERATOR\n" + + " 1\n" + + " 1\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " " + MyEnum.class.getName() + "\n" + + " \n" + + " \n" + + " \n" + + "\n"; + private Integer id; + private String name; + private MyEnum myEnum; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MyEnum getMyEnum() { + return myEnum; + } + + public void setMyEnum( + MyEnum myEnum) { + this.myEnum = myEnum; + } + } + + private enum MyEnum { + VALUE1, + VALUE2; + } + + public static class ExtendedBeanManagerImpl implements ExtendedBeanManager { + private LifecycleListener lifecycleListener; + + @Override + public void registerLifecycleListener(LifecycleListener lifecycleListener) { + assert this.lifecycleListener == null; + this.lifecycleListener = lifecycleListener; + } + + public void notify(BeanManager ready) { + lifecycleListener.beanManagerInitialized( ready ); + } + } +}