16
16
17
17
package org .springframework .orm .jpa .hibernate ;
18
18
19
+ import javax .persistence .AttributeConverter ;
20
+
19
21
import org .hibernate .SessionFactory ;
20
22
import org .hibernate .resource .beans .container .spi .BeanContainer ;
21
23
import org .hibernate .resource .beans .container .spi .ContainedBean ;
22
24
import org .hibernate .resource .beans .spi .BeanInstanceProducer ;
23
25
import org .hibernate .resource .beans .spi .ManagedBeanRegistry ;
24
26
import org .hibernate .service .ServiceRegistry ;
25
-
26
27
import org .junit .Test ;
27
28
29
+ import org .springframework .beans .factory .BeanCreationException ;
28
30
import org .springframework .beans .factory .annotation .Autowired ;
29
31
import org .springframework .context .ApplicationContext ;
30
32
import org .springframework .orm .jpa .AbstractEntityManagerFactoryIntegrationTests ;
31
- import org .springframework .orm .jpa .hibernate .beans .*;
33
+ import org .springframework .orm .jpa .hibernate .beans .BeanSource ;
34
+ import org .springframework .orm .jpa .hibernate .beans .MultiplePrototypesInSpringContextTestBean ;
35
+ import org .springframework .orm .jpa .hibernate .beans .NoDefinitionInSpringContextTestBean ;
36
+ import org .springframework .orm .jpa .hibernate .beans .SinglePrototypeInSpringContextTestBean ;
32
37
33
38
import static org .junit .Assert .*;
34
39
35
40
/**
36
41
* Hibernate-specific SpringBeanContainer integration tests.
37
42
*
38
43
* @author Yoann Rodiere
44
+ * @author Juergen Hoeller
39
45
*/
40
46
public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests
41
47
extends AbstractEntityManagerFactoryIntegrationTests {
@@ -51,9 +57,9 @@ protected String[] getConfigLocations() {
51
57
}
52
58
53
59
private ManagedBeanRegistry getManagedBeanRegistry () {
54
- SessionFactory sessionFactory = entityManagerFactory .unwrap ( SessionFactory .class );
60
+ SessionFactory sessionFactory = entityManagerFactory .unwrap (SessionFactory .class );
55
61
ServiceRegistry serviceRegistry = sessionFactory .getSessionFactoryOptions ().getServiceRegistry ();
56
- return serviceRegistry .requireService ( ManagedBeanRegistry .class );
62
+ return serviceRegistry .requireService (ManagedBeanRegistry .class );
57
63
}
58
64
59
65
private BeanContainer getBeanContainer () {
@@ -68,8 +74,7 @@ public void testCanRetrieveBeanByTypeWithJpaCompliantOptions() {
68
74
69
75
ContainedBean <SinglePrototypeInSpringContextTestBean > bean = beanContainer .getBean (
70
76
SinglePrototypeInSpringContextTestBean .class ,
71
- JpaLifecycleOptions .INSTANCE ,
72
- IneffectiveBeanInstanceProducer .INSTANCE
77
+ JpaLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
73
78
);
74
79
75
80
assertNotNull (bean );
@@ -85,8 +90,7 @@ public void testCanRetrieveBeanByNameWithJpaCompliantOptions() {
85
90
86
91
ContainedBean <MultiplePrototypesInSpringContextTestBean > bean = beanContainer .getBean (
87
92
"multiple-1" , MultiplePrototypesInSpringContextTestBean .class ,
88
- JpaLifecycleOptions .INSTANCE ,
89
- IneffectiveBeanInstanceProducer .INSTANCE
93
+ JpaLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
90
94
);
91
95
92
96
assertNotNull (bean );
@@ -103,8 +107,7 @@ public void testCanRetrieveBeanByTypeWithNativeOptions() {
103
107
104
108
ContainedBean <SinglePrototypeInSpringContextTestBean > bean = beanContainer .getBean (
105
109
SinglePrototypeInSpringContextTestBean .class ,
106
- NativeLifecycleOptions .INSTANCE ,
107
- IneffectiveBeanInstanceProducer .INSTANCE
110
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
108
111
);
109
112
110
113
assertNotNull (bean );
@@ -115,8 +118,7 @@ public void testCanRetrieveBeanByTypeWithNativeOptions() {
115
118
116
119
ContainedBean <SinglePrototypeInSpringContextTestBean > bean2 = beanContainer .getBean (
117
120
SinglePrototypeInSpringContextTestBean .class ,
118
- NativeLifecycleOptions .INSTANCE ,
119
- IneffectiveBeanInstanceProducer .INSTANCE
121
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
120
122
);
121
123
122
124
assertNotNull (bean2 );
@@ -133,8 +135,7 @@ public void testCanRetrieveBeanByNameWithNativeOptions() {
133
135
134
136
ContainedBean <MultiplePrototypesInSpringContextTestBean > bean = beanContainer .getBean (
135
137
"multiple-1" , MultiplePrototypesInSpringContextTestBean .class ,
136
- NativeLifecycleOptions .INSTANCE ,
137
- IneffectiveBeanInstanceProducer .INSTANCE
138
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
138
139
);
139
140
140
141
assertNotNull (bean );
@@ -145,8 +146,7 @@ public void testCanRetrieveBeanByNameWithNativeOptions() {
145
146
146
147
ContainedBean <MultiplePrototypesInSpringContextTestBean > bean2 = beanContainer .getBean (
147
148
"multiple-1" , MultiplePrototypesInSpringContextTestBean .class ,
148
- NativeLifecycleOptions .INSTANCE ,
149
- IneffectiveBeanInstanceProducer .INSTANCE
149
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
150
150
);
151
151
152
152
assertNotNull (bean2 );
@@ -164,8 +164,7 @@ public void testCanRetrieveFallbackBeanByTypeWithJpaCompliantOptions() {
164
164
165
165
ContainedBean <NoDefinitionInSpringContextTestBean > bean = beanContainer .getBean (
166
166
NoDefinitionInSpringContextTestBean .class ,
167
- JpaLifecycleOptions .INSTANCE ,
168
- fallbackProducer
167
+ JpaLifecycleOptions .INSTANCE , fallbackProducer
169
168
);
170
169
171
170
assertEquals (1 , fallbackProducer .currentUnnamedInstantiationCount ());
@@ -186,8 +185,7 @@ public void testCanRetrieveFallbackBeanByNameWithJpaCompliantOptions() {
186
185
187
186
ContainedBean <NoDefinitionInSpringContextTestBean > bean = beanContainer .getBean (
188
187
"some name" , NoDefinitionInSpringContextTestBean .class ,
189
- JpaLifecycleOptions .INSTANCE ,
190
- fallbackProducer
188
+ JpaLifecycleOptions .INSTANCE , fallbackProducer
191
189
);
192
190
193
191
assertEquals (0 , fallbackProducer .currentUnnamedInstantiationCount ());
@@ -209,8 +207,7 @@ public void testCanRetrieveFallbackBeanByTypeWithNativeOptions() {
209
207
210
208
ContainedBean <NoDefinitionInSpringContextTestBean > bean = beanContainer .getBean (
211
209
NoDefinitionInSpringContextTestBean .class ,
212
- NativeLifecycleOptions .INSTANCE ,
213
- fallbackProducer
210
+ NativeLifecycleOptions .INSTANCE , fallbackProducer
214
211
);
215
212
216
213
assertEquals (1 , fallbackProducer .currentUnnamedInstantiationCount ());
@@ -231,8 +228,7 @@ public void testCanRetrieveFallbackBeanByNameWithNativeOptions() {
231
228
232
229
ContainedBean <NoDefinitionInSpringContextTestBean > bean = beanContainer .getBean (
233
230
"some name" , NoDefinitionInSpringContextTestBean .class ,
234
- NativeLifecycleOptions .INSTANCE ,
235
- fallbackProducer
231
+ NativeLifecycleOptions .INSTANCE , fallbackProducer
236
232
);
237
233
238
234
assertEquals (0 , fallbackProducer .currentUnnamedInstantiationCount ());
@@ -246,11 +242,40 @@ public void testCanRetrieveFallbackBeanByNameWithNativeOptions() {
246
242
assertNull (instance .getApplicationContext ());
247
243
}
248
244
245
+ @ Test (expected = UnsupportedOperationException .class )
246
+ public void testFallbackExceptionInCaseOfNoSpringBeanFound () {
247
+ getBeanContainer ().getBean (NoDefinitionInSpringContextTestBean .class ,
248
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
249
+ );
250
+ }
251
+
252
+ @ Test (expected = BeanCreationException .class )
253
+ public void testOriginalExceptionInCaseOfFallbackProducerFailure () {
254
+ getBeanContainer ().getBean (AttributeConverter .class ,
255
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
256
+ );
257
+ }
258
+
259
+ @ Test (expected = UnsupportedOperationException .class )
260
+ public void testFallbackExceptionInCaseOfNoSpringBeanFoundByName () {
261
+ getBeanContainer ().getBean ("some name" , NoDefinitionInSpringContextTestBean .class ,
262
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
263
+ );
264
+ }
265
+
266
+ @ Test (expected = BeanCreationException .class )
267
+ public void testOriginalExceptionInCaseOfFallbackProducerFailureByName () {
268
+ getBeanContainer ().getBean ("invalid" , AttributeConverter .class ,
269
+ NativeLifecycleOptions .INSTANCE , IneffectiveBeanInstanceProducer .INSTANCE
270
+ );
271
+ }
272
+
249
273
250
274
/**
251
275
* The lifecycle options mandated by the JPA spec and used as a default in Hibernate ORM.
252
276
*/
253
277
private static class JpaLifecycleOptions implements BeanContainer .LifecycleOptions {
278
+
254
279
public static final JpaLifecycleOptions INSTANCE = new JpaLifecycleOptions ();
255
280
256
281
@ Override
@@ -264,12 +289,14 @@ public boolean useJpaCompliantCreation() {
264
289
}
265
290
}
266
291
292
+
267
293
/**
268
294
* The lifecycle options used by libraries integrating into Hibernate ORM
269
295
* and that want a behavior closer to Spring's native behavior,
270
296
* such as Hibernate Search.
271
297
*/
272
298
private static class NativeLifecycleOptions implements BeanContainer .LifecycleOptions {
299
+
273
300
public static final NativeLifecycleOptions INSTANCE = new NativeLifecycleOptions ();
274
301
275
302
@ Override
@@ -283,7 +310,9 @@ public boolean useJpaCompliantCreation() {
283
310
}
284
311
}
285
312
313
+
286
314
private static class IneffectiveBeanInstanceProducer implements BeanInstanceProducer {
315
+
287
316
public static final IneffectiveBeanInstanceProducer INSTANCE = new IneffectiveBeanInstanceProducer ();
288
317
289
318
@ Override
@@ -297,8 +326,11 @@ public <B> B produceBeanInstance(String s, Class<B> aClass) {
297
326
}
298
327
}
299
328
329
+
300
330
private static class NoDefinitionInSpringContextTestBeanInstanceProducer implements BeanInstanceProducer {
331
+
301
332
private int unnamedInstantiationCount = 0 ;
333
+
302
334
private int namedInstantiationCount = 0 ;
303
335
304
336
@ Override
@@ -341,4 +373,5 @@ private int currentNamedInstantiationCount() {
341
373
return namedInstantiationCount ;
342
374
}
343
375
}
376
+
344
377
}
0 commit comments