Polishing

This commit is contained in:
Juergen Hoeller
2014-11-01 13:00:54 +01:00
parent fde0713a94
commit f691618967
30 changed files with 151 additions and 163 deletions

View File

@@ -68,7 +68,7 @@ import static org.mockito.BDDMockito.*;
* @author Phillip Webb
* @since 05.03.2005
*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public class HibernateTransactionManagerTests {
@After

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -123,8 +123,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
newTony.getDriversLicense().getSerialNumber();
}
finally {
deleteFromTables(new String[] { "person", "drivers_license" });
//setComplete();
deleteFromTables("person", "drivers_license");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,9 +87,11 @@ public abstract class AbstractEntityManagerFactoryIntegrationTests extends Abstr
protected int countRowsInTable(EntityManager em, String tableName) {
Query query = em.createNativeQuery("SELECT COUNT(0) FROM " + tableName);
return ((Number) query.getSingleResult()).intValue();
};
}
public enum Provider {
ECLIPSELINK, HIBERNATE, OPENJPA
}

View File

@@ -121,15 +121,13 @@ public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEnt
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back",
1, countRowsInTable(em, "person"));
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
startNewTransaction();
em.joinTransaction();
deleteAllPeopleUsingEntityManager(em);
assertEquals("People have been killed",
0, countRowsInTable(em, "person"));
assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
setComplete();
}
@@ -142,8 +140,7 @@ public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEnt
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction(); // Should rollback
assertEquals("Tx must have been rolled back",
0, countRowsInTable(em, "person"));
assertEquals("Tx must have been rolled back", 0, countRowsInTable(em, "person"));
}
public void testCommitOccurs() {
@@ -153,11 +150,10 @@ public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEnt
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back",
1, countRowsInTable(em, "person"));
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
deleteFromTables(new String[] { "person" });
deleteFromTables("person");
}
}

View File

