DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties = new DefaultPrivatePersistenceContextFieldWithProperties(); DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
@Test public void testPropertiesForSharedEntityManager2() { Properties props = new Properties(); props.put("foo", "bar"); EntityManager em = mock(EntityManager.class); // only one call made - the first EM definition wins (in this case the one w/o the properties) given(mockEmf.createEntityManager()).willReturn(em); given(em.getDelegate()).willReturn(new Object(), 2); given(em.isOpen()).willReturn(true); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties = new DefaultPrivatePersistenceContextFieldWithProperties(); DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField(); pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1"); pabpp.postProcessProperties(null, transactionalField, "bean2"); assertNotNull(transactionalFieldWithProperties.em); assertNotNull(transactionalField.em); // the EM w/o properties will be created assertNotNull(transactionalField.em.getDelegate()); // bind em to the thread now since it's created try { TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em)); assertNotNull(transactionalFieldWithProperties.em.getDelegate()); verify(em).close(); } finally { TransactionSynchronizationManager.unbindResource(mockEmf); } }
@Test public void testInjectionIntoExistingObjects() { EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(mockEm); GenericApplicationContext gac = new GenericApplicationContext(); gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf); gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class)); gac.refresh(); DefaultPrivatePersistenceContextField existingBean1 = new DefaultPrivatePersistenceContextField(); gac.getAutowireCapableBeanFactory().autowireBean(existingBean1); assertNotNull(existingBean1.em); DefaultPublicPersistenceContextSetter existingBean2 = new DefaultPublicPersistenceContextSetter(); gac.getAutowireCapableBeanFactory().autowireBean(existingBean2); assertNotNull(existingBean2.em); }