1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .util .HashMap ;
24
24
import java .util .Map ;
25
25
import java .util .Properties ;
26
-
27
26
import javax .persistence .EntityManager ;
28
27
import javax .persistence .EntityManagerFactory ;
29
28
import javax .persistence .PersistenceContext ;
34
33
import org .hibernate .ejb .HibernateEntityManager ;
35
34
import org .junit .Ignore ;
36
35
import org .junit .Test ;
36
+
37
37
import org .springframework .beans .factory .FactoryBean ;
38
38
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
39
39
import org .springframework .beans .factory .support .RootBeanDefinition ;
@@ -140,6 +140,26 @@ public void testPublicSpecificExtendedPersistenceContextSetter() throws Exceptio
140
140
verify (mockEm2 ).flush ();
141
141
}
142
142
143
+ @ Test
144
+ public void testInjectionIntoExistingObjects () {
145
+ EntityManager mockEm = mock (EntityManager .class );
146
+ given (mockEmf .createEntityManager ()).willReturn (mockEm );
147
+
148
+ GenericApplicationContext gac = new GenericApplicationContext ();
149
+ gac .getDefaultListableBeanFactory ().registerSingleton ("entityManagerFactory" , mockEmf );
150
+ gac .registerBeanDefinition ("annotationProcessor" ,
151
+ new RootBeanDefinition (PersistenceAnnotationBeanPostProcessor .class ));
152
+ gac .refresh ();
153
+
154
+ DefaultPrivatePersistenceContextField existingBean1 = new DefaultPrivatePersistenceContextField ();
155
+ gac .getAutowireCapableBeanFactory ().autowireBean (existingBean1 );
156
+ assertNotNull (existingBean1 .em );
157
+
158
+ DefaultPublicPersistenceContextSetter existingBean2 = new DefaultPublicPersistenceContextSetter ();
159
+ gac .getAutowireCapableBeanFactory ().autowireBean (existingBean2 );
160
+ assertNotNull (existingBean2 .em );
161
+ }
162
+
143
163
@ Test
144
164
public void testPublicExtendedPersistenceContextSetterWithSerialization () throws Exception {
145
165
DummyInvocationHandler ih = new DummyInvocationHandler ();
@@ -524,7 +544,7 @@ public void testSinglePersistenceContextFromJndi() {
524
544
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit () {
525
545
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor ();
526
546
try {
527
- babpp .postProcessPropertyValues (null , null , new FieldOfWrongTypeAnnotatedWithPersistenceUnit (), null );
547
+ babpp .postProcessPropertyValues (null , null , new FieldOfWrongTypeAnnotatedWithPersistenceUnit (), "bean" );
528
548
fail ("Can't inject this field" );
529
549
}
530
550
catch (IllegalStateException ex ) {
@@ -536,7 +556,7 @@ public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
536
556
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit () {
537
557
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor ();
538
558
try {
539
- babpp .postProcessPropertyValues (null , null , new SetterOfWrongTypeAnnotatedWithPersistenceUnit (), null );
559
+ babpp .postProcessPropertyValues (null , null , new SetterOfWrongTypeAnnotatedWithPersistenceUnit (), "bean" );
540
560
fail ("Can't inject this setter" );
541
561
}
542
562
catch (IllegalStateException ex ) {
@@ -548,7 +568,7 @@ public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
548
568
public void testSetterWithNoArgs () {
549
569
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor ();
550
570
try {
551
- babpp .postProcessPropertyValues (null , null , new SetterWithNoArgs (), null );
571
+ babpp .postProcessPropertyValues (null , null , new SetterWithNoArgs (), "bean" );
552
572
fail ("Can't inject this setter" );
553
573
}
554
574
catch (IllegalStateException ex ) {
@@ -593,7 +613,7 @@ public void testPropertiesForTransactionalEntityManager() {
593
613
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor ();
594
614
DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
595
615
new DefaultPrivatePersistenceContextFieldWithProperties ();
596
- babpp .postProcessPropertyValues (null , null , transactionalField , null );
616
+ babpp .postProcessPropertyValues (null , null , transactionalField , "bean" );
597
617
598
618
assertNotNull (transactionalField .em );
599
619
assertNotNull (transactionalField .em .getDelegate ());
@@ -620,8 +640,8 @@ public void testPropertiesForSharedEntityManager1() {
620
640
new DefaultPrivatePersistenceContextFieldWithProperties ();
621
641
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField ();
622
642
623
- babpp .postProcessPropertyValues (null , null , transactionalFieldWithProperties , null );
624
- babpp .postProcessPropertyValues (null , null , transactionalField , null );
643
+ babpp .postProcessPropertyValues (null , null , transactionalFieldWithProperties , "bean1" );
644
+ babpp .postProcessPropertyValues (null , null , transactionalField , "bean2" );
625
645
626
646
assertNotNull (transactionalFieldWithProperties .em );
627
647
assertNotNull (transactionalField .em );
@@ -653,8 +673,8 @@ public void testPropertiesForSharedEntityManager2() {
653
673
new DefaultPrivatePersistenceContextFieldWithProperties ();
654
674
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField ();
655
675
656
- babpp .postProcessPropertyValues (null , null , transactionalFieldWithProperties , null );
657
- babpp .postProcessPropertyValues (null , null , transactionalField , null );
676
+ babpp .postProcessPropertyValues (null , null , transactionalFieldWithProperties , "bean1" );
677
+ babpp .postProcessPropertyValues (null , null , transactionalField , "bean2" );
658
678
659
679
assertNotNull (transactionalFieldWithProperties .em );
660
680
assertNotNull (transactionalField .em );
0 commit comments