|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -51,10 +51,6 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans |
|
|
|
private static final String SUE = "sue"; |
|
|
|
private static final String SUE = "sue"; |
|
|
|
private static final String YODA = "yoda"; |
|
|
|
private static final String YODA = "yoda"; |
|
|
|
|
|
|
|
|
|
|
|
private boolean beanInitialized = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String beanName = "replace me with [" + getClass().getName() + "]"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Employee employee; |
|
|
|
private Employee employee; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
@ -68,54 +64,86 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans |
|
|
|
|
|
|
|
|
|
|
|
private String bar; |
|
|
|
private String bar; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String beanName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean beanInitialized = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private final void setEmployee(final Employee employee) { |
|
|
|
private void setEmployee(Employee employee) { |
|
|
|
this.employee = employee; |
|
|
|
this.employee = employee; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
@Resource |
|
|
|
private final void setBar(final String bar) { |
|
|
|
private void setBar(String bar) { |
|
|
|
this.bar = bar; |
|
|
|
this.bar = bar; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final void setBeanName(final String beanName) { |
|
|
|
public void setBeanName(String beanName) { |
|
|
|
this.beanName = beanName; |
|
|
|
this.beanName = beanName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final void afterPropertiesSet() throws Exception { |
|
|
|
public void afterPropertiesSet() { |
|
|
|
this.beanInitialized = true; |
|
|
|
this.beanInitialized = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
|
|
|
public void setUp() { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table before a test method.", |
|
|
|
|
|
|
|
(inTransaction() ? 2 : 1), countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
|
|
|
public void tearDown() { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table after a test method.", |
|
|
|
|
|
|
|
(inTransaction() ? 4 : 1), countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeTransaction |
|
|
|
|
|
|
|
public void beforeTransaction() { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table before a transactional test method.", |
|
|
|
|
|
|
|
1, countRowsInPersonTable()); |
|
|
|
|
|
|
|
assertEquals("Adding yoda", 1, addPerson(YODA)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@AfterTransaction |
|
|
|
|
|
|
|
public void afterTransaction() { |
|
|
|
|
|
|
|
assertEquals("Deleting yoda", 1, deletePerson(YODA)); |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table after a transactional test method.", |
|
|
|
|
|
|
|
1, countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyApplicationContext() { |
|
|
|
public void verifyBeanNameSet() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.", |
|
|
|
assertTrue("The bean name of this test instance should have been set to the fully qualified class name " + |
|
|
|
super.applicationContext); |
|
|
|
"due to BeanNameAware semantics.", this.beanName.startsWith(getClass().getName())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyBeanInitialized() { |
|
|
|
public void verifyApplicationContext() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertTrue("This test bean should have been initialized due to InitializingBean semantics.", |
|
|
|
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.", |
|
|
|
this.beanInitialized); |
|
|
|
super.applicationContext); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyBeanNameSet() { |
|
|
|
public void verifyBeanInitialized() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertEquals("The bean name of this test instance should have been set to the fully qualified class name " |
|
|
|
assertTrue("This test bean should have been initialized due to InitializingBean semantics.", |
|
|
|
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName); |
|
|
|
this.beanInitialized); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyAnnotationAutowiredFields() { |
|
|
|
public void verifyAnnotationAutowiredFields() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong); |
|
|
|
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong); |
|
|
|
assertNotNull("The pet field should have been autowired.", this.pet); |
|
|
|
assertNotNull("The pet field should have been autowired.", this.pet); |
|
|
|
@ -124,7 +152,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyAnnotationAutowiredMethods() { |
|
|
|
public void verifyAnnotationAutowiredMethods() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertNotNull("The employee setter method should have been autowired.", this.employee); |
|
|
|
assertNotNull("The employee setter method should have been autowired.", this.employee); |
|
|
|
assertEquals("John Smith", this.employee.getName()); |
|
|
|
assertEquals("John Smith", this.employee.getName()); |
|
|
|
@ -132,58 +160,33 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyResourceAnnotationWiredFields() { |
|
|
|
public void verifyResourceAnnotationWiredFields() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo); |
|
|
|
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
|
|
|
public final void verifyResourceAnnotationWiredMethods() { |
|
|
|
public void verifyResourceAnnotationWiredMethods() { |
|
|
|
assertInTransaction(false); |
|
|
|
assertInTransaction(false); |
|
|
|
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar); |
|
|
|
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@BeforeTransaction |
|
|
|
|
|
|
|
public void beforeTransaction() { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1, |
|
|
|
|
|
|
|
countRowsInPersonTable()); |
|
|
|
|
|
|
|
assertEquals("Adding yoda", 1, addPerson(YODA)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
|
|
|
public void setUp() throws Exception { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table before a test method.", |
|
|
|
|
|
|
|
(inTransaction() ? 2 : 1), countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void modifyTestDataWithinTransaction() { |
|
|
|
public void modifyTestDataWithinTransaction() { |
|
|
|
assertInTransaction(true); |
|
|
|
assertInTransaction(true); |
|
|
|
assertEquals("Adding jane", 1, addPerson(JANE)); |
|
|
|
assertEquals("Adding jane", 1, addPerson(JANE)); |
|
|
|
assertEquals("Adding sue", 1, addPerson(SUE)); |
|
|
|
assertEquals("Adding sue", 1, addPerson(SUE)); |
|
|
|
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4, |
|
|
|
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", |
|
|
|
countRowsInPersonTable()); |
|
|
|
4, countRowsInPersonTable()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
|
|
|
public void tearDown() throws Exception { |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table after a test method.", |
|
|
|
|
|
|
|
(inTransaction() ? 4 : 1), countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@AfterTransaction |
|
|
|
|
|
|
|
public void afterTransaction() { |
|
|
|
|
|
|
|
assertEquals("Deleting yoda", 1, deletePerson(YODA)); |
|
|
|
|
|
|
|
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1, |
|
|
|
|
|
|
|
countRowsInPersonTable()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int addPerson(final String name) { |
|
|
|
private int addPerson(String name) { |
|
|
|
return super.jdbcTemplate.update("INSERT INTO person VALUES(?)", name); |
|
|
|
return super.jdbcTemplate.update("INSERT INTO person VALUES(?)", name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int deletePerson(final String name) { |
|
|
|
private int deletePerson(String name) { |
|
|
|
return super.jdbcTemplate.update("DELETE FROM person WHERE name=?", name); |
|
|
|
return super.jdbcTemplate.update("DELETE FROM person WHERE name=?", name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|