@Deprecated @Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { return postProcessProperties(pvs, bean, beanName); }
@Deprecated @Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { return postProcessProperties(pvs, bean, beanName); }
@Test public void testPropertiesPassedIn() { Properties props = new Properties(); props.put("foo", "bar"); EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager(props)).willReturn(mockEm); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldExtendedWithProps dppcf = new DefaultPrivatePersistenceContextFieldExtendedWithProps(); pabpp.postProcessProperties(null, dppcf, "bean"); assertNotNull(dppcf.em); }
@Test public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"); fail("Can't inject this field"); } catch (IllegalStateException ex) { // Ok } }
@Test public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"); fail("Can't inject this setter"); } catch (IllegalStateException ex) { // Ok } }
@Test public void testSetterWithNoArgs() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new SetterWithNoArgs(), "bean"); fail("Can't inject this setter"); } catch (IllegalStateException ex) { // Ok } }
@Test public void testNoPropertiesPassedIn() { EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(mockEm); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldExtended dppcf = new DefaultPrivatePersistenceContextFieldExtended(); pabpp.postProcessProperties(null, dppcf, "bean"); assertNotNull(dppcf.em); }
@Test public void testPropertiesForTransactionalEntityManager() { Properties props = new Properties(); props.put("foo", "bar"); EntityManager em = mock(EntityManager.class); given(mockEmf.createEntityManager(props)).willReturn(em); given(em.getDelegate()).willReturn(new Object()); given(em.isOpen()).willReturn(true); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldWithProperties transactionalField = new DefaultPrivatePersistenceContextFieldWithProperties(); pabpp.postProcessProperties(null, transactionalField, "bean"); assertNotNull(transactionalField.em); assertNotNull(transactionalField.em.getDelegate()); verify(em).close(); }
@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); } }
@Before public void setup() { factory = mock(EntityManagerFactory.class); manager = mock(EntityManager.class); tx = mock(EntityTransaction.class); JpaTransactionManager tm = new JpaTransactionManager(factory); tt = new TransactionTemplate(tm); given(factory.createEntityManager()).willReturn(manager); given(manager.getTransaction()).willReturn(tx); given(manager.isOpen()).willReturn(true); bean = new EntityManagerHoldingBean(); @SuppressWarnings("serial") PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() { @Override protected EntityManagerFactory findEntityManagerFactory(@Nullable String unitName, String requestingBeanName) { return factory; } }; pabpp.postProcessProperties(null, bean, "bean"); assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); }
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField(); pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1"); pabpp.postProcessProperties(null, transactionalField, "bean2");