HHH-15068 Reproduce problem where type resolution accesses the bean manager before it's ready

This commit is contained in:
Yoann Rodière 2022-02-03 13:25:26 +01:00 committed by Christian Beikov
parent 5ced797dd5
commit b8060d2df7
1 changed files with 122 additions and 0 deletions

View File

@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<hibernate-mapping>\n" +
" <class name=\"" + TheEntity.class.getName() + "\" entity-name=\"" + ENTITY_NAME + "\" table=\"" + TABLE_NAME + "\">\n" +
" <id name=\"id\">\n" +
" <generator class=\"org.hibernate.id.enhanced.SequenceStyleGenerator\">\n" +
" <param name=\"sequence_name\">" + TABLE_NAME + "_GENERATOR</param>\n" +
" <param name=\"table_name\">" + TABLE_NAME + "_GENERATOR</param>\n" +
" <param name=\"initial_value\">1</param>\n" +
" <param name=\"increment_size\">1</param>\n" +
" </generator>\n" +
" </id>\n" +
" <property name=\"name\" />\n" +
" <property name=\"myEnum\" >\n" +
" <type name=\"org.hibernate.type.EnumType\">\n" +
" <param name=\"enumClass\">" + MyEnum.class.getName() + "</param>\n" +
" </type>\n" +
" </property>\n" +
" </class>\n" +
"</hibernate-mapping>\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 );
}
}
}