/
CdiBeanResolutionIT.java
277 lines (230 loc) · 7.92 KB
/
CdiBeanResolutionIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Hibernate Search, full-text search for your domain model
*
* 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.search.integrationtest.mapper.orm.cdi;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.search.engine.environment.bean.BeanHolder;
import org.hibernate.search.engine.environment.bean.BeanReference;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.test.annotation.PortedFromSearch5;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.hibernate.search.util.impl.test.rule.StaticCounters;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.assertj.core.api.InstanceOfAssertFactories;
/**
* Check that Hibernate Search will be able to get CDI beans from the BeanResolver thanks to the ORM integration,
* when CDI is enabled in Hibernate ORM.
* <p>
* We test this by retrieving custom beans from the backend mock,
* then checking the CDI-defined hooks (@PostConstruct and @PreDestroy) have been called
* exactly as many times as expected.
*/
@TestForIssue(jiraKey = { "HSEARCH-1316", "HSEARCH-3171" })
@PortedFromSearch5(original = {
"org.hibernate.search.test.integration.wildfly.cdi.CDIInjectionIT",
"org.hibernate.search.test.integration.wildfly.cdi.CDIInjectionLifecycleEventsIT"
})
public class CdiBeanResolutionIT {
@Rule
public BackendMock backendMock = new BackendMock();
@Rule
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
@Rule
public StaticCounters counters = new StaticCounters();
private SeContainer cdiContainer;
@Before
public void setup() {
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
.disableDiscovery()
.addBeanClasses(
InjectedBean.class,
UnnamedSingletonBean.class,
NamedSingletonBean.class,
UnnamedDependentBean.class,
NamedDependentBean.class
);
this.cdiContainer = cdiInitializer.initialize();
}
@After
public void tearDown() {
if ( cdiContainer != null ) {
cdiContainer.close();
}
}
@Test
public void singleton_byType() {
doTest(
ExpectedScope.SINGLETON, UnnamedSingletonBean.KEYS,
BeanReference.of( UnnamedSingletonBean.class )
);
}
@Test
public void singleton_byName() {
doTest(
ExpectedScope.SINGLETON, NamedSingletonBean.KEYS,
BeanReference.of( InterfaceDefinedByMapper.class, NamedSingletonBean.NAME )
);
}
@Test
public void dependent_byType() {
doTest(
ExpectedScope.DEPENDENT, UnnamedDependentBean.KEYS,
BeanReference.of( UnnamedDependentBean.class )
);
}
@Test
public void dependent_byName() {
doTest(
ExpectedScope.DEPENDENT, NamedDependentBean.KEYS,
BeanReference.of( InterfaceDefinedByMapper.class, NamedDependentBean.NAME )
);
}
private <T> void doTest(ExpectedScope expectedScope, CounterKeys counterKeys, BeanReference<T> reference) {
List<BeanHolder<T>> retrievedBeans = new ArrayList<>();
backendMock.onCreate( context -> {
BeanHolder<T> retrievedBean1 = context.beanResolver().resolve( reference );
retrievedBeans.add( retrievedBean1 );
BeanHolder<T> retrievedBean2 = context.beanResolver().resolve( reference );
retrievedBeans.add( retrievedBean2 );
} );
backendMock.onStop( () -> {
for ( BeanHolder<T> retrievedBean : retrievedBeans ) {
retrievedBean.close();
}
} );
backendMock.expectAnySchema( IndexedEntity.NAME );
int expectedInstances = ExpectedScope.SINGLETON.equals( expectedScope ) ? 1 : 2;
try ( @SuppressWarnings("unused") SessionFactory sessionFactory = ormSetupHelper.start()
.withProperty( AvailableSettings.JAKARTA_CDI_BEAN_MANAGER, cdiContainer.getBeanManager() )
.setup( IndexedEntity.class ) ) {
backendMock.verifyExpectationsMet();
assertThat( retrievedBeans )
.hasSize( 2 )
.doesNotContainNull()
.extracting( BeanHolder::get )
.doesNotContainNull()
// All created beans should have been injected
.allSatisfy( bean -> assertThat( bean )
.asInstanceOf( InstanceOfAssertFactories.type( AbstractBeanBase.class ) )
.extracting( "injectedBean" )
.isNotNull()
);
assertThat( StaticCounters.get().get( counterKeys.instantiate ) ).isEqualTo( expectedInstances );
assertThat( StaticCounters.get().get( counterKeys.postConstruct ) ).isEqualTo( expectedInstances );
assertThat( StaticCounters.get().get( counterKeys.preDestroy ) ).isEqualTo( 0 );
}
switch ( expectedScope ) {
case DEPENDENT:
// Since we closed the BeanHolders when the backend was stopped,
// @PreDestroy should now have been executed on each instance.
assertThat( StaticCounters.get().get( counterKeys.preDestroy ) ).isEqualTo( expectedInstances );
break;
case SINGLETON:
default:
assertThat( StaticCounters.get().get( counterKeys.preDestroy ) ).isEqualTo( 0 );
break;
}
}
@Entity(name = IndexedEntity.NAME)
@Indexed(index = IndexedEntity.NAME)
public static final class IndexedEntity {
static final String NAME = "indexed";
@Id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
public interface InterfaceDefinedByMapper {
}
@Dependent
public static class InjectedBean {
}
public abstract static class AbstractBeanBase
implements InterfaceDefinedByMapper {
@Inject
private InjectedBean injectedBean;
protected AbstractBeanBase() {
StaticCounters.get().increment( getCounterKeys().instantiate );
}
@PostConstruct
public void postConstruct() {
StaticCounters.get().increment( getCounterKeys().postConstruct );
}
@PreDestroy
public void preDestroy() {
StaticCounters.get().increment( getCounterKeys().preDestroy );
}
protected abstract CounterKeys getCounterKeys();
}
@Dependent
public static class UnnamedDependentBean extends AbstractBeanBase {
public static final CounterKeys KEYS = new CounterKeys();
@Override
protected CounterKeys getCounterKeys() {
return KEYS;
}
}
@Dependent
@Named(NamedDependentBean.NAME)
public static class NamedDependentBean extends AbstractBeanBase {
public static final String NAME = "NameOfDependentBean";
public static final CounterKeys KEYS = new CounterKeys();
@Override
protected CounterKeys getCounterKeys() {
return KEYS;
}
}
@Singleton
public static class UnnamedSingletonBean extends AbstractBeanBase {
public static final CounterKeys KEYS = new CounterKeys();
@Override
protected CounterKeys getCounterKeys() {
return KEYS;
}
}
@Singleton
@Named(NamedSingletonBean.NAME)
public static class NamedSingletonBean extends AbstractBeanBase {
public static final String NAME = "NameOfSingletonBean";
public static final CounterKeys KEYS = new CounterKeys();
@Override
protected CounterKeys getCounterKeys() {
return KEYS;
}
}
private static class CounterKeys {
final StaticCounters.Key instantiate = StaticCounters.createKey();
final StaticCounters.Key postConstruct = StaticCounters.createKey();
final StaticCounters.Key preDestroy = StaticCounters.createKey();
}
private enum ExpectedScope {
SINGLETON,
DEPENDENT
}
}