@@ -104,8 +104,7 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
}
public void doInstantiateAndSave(EntityManager em) {
assertEquals("Should be no people from previous transactions",
0, countRowsInTable(em, "person"));
assertEquals("Should be no people from previous transactions", 0, countRowsInTable(em, "person"));
Person p = new Person();
p.setFirstName("Tony");
@@ -131,19 +130,17 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back",
1, countRowsInTable(em, "person"));
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
deleteFromTables(new String[] { "person" });
deleteFromTables("person");
}
public void testRollbackOccurs() {
EntityManager em = createContainerManagedEntityManager();
doInstantiateAndSave(em);
endTransaction(); // Should rollback
assertEquals("Tx must have been rolled back",
0, countRowsInTable(em, "person"));
assertEquals("Tx must have been rolled back", 0, countRowsInTable(em, "person"));
}
public void testCommitOccurs() {
@@ -151,11 +148,10 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
doInstantiateAndSave(em);
setComplete();
endTransaction(); // Should rollback
assertEquals("Tx must have committed back",
1, countRowsInTable(em, "person"));
assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
// Now clean up the database
deleteFromTables(new String[] { "person" });
deleteFromTables("person");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -50,13 +50,13 @@ import static org.mockito.BDDMockito.*;
*/
public class JpaTransactionManagerTests {
private EntityManagerFactory factory;
private EntityManager manager;
private EntityTransaction tx;
private EntityManagerFactory factory;
private JpaTransactionManager transactionManager;
private JpaTransactionManager tm;
private TransactionTemplate tt;
@@ -67,8 +67,8 @@ public class JpaTransactionManagerTests {
manager = mock(EntityManager.class);
tx = mock(EntityTransaction.class);
transactionManager = new JpaTransactionManager(factory);
tt = new TransactionTemplate(transactionManager);
tm = new JpaTransactionManager(factory);
tt = new TransactionTemplate(tm);
given(factory.createEntityManager()).willReturn(manager);
given(manager.getTransaction()).willReturn(tx);
@@ -83,6 +83,7 @@ public class JpaTransactionManagerTests {
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}
@Test
public void testTransactionCommit() {
given(manager.getTransaction()).willReturn(tx);
@@ -456,7 +457,7 @@ public class JpaTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertFalse(TransactionSynchronizationManager.hasResource(factory));
TransactionTemplate tt2 = new TransactionTemplate(transactionManager);
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
return tt2.execute(new TransactionCallback() {
@Override
@@ -497,7 +498,7 @@ public class JpaTransactionManagerTests {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
assertTrue(TransactionSynchronizationManager.hasResource(factory));
TransactionTemplate tt2 = new TransactionTemplate(transactionManager);
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
return tt2.execute(new TransactionCallback() {
@Override
@@ -660,7 +661,6 @@ public class JpaTransactionManagerTests {
@Test
public void testTransactionRollbackWithPrebound() {
given(manager.getTransaction()).willReturn(tx);
given(tx.isActive()).willReturn(true);
@@ -694,7 +694,6 @@ public class JpaTransactionManagerTests {
@Test
public void testTransactionCommitWithPreboundAndPropagationSupports() {
final List<String> l = new ArrayList<String>();
l.add("test");
@@ -730,7 +729,6 @@ public class JpaTransactionManagerTests {
@Test
public void testTransactionRollbackWithPreboundAndPropagationSupports() {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
@@ -765,7 +763,7 @@ public class JpaTransactionManagerTests {
@Test
public void testTransactionCommitWithDataSource() throws SQLException {
DataSource ds = mock(DataSource.class);
transactionManager.setDataSource(ds);
tm.setDataSource(ds);
given(manager.getTransaction()).willReturn(tx);

View File

@@ -38,8 +38,7 @@ public class SharedEntityManagerCreatorTests {
EntityManagerFactory emf = mock(EntityManagerFactory.class,
withSettings().extraInterfaces(EntityManagerFactoryInfo.class));
// EntityManagerFactoryInfo.getEntityManagerInterface returns null
assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf),
is(notNullValue()));
assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf), is(notNullValue()));
}
@Test(expected = TransactionRequiredException.class)

View File

@@ -2,7 +2,7 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
@@ -11,13 +11,13 @@
<bean class="org.springframework.orm.jpa.support.PersistenceInjectionTests$DefaultPublicPersistenceContextSetter"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
<property name="targetObject" ref="dao"/>
<property name="targetMethod" value="toString"/>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
<property name="targetObject" ref="dao"/>
<property name="targetMethod" value="toString"/>
</bean>
</beans>

View File

@@ -51,11 +51,6 @@ public class PersistenceInjectionIntegrationTests extends AbstractEntityManagerF
assertNotNull("Default PersistenceContext Setter was injected", injectedEm);
}
public void testInjectedEntityManagerImplmentsPortableEntityManagerPlus() {
EntityManager injectedEm = defaultSetterInjected.getEntityManager();
assertNotNull("Default PersistenceContext Setter was injected", injectedEm);
}
public void testSetterInjectionOfNamedPersistenceContext() {
assertNotNull("Named PersistenceContext Setter was injected", namedSetterInjected.getEntityManagerFactory());
}

View File

@@ -330,8 +330,9 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
assertSame(mockEmf2, bean2.emf);
}
@Test
@Ignore
public void ignoreTestPersistenceUnitsFromJndi() {
public void testPersistenceUnitsFromJndi() {
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager()).willReturn(mockEm);
@@ -542,9 +543,9 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
@Test
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
babpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
pabpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this field");
}
catch (IllegalStateException ex) {
@@ -554,9 +555,9 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
@Test
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
babpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
pabpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
@@ -566,9 +567,9 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
@Test
public void testSetterWithNoArgs() {
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
babpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), "bean");
pabpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
@@ -576,28 +577,28 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
}
}
@Ignore
public void ignoreTestNoPropertiesPassedIn() {
@Test
public void testNoPropertiesPassedIn() {
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager()).willReturn(mockEm);
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldExtended dppcf = new DefaultPrivatePersistenceContextFieldExtended();
babpp.postProcessAfterInstantiation(dppcf, "bean name does not matter");
pabpp.postProcessPropertyValues(null, null, dppcf, "bean");
assertNotNull(dppcf.em);
}
@Ignore
public void ignoreTestPropertiesPassedIn() {
@Test
public void testPropertiesPassedIn() {
Properties props = new Properties();
props.put("foo", "bar");
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager(props)).willReturn(mockEm);
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldExtendedWithProps dppcf =
new DefaultPrivatePersistenceContextFieldExtendedWithProps();
babpp.postProcessAfterInstantiation(dppcf, "bean name does not matter");
pabpp.postProcessPropertyValues(null, null, dppcf, "bean");
assertNotNull(dppcf.em);
}
@@ -610,10 +611,10 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
given(em.getDelegate()).willReturn(new Object());
given(em.isOpen()).willReturn(true);
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
new DefaultPrivatePersistenceContextFieldWithProperties();
babpp.postProcessPropertyValues(null, null, transactionalField, "bean");
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean");
assertNotNull(transactionalField.em);
assertNotNull(transactionalField.em.getDelegate());
@@ -635,13 +636,13 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
given(em.getDelegate()).willReturn(new Object());
given(em.isOpen()).willReturn(true);
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
babpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
assertNotNull(transactionalFieldWithProperties.em);
assertNotNull(transactionalField.em);
@@ -668,13 +669,13 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
given(em.getDelegate()).willReturn(new Object(), 2);
given(em.isOpen()).willReturn(true);
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
babpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
assertNotNull(transactionalFieldWithProperties.em);
assertNotNull(transactionalField.em);

View File

@@ -48,8 +48,7 @@ import org.springframework.test.jdbc.JdbcTestUtils;
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class AbstractTransactionalDataSourceSpringContextTests
extends AbstractTransactionalSpringContextTests {
public abstract class AbstractTransactionalDataSourceSpringContextTests extends AbstractTransactionalSpringContextTests {
protected JdbcTemplate jdbcTemplate;
@@ -105,11 +104,11 @@ public abstract class AbstractTransactionalDataSourceSpringContextTests
* {@code setComplete()} impossible.
* @see #setComplete
*/
protected void deleteFromTables(String[] names) {
for (int i = 0; i < names.length; i++) {
int rowCount = this.jdbcTemplate.update("DELETE FROM " + names[i]);
protected void deleteFromTables(String... names) {
for (String name : names) {
int rowCount = this.jdbcTemplate.update("DELETE FROM " + name);
if (logger.isInfoEnabled()) {
logger.info("Deleted " + rowCount + " rows from table " + names[i]);
logger.info("Deleted " + rowCount + " rows from table " + name);
}
}
this.zappedTables = true;