1212import org .hibernate .boot .registry .BootstrapServiceRegistry ;
1313import org .hibernate .boot .registry .BootstrapServiceRegistryBuilder ;
1414import org .hibernate .boot .registry .StandardServiceRegistry ;
15- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
16- import org .hibernate .cfg .AvailableSettings ;
15+ import org .hibernate .boot .spi .MetadataImplementor ;
1716import org .hibernate .engine .spi .SessionFactoryImplementor ;
17+ import org .hibernate .testing .orm .junit .BaseUnitTest ;
1818import org .hibernate .tool .schema .Action ;
1919
20- import org .hibernate .testing .junit4 .BaseUnitTestCase ;
2120import org .hibernate .testing .util .ServiceRegistryUtil ;
2221
2322import org .hibernate .orm .test .cdi .general .hibernatesearch .Monitor ;
3837import org .hibernate .orm .test .cdi .testsupport .TestingExtendedBeanManager ;
3938import org .junit .Test ;
4039
40+ import static org .hibernate .cfg .ManagedBeanSettings .JAKARTA_CDI_BEAN_MANAGER ;
41+ import static org .hibernate .cfg .SchemaToolingSettings .HBM2DDL_AUTO ;
4142import static org .junit .Assert .assertEquals ;
4243
4344/**
5758 *
5859 * @see HibernateSearchSimulatedIntegrator
5960 */
60- public class HibernateSearchExtendedCdiSupportTest extends BaseUnitTestCase {
61+ @ BaseUnitTest
62+ public class HibernateSearchExtendedCdiSupportTest {
6163 @ Test
6264 public void test () {
63- doTest ( TestingExtendedBeanManager .create () );
64- }
65-
66- private void doTest (TestingExtendedBeanManager beanManager ) {
6765 Monitor .reset ();
6866
69- final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer =
70- new TheFallbackBeanInstanceProducer ();
71- final HibernateSearchSimulatedIntegrator beanConsumingIntegrator =
72- new HibernateSearchSimulatedIntegrator ( fallbackBeanInstanceProducer );
73-
74- try (SessionFactoryImplementor sessionFactory = buildSessionFactory ( beanManager , beanConsumingIntegrator )) {
75- final SeContainerInitializer cdiInitializer = SeContainerInitializer .newInstance ()
76- .disableDiscovery ()
77- .addBeanClasses ( TheApplicationScopedBean .class )
78- .addBeanClasses ( TheNamedApplicationScopedBean .class , TheMainNamedApplicationScopedBeanImpl .class ,
79- TheAlternativeNamedApplicationScopedBeanImpl .class )
80- .addBeanClasses ( TheSharedApplicationScopedBean .class )
81- .addBeanClasses ( TheDependentBean .class )
82- .addBeanClasses ( TheNamedDependentBean .class , TheMainNamedDependentBeanImpl .class ,
83- TheAlternativeNamedDependentBeanImpl .class )
84- .addBeanClasses ( TheNestedDependentBean .class )
85- .addBeanClasses ( TheNonHibernateBeanConsumer .class );
86- try (final SeContainer cdiContainer = cdiInitializer .initialize ()) {
87- // Simulate CDI bean consumers outside of Hibernate ORM
88- Instance <TheNonHibernateBeanConsumer > nonHibernateBeanConsumerInstance =
89- cdiContainer .getBeanManager ().createInstance ().select ( TheNonHibernateBeanConsumer .class );
90- nonHibernateBeanConsumerInstance .get ();
91-
92- // Here, the HibernateSearchSimulatedIntegrator has just been integrated and has requested beans
93- // BUT it has not fetched instances of beans yet, so non-shared beans should not have been instantiated yet.
94- assertEquals ( 0 , Monitor .theApplicationScopedBean ().currentInstantiationCount () );
95- assertEquals ( 0 , Monitor .theMainNamedApplicationScopedBean ().currentInstantiationCount () );
96- assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentInstantiationCount () );
97- assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentInstantiationCount () );
98- assertEquals ( 0 , Monitor .theDependentBean ().currentInstantiationCount () );
99- assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentInstantiationCount () );
100- assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentInstantiationCount () );
101- assertEquals ( 0 , fallbackBeanInstanceProducer .currentInstantiationCount () );
102- assertEquals ( 0 , fallbackBeanInstanceProducer .currentNamedInstantiationCount () );
103- // Nested dependent bean: 1 instance per bean that depends on it
104- assertEquals ( 1 , Monitor .theNestedDependentBean ().currentInstantiationCount () );
105-
106- beanManager .notifyListenerReady ( cdiContainer .getBeanManager () );
107-
108- beanConsumingIntegrator .ensureInstancesInitialized ();
109-
110- // Here the HibernateSearchSimulatedIntegrator *did* fetch an instance of each bean,
111- // so all beans should have been instantiated.
112- // See HibernateSearchSimulatedIntegrator for a detailed list of requested beans
113-
114- // Application scope: maximum 1 instance as soon as at least one was requested
115- assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentInstantiationCount () );
116- assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentInstantiationCount () );
117- assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentInstantiationCount () );
118- assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentInstantiationCount () );
119-
120- // Dependent scope: 1 instance per bean we requested explicitly
121- assertEquals ( 2 , Monitor .theDependentBean ().currentInstantiationCount () );
122- assertEquals ( 2 , Monitor .theMainNamedDependentBean ().currentInstantiationCount () );
123- assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentInstantiationCount () );
124-
125- // Reflection-instantiated: 1 instance per bean we requested explicitly
126- assertEquals ( 2 , fallbackBeanInstanceProducer .currentInstantiationCount () );
127- assertEquals ( 2 , fallbackBeanInstanceProducer .currentNamedInstantiationCount () );
128-
129- // Nested dependent bean: 1 instance per bean that depends on it
130- assertEquals ( 7 , Monitor .theNestedDependentBean ().currentInstantiationCount () );
131-
132- // Expect one PostConstruct call per CDI bean instance
133- assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentPostConstructCount () );
134- assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentPostConstructCount () );
135- assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPostConstructCount () );
136- assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentPostConstructCount () );
137- assertEquals ( 2 , Monitor .theDependentBean ().currentPostConstructCount () );
138- assertEquals ( 2 , Monitor .theMainNamedDependentBean ().currentPostConstructCount () );
139- assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPostConstructCount () );
140- assertEquals ( 7 , Monitor .theNestedDependentBean ().currentPostConstructCount () );
141-
142- // Expect no PreDestroy call yet
143- assertEquals ( 0 , Monitor .theApplicationScopedBean ().currentPreDestroyCount () );
144- assertEquals ( 0 , Monitor .theMainNamedApplicationScopedBean ().currentPreDestroyCount () );
145- assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPreDestroyCount () );
146- assertEquals ( 0 , Monitor .theSharedApplicationScopedBean ().currentPreDestroyCount () );
147- assertEquals ( 0 , Monitor .theDependentBean ().currentPreDestroyCount () );
148- assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentPreDestroyCount () );
149- assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPreDestroyCount () );
150- assertEquals ( 0 , Monitor .theNestedDependentBean ().currentPreDestroyCount () );
151- }
67+ final TestingExtendedBeanManager extendedBeanManager = TestingExtendedBeanManager .create ();
15268
153- // After the CDI context has ended, PreDestroy should have been called on every "normal-scoped" CDI bean
154- // (i.e. all CDI beans excepting the dependent ones we requested explicitly and haven't released yet)
155- assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentPreDestroyCount () );
156- assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentPreDestroyCount () );
157- assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPreDestroyCount () );
158- assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentPreDestroyCount () );
159- assertEquals ( 0 , Monitor .theDependentBean ().currentPreDestroyCount () );
160- assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentPreDestroyCount () );
161- assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPreDestroyCount () );
162- assertEquals ( 3 , Monitor .theNestedDependentBean ().currentPreDestroyCount () );
69+ final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer = new TheFallbackBeanInstanceProducer ();
70+ final HibernateSearchSimulatedIntegrator beanConsumingIntegrator = new HibernateSearchSimulatedIntegrator ( fallbackBeanInstanceProducer );
71+
72+ try (BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder ()
73+ .applyIntegrator ( beanConsumingIntegrator )
74+ .build ()) {
75+
76+ try (StandardServiceRegistry ssr = ServiceRegistryUtil .serviceRegistryBuilder ( bsr )
77+ .applySetting ( HBM2DDL_AUTO , Action .CREATE_DROP )
78+ .applySetting ( JAKARTA_CDI_BEAN_MANAGER , extendedBeanManager )
79+ .build ()) {
80+
81+ final MetadataImplementor metadata = (MetadataImplementor ) new MetadataSources ( ssr )
82+ .addAnnotatedClass ( TheEntity .class )
83+ .buildMetadata ();
84+
85+ try (SessionFactoryImplementor sessionFactory = metadata .buildSessionFactory ()) {
86+ final SeContainerInitializer cdiInitializer = SeContainerInitializer .newInstance ()
87+ .disableDiscovery ()
88+ .addBeanClasses ( TheApplicationScopedBean .class )
89+ .addBeanClasses ( TheNamedApplicationScopedBean .class , TheMainNamedApplicationScopedBeanImpl .class ,
90+ TheAlternativeNamedApplicationScopedBeanImpl .class )
91+ .addBeanClasses ( TheSharedApplicationScopedBean .class )
92+ .addBeanClasses ( TheDependentBean .class )
93+ .addBeanClasses ( TheNamedDependentBean .class , TheMainNamedDependentBeanImpl .class ,
94+ TheAlternativeNamedDependentBeanImpl .class )
95+ .addBeanClasses ( TheNestedDependentBean .class )
96+ .addBeanClasses ( TheNonHibernateBeanConsumer .class );
97+ try (final SeContainer cdiContainer = cdiInitializer .initialize ()) {
98+ // Simulate CDI bean consumers outside of Hibernate ORM
99+ Instance <TheNonHibernateBeanConsumer > nonHibernateBeanConsumerInstance =
100+ cdiContainer .getBeanManager ().createInstance ().select ( TheNonHibernateBeanConsumer .class );
101+ nonHibernateBeanConsumerInstance .get ();
102+
103+ // Here, the HibernateSearchSimulatedIntegrator has just been integrated and has requested beans
104+ // BUT it has not fetched instances of beans yet, so non-shared beans should not have been instantiated yet.
105+ assertEquals ( 0 , Monitor .theApplicationScopedBean ().currentInstantiationCount () );
106+ assertEquals ( 0 , Monitor .theMainNamedApplicationScopedBean ().currentInstantiationCount () );
107+ assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentInstantiationCount () );
108+ assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentInstantiationCount () );
109+ assertEquals ( 0 , Monitor .theDependentBean ().currentInstantiationCount () );
110+ assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentInstantiationCount () );
111+ assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentInstantiationCount () );
112+ assertEquals ( 0 , fallbackBeanInstanceProducer .currentInstantiationCount () );
113+ assertEquals ( 0 , fallbackBeanInstanceProducer .currentNamedInstantiationCount () );
114+ // Nested dependent bean: 1 instance per bean that depends on it
115+ assertEquals ( 1 , Monitor .theNestedDependentBean ().currentInstantiationCount () );
116+
117+ extendedBeanManager .notifyListenerReady ( cdiContainer .getBeanManager () );
118+
119+ beanConsumingIntegrator .ensureInstancesInitialized ();
120+
121+ // Here the HibernateSearchSimulatedIntegrator *did* fetch an instance of each bean,
122+ // so all beans should have been instantiated.
123+ // See HibernateSearchSimulatedIntegrator for a detailed list of requested beans
124+
125+ // Application scope: maximum 1 instance as soon as at least one was requested
126+ assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentInstantiationCount () );
127+ assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentInstantiationCount () );
128+ assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentInstantiationCount () );
129+ assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentInstantiationCount () );
130+
131+ // Dependent scope: 1 instance per bean we requested explicitly
132+ assertEquals ( 2 , Monitor .theDependentBean ().currentInstantiationCount () );
133+ assertEquals ( 2 , Monitor .theMainNamedDependentBean ().currentInstantiationCount () );
134+ assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentInstantiationCount () );
135+
136+ // Reflection-instantiated: 1 instance per bean we requested explicitly
137+ assertEquals ( 2 , fallbackBeanInstanceProducer .currentInstantiationCount () );
138+ assertEquals ( 2 , fallbackBeanInstanceProducer .currentNamedInstantiationCount () );
139+
140+ // Nested dependent bean: 1 instance per bean that depends on it
141+ assertEquals ( 7 , Monitor .theNestedDependentBean ().currentInstantiationCount () );
142+
143+ // Expect one PostConstruct call per CDI bean instance
144+ assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentPostConstructCount () );
145+ assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentPostConstructCount () );
146+ assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPostConstructCount () );
147+ assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentPostConstructCount () );
148+ assertEquals ( 2 , Monitor .theDependentBean ().currentPostConstructCount () );
149+ assertEquals ( 2 , Monitor .theMainNamedDependentBean ().currentPostConstructCount () );
150+ assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPostConstructCount () );
151+ assertEquals ( 7 , Monitor .theNestedDependentBean ().currentPostConstructCount () );
152+
153+ // Expect no PreDestroy call yet
154+ assertEquals ( 0 , Monitor .theApplicationScopedBean ().currentPreDestroyCount () );
155+ assertEquals ( 0 , Monitor .theMainNamedApplicationScopedBean ().currentPreDestroyCount () );
156+ assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPreDestroyCount () );
157+ assertEquals ( 0 , Monitor .theSharedApplicationScopedBean ().currentPreDestroyCount () );
158+ assertEquals ( 0 , Monitor .theDependentBean ().currentPreDestroyCount () );
159+ assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentPreDestroyCount () );
160+ assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPreDestroyCount () );
161+ assertEquals ( 0 , Monitor .theNestedDependentBean ().currentPreDestroyCount () );
162+ }
163+
164+ // After the CDI context has ended, PreDestroy should have been called on every "normal-scoped" CDI bean
165+ // (i.e. all CDI beans excepting the dependent ones we requested explicitly and haven't released yet)
166+ assertEquals ( 1 , Monitor .theApplicationScopedBean ().currentPreDestroyCount () );
167+ assertEquals ( 1 , Monitor .theMainNamedApplicationScopedBean ().currentPreDestroyCount () );
168+ assertEquals ( 0 , Monitor .theAlternativeNamedApplicationScopedBean ().currentPreDestroyCount () );
169+ assertEquals ( 1 , Monitor .theSharedApplicationScopedBean ().currentPreDestroyCount () );
170+ assertEquals ( 0 , Monitor .theDependentBean ().currentPreDestroyCount () );
171+ assertEquals ( 0 , Monitor .theMainNamedDependentBean ().currentPreDestroyCount () );
172+ assertEquals ( 0 , Monitor .theAlternativeNamedDependentBean ().currentPreDestroyCount () );
173+ assertEquals ( 3 , Monitor .theNestedDependentBean ().currentPreDestroyCount () );
174+ }
175+ }
163176 }
164177
165178 // Here, the HibernateSearchSimulatedIntegrator has just been disintegrated and has released beans
@@ -174,27 +187,4 @@ private void doTest(TestingExtendedBeanManager beanManager) {
174187 assertEquals ( 7 , Monitor .theNestedDependentBean ().currentPreDestroyCount () );
175188 }
176189
177- private SessionFactoryImplementor buildSessionFactory (TestingExtendedBeanManager beanManager ,
178- HibernateSearchSimulatedIntegrator beanConsumingIntegrator ) {
179- BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder ()
180- .applyIntegrator ( beanConsumingIntegrator )
181- .build ();
182-
183- final StandardServiceRegistry ssr = ServiceRegistryUtil .serviceRegistryBuilder ( bsr )
184- .applySetting ( AvailableSettings .HBM2DDL_AUTO , Action .CREATE_DROP )
185- .applySetting ( AvailableSettings .CDI_BEAN_MANAGER , beanManager )
186- .build ();
187-
188- try {
189- return (SessionFactoryImplementor ) new MetadataSources ( ssr )
190- .addAnnotatedClass ( TheEntity .class )
191- .buildMetadata ()
192- .getSessionFactoryBuilder ()
193- .build ();
194- }
195- catch (Exception e ) {
196- StandardServiceRegistryBuilder .destroy ( ssr );
197- throw e ;
198- }
199- }
200190}
0 commit comments