[SPR-6184] added tests to verify support for @Configuration classes with TestNG; simplified existing TestNG tests using Spring 3.0 and 3.1 features.
This commit is contained in:
parent
522a879496
commit
174bf58308
|
|
@ -0,0 +1,154 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.test.context.testng;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||||
|
import static org.springframework.test.transaction.TransactionTestUtils.inTransaction;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.springframework.beans.Employee;
|
||||||
|
import org.springframework.beans.Pet;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.annotation.NotTransactional;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
import org.springframework.test.context.transaction.AfterTransaction;
|
||||||
|
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||||
|
import org.testng.annotations.AfterClass;
|
||||||
|
import org.testng.annotations.AfterMethod;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests that verify support for
|
||||||
|
* {@link import org.springframework.context.annotation.Configuration @Configuration}
|
||||||
|
* classes with TestNG-based tests.
|
||||||
|
*
|
||||||
|
* <p>Configuration will be loaded from
|
||||||
|
* {@link AnnotationConfigTransactionalTestNGSpringContextTestsConfig}.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 3.1
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class AnnotationConfigTransactionalTestNGSpringContextTests extends
|
||||||
|
AbstractTransactionalTestNGSpringContextTests {
|
||||||
|
|
||||||
|
private static final String JANE = "jane";
|
||||||
|
private static final String SUE = "sue";
|
||||||
|
private static final String YODA = "yoda";
|
||||||
|
|
||||||
|
private static final int NUM_TESTS = 2;
|
||||||
|
private static final int NUM_TX_TESTS = 1;
|
||||||
|
|
||||||
|
private static int numSetUpCalls = 0;
|
||||||
|
private static int numSetUpCallsInTransaction = 0;
|
||||||
|
private static int numTearDownCalls = 0;
|
||||||
|
private static int numTearDownCallsInTransaction = 0;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Employee employee;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Pet pet;
|
||||||
|
|
||||||
|
|
||||||
|
private int createPerson(String name) {
|
||||||
|
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int deletePerson(String name) {
|
||||||
|
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
|
||||||
|
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
|
||||||
|
+ testState + ").");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertAddPerson(final String name) {
|
||||||
|
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public void beforeClass() {
|
||||||
|
numSetUpCalls = 0;
|
||||||
|
numSetUpCallsInTransaction = 0;
|
||||||
|
numTearDownCalls = 0;
|
||||||
|
numTearDownCallsInTransaction = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public void afterClass() {
|
||||||
|
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
|
||||||
|
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
|
||||||
|
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
|
||||||
|
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@NotTransactional
|
||||||
|
public void autowiringFromConfigClass() {
|
||||||
|
assertNotNull("The employee should have been autowired.", employee);
|
||||||
|
assertEquals("John Smith", employee.getName());
|
||||||
|
|
||||||
|
assertNotNull("The pet should have been autowired.", pet);
|
||||||
|
assertEquals("Fido", pet.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeTransaction
|
||||||
|
public void beforeTransaction() {
|
||||||
|
assertNumRowsInPersonTable(1, "before a transactional test method");
|
||||||
|
assertAddPerson(YODA);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
numSetUpCalls++;
|
||||||
|
if (inTransaction()) {
|
||||||
|
numSetUpCallsInTransaction++;
|
||||||
|
}
|
||||||
|
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void modifyTestDataWithinTransaction() {
|
||||||
|
assertInTransaction(true);
|
||||||
|
assertAddPerson(JANE);
|
||||||
|
assertAddPerson(SUE);
|
||||||
|
assertNumRowsInPersonTable(4, "in modifyTestDataWithinTransaction()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterMethod
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
numTearDownCalls++;
|
||||||
|
if (inTransaction()) {
|
||||||
|
numTearDownCallsInTransaction++;
|
||||||
|
}
|
||||||
|
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterTransaction
|
||||||
|
public void afterTransaction() {
|
||||||
|
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
|
||||||
|
assertNumRowsInPersonTable(1, "after a transactional test method");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.test.context.testng;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.Employee;
|
||||||
|
import org.springframework.beans.Pet;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ApplicationContext configuration for {@link AnnotationConfigTransactionalTestNGSpringContextTests}.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 3.1
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class AnnotationConfigTransactionalTestNGSpringContextTestsConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Employee employee() {
|
||||||
|
Employee employee = new Employee();
|
||||||
|
employee.setName("John Smith");
|
||||||
|
employee.setAge(42);
|
||||||
|
employee.setCompany("Acme Widgets, Inc.");
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Pet pet() {
|
||||||
|
return new Pet("Fido");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PlatformTransactionManager transactionManager() {
|
||||||
|
return new DataSourceTransactionManager(dataSource());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return new EmbeddedDatabaseBuilder()//
|
||||||
|
.addScript("classpath:/org/springframework/test/context/testng/schema.sql")//
|
||||||
|
.addScript("classpath:/org/springframework/test/context/testng/data.sql")//
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,33 +1,25 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
|
||||||
|
|
||||||
<bean id="employee" class="org.springframework.beans.Employee">
|
<bean id="employee" class="org.springframework.beans.Employee" p:name="John Smith" p:age="42"
|
||||||
<property name="name" value="John Smith" />
|
p:company="Acme Widgets, Inc." />
|
||||||
<property name="age" value="42" />
|
|
||||||
<property name="company" value="Acme Widgets, Inc." />
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="pet" class="org.springframework.beans.Pet">
|
<bean id="pet" class="org.springframework.beans.Pet" c:_="Fido" />
|
||||||
<constructor-arg value="Fido" />
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="foo" class="java.lang.String">
|
<bean id="foo" class="java.lang.String" c:_="Foo" />
|
||||||
<constructor-arg value="Foo" />
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="bar" class="java.lang.String">
|
<bean id="bar" class="java.lang.String" c:_="Bar" />
|
||||||
<constructor-arg value="Bar" />
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
|
||||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
|
||||||
|
|
||||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||||
p:data-source-ref="dataSource" />
|
p:data-source-ref="dataSource" />
|
||||||
|
|
||||||
<bean id="databaseSetup"
|
<jdbc:embedded-database id="dataSource">
|
||||||
class="org.springframework.test.context.testng.ConcreteTransactionalTestNGSpringContextTests$DatabaseSetup" />
|
<jdbc:script location="classpath:/org/springframework/test/context/testng/schema.sql" />
|
||||||
|
<jdbc:script location="classpath:/org/springframework/test/context/testng/data.sql" />
|
||||||
|
</jdbc:embedded-database>
|
||||||
|
|
||||||
</beans>
|
</beans>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -24,20 +24,16 @@ import static org.testng.Assert.assertNull;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.springframework.beans.Employee;
|
import org.springframework.beans.Employee;
|
||||||
import org.springframework.beans.Pet;
|
import org.springframework.beans.Pet;
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.BadSqlGrammarException;
|
|
||||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
|
||||||
import org.springframework.test.annotation.NotTransactional;
|
import org.springframework.test.annotation.NotTransactional;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.transaction.AfterTransaction;
|
import org.springframework.test.context.transaction.AfterTransaction;
|
||||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||||
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
|
||||||
import org.testng.annotations.AfterClass;
|
import org.testng.annotations.AfterClass;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
|
|
@ -56,18 +52,18 @@ import org.testng.annotations.Test;
|
||||||
public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests
|
public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests
|
||||||
implements BeanNameAware, InitializingBean {
|
implements BeanNameAware, InitializingBean {
|
||||||
|
|
||||||
private static final String BOB = "bob";
|
|
||||||
private static final String JANE = "jane";
|
private static final String JANE = "jane";
|
||||||
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 static final int NUM_TESTS = 8;
|
||||||
|
private static final int NUM_TX_TESTS = 1;
|
||||||
|
|
||||||
private static int numSetUpCalls = 0;
|
private static int numSetUpCalls = 0;
|
||||||
private static int numSetUpCallsInTransaction = 0;
|
private static int numSetUpCallsInTransaction = 0;
|
||||||
private static int numTearDownCalls = 0;
|
private static int numTearDownCalls = 0;
|
||||||
private static int numTearDownCallsInTransaction = 0;
|
private static int numTearDownCallsInTransaction = 0;
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
private boolean beanInitialized = false;
|
private boolean beanInitialized = false;
|
||||||
|
|
||||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||||
|
|
@ -86,35 +82,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
protected String bar;
|
protected String bar;
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
private int createPerson(String name) {
|
||||||
|
|
||||||
private static int clearPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
|
||||||
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void createPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
|
||||||
try {
|
|
||||||
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
|
|
||||||
}
|
|
||||||
catch (BadSqlGrammarException bsge) {
|
|
||||||
/* ignore */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int countRowsInPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
|
||||||
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int addPerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
|
||||||
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int deletePerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
private int deletePerson(String name) {
|
||||||
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
this.beanInitialized = true;
|
this.beanInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
@ -133,19 +108,15 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
this.bar = bar;
|
this.bar = bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
|
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
|
||||||
assertEquals(countRowsInPersonTable(this.simpleJdbcTemplate), expectedNumRows,
|
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
|
||||||
"Verifying the number of rows in the person table (" + testState + ").");
|
+ testState + ").");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertAddPerson(final String name) {
|
private void assertAddPerson(final String name) {
|
||||||
assertEquals(addPerson(this.simpleJdbcTemplate, name), 1, "Adding '" + name + "'");
|
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public void beforeClass() {
|
public void beforeClass() {
|
||||||
numSetUpCalls = 0;
|
numSetUpCalls = 0;
|
||||||
|
|
@ -156,10 +127,10 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public void afterClass() {
|
public void afterClass() {
|
||||||
assertEquals(numSetUpCalls, 8, "Verifying number of calls to setUp().");
|
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
|
||||||
assertEquals(numSetUpCallsInTransaction, 1, "Verifying number of calls to setUp() within a transaction.");
|
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
|
||||||
assertEquals(numTearDownCalls, 8, "Verifying number of calls to tearDown().");
|
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
|
||||||
assertEquals(numTearDownCallsInTransaction, 1, "Verifying number of calls to tearDown() within a transaction.");
|
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -169,14 +140,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
assertNotNull(super.applicationContext,
|
assertNotNull(super.applicationContext,
|
||||||
"The application context should have been set due to ApplicationContextAware semantics.");
|
"The application context should have been set due to ApplicationContextAware semantics.");
|
||||||
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
|
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
|
||||||
assertEquals(employeeBean.getName(), "John Smith", "Verifying employee's name.");
|
assertEquals(employeeBean.getName(), "John Smith", "employee's name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyBeanInitialized() {
|
public void verifyBeanInitialized() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertTrue(this.beanInitialized,
|
assertTrue(beanInitialized,
|
||||||
"This test instance should have been initialized due to InitializingBean semantics.");
|
"This test instance should have been initialized due to InitializingBean semantics.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -184,7 +155,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyBeanNameSet() {
|
public void verifyBeanNameSet() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertEquals(this.beanName, getClass().getName(),
|
assertEquals(beanName, getClass().getName(),
|
||||||
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
|
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,35 +163,33 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyAnnotationAutowiredFields() {
|
public void verifyAnnotationAutowiredFields() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertNull(this.nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
|
assertNull(nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
|
||||||
assertNotNull(this.pet, "The pet field should have been autowired.");
|
assertNotNull(pet, "The pet field should have been autowired.");
|
||||||
assertEquals(this.pet.getName(), "Fido", "Verifying pet's name.");
|
assertEquals(pet.getName(), "Fido", "pet's name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyAnnotationAutowiredMethods() {
|
public void verifyAnnotationAutowiredMethods() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertNotNull(this.employee, "The setEmployee() method should have been autowired.");
|
assertNotNull(employee, "The setEmployee() method should have been autowired.");
|
||||||
assertEquals(this.employee.getName(), "John Smith", "Verifying employee's name.");
|
assertEquals(employee.getName(), "John Smith", "employee's name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyResourceAnnotationInjectedFields() {
|
public void verifyResourceAnnotationInjectedFields() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertEquals(this.foo, "Foo", "The foo field should have been injected via @Resource.");
|
assertEquals(foo, "Foo", "The foo field should have been injected via @Resource.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void verifyResourceAnnotationInjectedMethods() {
|
public void verifyResourceAnnotationInjectedMethods() {
|
||||||
assertInTransaction(false);
|
assertInTransaction(false);
|
||||||
assertEquals(this.bar, "Bar", "The setBar() method should have been injected via @Resource.");
|
assertEquals(bar, "Bar", "The setBar() method should have been injected via @Resource.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
@BeforeTransaction
|
@BeforeTransaction
|
||||||
public void beforeTransaction() {
|
public void beforeTransaction() {
|
||||||
assertNumRowsInPersonTable(1, "before a transactional test method");
|
assertNumRowsInPersonTable(1, "before a transactional test method");
|
||||||
|
|
@ -255,22 +224,8 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||||
|
|
||||||
@AfterTransaction
|
@AfterTransaction
|
||||||
public void afterTransaction() {
|
public void afterTransaction() {
|
||||||
assertEquals(deletePerson(this.simpleJdbcTemplate, YODA), 1, "Deleting yoda");
|
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
|
||||||
assertNumRowsInPersonTable(1, "after a transactional test method");
|
assertNumRowsInPersonTable(1, "after a transactional test method");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
public static class DatabaseSetup {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
void setDataSource(DataSource dataSource) {
|
|
||||||
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
|
||||||
createPersonTable(simpleJdbcTemplate);
|
|
||||||
clearPersonTable(simpleJdbcTemplate);
|
|
||||||
addPerson(simpleJdbcTemplate, BOB);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
|
||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
<jdbc:embedded-database id="dataSource" />
|
||||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
|
||||||
|
|
||||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||||
p:data-source-ref="dataSource" />
|
p:data-source-ref="dataSource" />
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
|
||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
<jdbc:embedded-database id="dataSource" />
|
||||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
|
||||||
|
|
||||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||||
p:data-source-ref="dataSource" />
|
p:data-source-ref="dataSource" />
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
|
||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
<jdbc:embedded-database id="dataSource" />
|
||||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa"
|
|
||||||
p:password="" />
|
|
||||||
|
|
||||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||||
p:data-source-ref="dataSource" />
|
p:data-source-ref="dataSource" />
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
INSERT INTO person VALUES('bob');
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
CREATE TABLE person (
|
||||||
|
name VARCHAR(20) NOT NULL,
|
||||||
|
PRIMARY KEY(name)
|
||||||
|
);
|
||||||
Loading…
Reference in New Issue