|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.cdi.converters.delayed; |
6 | 6 |
|
7 | | -import jakarta.enterprise.inject.se.SeContainer; |
8 | | -import jakarta.enterprise.inject.se.SeContainerInitializer; |
9 | | -import org.hibernate.boot.MetadataSources; |
10 | | -import org.hibernate.boot.registry.BootstrapServiceRegistry; |
11 | | -import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; |
12 | | -import org.hibernate.boot.registry.StandardServiceRegistry; |
13 | | -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
14 | | -import org.hibernate.cfg.AvailableSettings; |
15 | | -import org.hibernate.engine.spi.SessionFactoryImplementor; |
16 | 7 | import org.hibernate.orm.test.cdi.converters.ConverterBean; |
17 | 8 | import org.hibernate.orm.test.cdi.converters.MonitorBean; |
18 | 9 | import org.hibernate.orm.test.cdi.converters.TheEntity; |
| 10 | +import org.hibernate.orm.test.cdi.testsupport.CdiContainer; |
| 11 | +import org.hibernate.orm.test.cdi.testsupport.CdiContainerLinker; |
| 12 | +import org.hibernate.orm.test.cdi.testsupport.CdiContainerScope; |
19 | 13 | import org.hibernate.testing.orm.junit.BaseUnitTest; |
20 | | -import org.hibernate.testing.util.ServiceRegistryUtil; |
21 | | -import org.hibernate.tool.schema.Action; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.hibernate.testing.orm.junit.Setting; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
22 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtendWith; |
23 | 22 |
|
24 | | -import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; |
| 23 | +import static org.hibernate.cfg.ManagedBeanSettings.CDI_BEAN_MANAGER; |
| 24 | +import static org.hibernate.cfg.ManagedBeanSettings.DELAY_CDI_ACCESS; |
25 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals; |
26 | 26 | import static org.junit.jupiter.api.Assertions.assertFalse; |
27 | 27 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
30 | 30 | /** |
31 | 31 | * @author Steve Ebersole |
32 | 32 | */ |
| 33 | +@SuppressWarnings("JUnitMalformedDeclaration") |
33 | 34 | @BaseUnitTest |
34 | 35 | public class DelayedCdiHostedConverterTest { |
35 | | - @Test |
36 | | - public void testIt() { |
37 | | - MonitorBean.reset(); |
38 | | - |
39 | | - final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance() |
40 | | - .disableDiscovery() |
41 | | - .addBeanClasses( MonitorBean.class, ConverterBean.class ); |
42 | | - try ( final SeContainer cdiContainer = cdiInitializer.initialize() ) { |
43 | | - BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build(); |
44 | | - |
45 | | - final StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder( bsr ) |
46 | | - .applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP ) |
47 | | - .applySetting( AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager() ) |
48 | | - .applySetting( AvailableSettings.DELAY_CDI_ACCESS, "true" ) |
49 | | - .build(); |
50 | | - |
51 | | - final SessionFactoryImplementor sessionFactory; |
52 | | - |
53 | | - try { |
54 | | - sessionFactory = (SessionFactoryImplementor) new MetadataSources( ssr ) |
55 | | - .addAnnotatedClass( TheEntity.class ) |
56 | | - .buildMetadata() |
57 | | - .getSessionFactoryBuilder() |
58 | | - .build(); |
59 | | - } |
60 | | - catch ( Exception e ) { |
61 | | - StandardServiceRegistryBuilder.destroy( ssr ); |
62 | | - throw e; |
63 | | - } |
64 | | - |
65 | | - // The CDI bean should not have been built immediately... |
66 | | - assertFalse( MonitorBean.wasInstantiated() ); |
67 | | - assertEquals( 0, MonitorBean.currentFromDbCount() ); |
68 | | - assertEquals( 0, MonitorBean.currentToDbCount() ); |
| 36 | + @AfterEach |
| 37 | + void tearDown(SessionFactoryScope factoryScope) { |
| 38 | + factoryScope.dropData(); |
| 39 | + } |
69 | 40 |
|
70 | | - try { |
71 | | - inTransaction( |
72 | | - sessionFactory, |
73 | | - session -> session.persist( new TheEntity( 1, "me", 5 ) ) |
74 | | - ); |
| 41 | + @Test |
| 42 | + @ExtendWith(MonitorBean.Resetter.class ) |
| 43 | + @CdiContainer(beanClasses = {MonitorBean.class, ConverterBean.class}) |
| 44 | + @ServiceRegistry( |
| 45 | + settings = @Setting(name=DELAY_CDI_ACCESS, value = "true"), |
| 46 | + resolvableSettings = @ServiceRegistry.ResolvableSetting( |
| 47 | + settingName = CDI_BEAN_MANAGER, |
| 48 | + resolver = CdiContainerLinker.StandardResolver.class |
| 49 | + ) |
| 50 | + ) |
| 51 | + @DomainModel(annotatedClasses = TheEntity.class) |
| 52 | + @SessionFactory |
| 53 | + public void testIt(CdiContainerScope containerScope, SessionFactoryScope factoryScope) { |
| 54 | + // The CDI bean should _not_ have been built immediately... |
| 55 | + assertFalse( MonitorBean.wasInstantiated() ); |
| 56 | + assertEquals( 0, MonitorBean.currentFromDbCount() ); |
| 57 | + assertEquals( 0, MonitorBean.currentToDbCount() ); |
75 | 58 |
|
76 | | - // The CDI bean should have been built on first use |
77 | | - assertTrue( MonitorBean.wasInstantiated() ); |
78 | | - assertEquals( 0, MonitorBean.currentFromDbCount() ); |
79 | | - assertEquals( 1, MonitorBean.currentToDbCount() ); |
| 59 | + factoryScope.inTransaction( (session) -> { |
| 60 | + session.persist( new TheEntity( 1, "me", 5 ) ); |
| 61 | + } ); |
80 | 62 |
|
81 | | - inTransaction( |
82 | | - sessionFactory, |
83 | | - session -> { |
84 | | - TheEntity it = session.find( TheEntity.class, 1 ); |
85 | | - assertNotNull( it ); |
86 | | - } |
87 | | - ); |
| 63 | + // The CDI bean should have been built on first use |
| 64 | + assertTrue( MonitorBean.wasInstantiated() ); |
| 65 | + assertEquals( 0, MonitorBean.currentFromDbCount() ); |
| 66 | + assertEquals( 1, MonitorBean.currentToDbCount() ); |
88 | 67 |
|
89 | | - assertEquals( 1, MonitorBean.currentFromDbCount() ); |
90 | | - assertEquals( 1, MonitorBean.currentToDbCount() ); |
91 | | - } |
92 | | - finally { |
93 | | - inTransaction( |
94 | | - sessionFactory, |
95 | | - session -> { |
96 | | - session.createQuery( "delete TheEntity" ).executeUpdate(); |
97 | | - } |
98 | | - ); |
| 68 | + factoryScope.inTransaction( (session) -> { |
| 69 | + TheEntity it = session.find( TheEntity.class, 1 ); |
| 70 | + assertNotNull( it ); |
| 71 | + } ); |
99 | 72 |
|
100 | | - sessionFactory.close(); |
101 | | - } |
102 | | - } |
| 73 | + assertEquals( 1, MonitorBean.currentFromDbCount() ); |
| 74 | + assertEquals( 1, MonitorBean.currentToDbCount() ); |
103 | 75 | } |
104 | 76 | } |
0 commit comments