diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
new file mode 100644
index 00000000000..6ec76195dfb
--- /dev/null
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
@@ -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.
+ *
+ *
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");
+ }
+
+}
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTestsConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTestsConfig.java
new file mode 100644
index 00000000000..2af778e56cf
--- /dev/null
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTestsConfig.java
@@ -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();
+ }
+
+}
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests-context.xml b/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests-context.xml
index 037962cac89..11e92153da0 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests-context.xml
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests-context.xml
@@ -1,33 +1,25 @@
+ xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
+ 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">
-
-
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
+
+
+
+
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java
index 052a7082f29..06060043ce8 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java
@@ -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");
* 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 javax.annotation.Resource;
-import javax.sql.DataSource;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
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.context.ContextConfiguration;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
-import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
@@ -56,18 +52,18 @@ import org.testng.annotations.Test;
public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests
implements BeanNameAware, InitializingBean {
- private static final String BOB = "bob";
private static final String JANE = "jane";
private static final String SUE = "sue";
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 numSetUpCallsInTransaction = 0;
private static int numTearDownCalls = 0;
private static int numTearDownCallsInTransaction = 0;
- // ------------------------------------------------------------------------|
-
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
@@ -86,35 +82,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
protected String bar;
- // ------------------------------------------------------------------------|
-
- 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) {
+ private int createPerson(String 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);
}
- // ------------------------------------------------------------------------|
-
public void afterPropertiesSet() throws Exception {
this.beanInitialized = true;
}
@@ -133,19 +108,15 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
this.bar = bar;
}
- // ------------------------------------------------------------------------|
-
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
- assertEquals(countRowsInPersonTable(this.simpleJdbcTemplate), expectedNumRows,
- "Verifying the number of rows in the person table (" + testState + ").");
+ assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
+ + testState + ").");
}
private void assertAddPerson(final String name) {
- assertEquals(addPerson(this.simpleJdbcTemplate, name), 1, "Adding '" + name + "'");
+ assertEquals(createPerson(name), 1, "Adding '" + name + "'");
}
- // ------------------------------------------------------------------------|
-
@BeforeClass
public void beforeClass() {
numSetUpCalls = 0;
@@ -156,10 +127,10 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@AfterClass
public void afterClass() {
- assertEquals(numSetUpCalls, 8, "Verifying number of calls to setUp().");
- assertEquals(numSetUpCallsInTransaction, 1, "Verifying number of calls to setUp() within a transaction.");
- assertEquals(numTearDownCalls, 8, "Verifying number of calls to tearDown().");
- assertEquals(numTearDownCallsInTransaction, 1, "Verifying number of calls to tearDown() within a transaction.");
+ 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
@@ -169,14 +140,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
assertNotNull(super.applicationContext,
"The application context should have been set due to ApplicationContextAware semantics.");
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
@NotTransactional
public void verifyBeanInitialized() {
assertInTransaction(false);
- assertTrue(this.beanInitialized,
+ assertTrue(beanInitialized,
"This test instance should have been initialized due to InitializingBean semantics.");
}
@@ -184,7 +155,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@NotTransactional
public void verifyBeanNameSet() {
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.");
}
@@ -192,35 +163,33 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@NotTransactional
public void verifyAnnotationAutowiredFields() {
assertInTransaction(false);
- assertNull(this.nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
- assertNotNull(this.pet, "The pet field should have been autowired.");
- assertEquals(this.pet.getName(), "Fido", "Verifying pet's name.");
+ assertNull(nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
+ assertNotNull(pet, "The pet field should have been autowired.");
+ assertEquals(pet.getName(), "Fido", "pet's name.");
}
@Test
@NotTransactional
public void verifyAnnotationAutowiredMethods() {
assertInTransaction(false);
- assertNotNull(this.employee, "The setEmployee() method should have been autowired.");
- assertEquals(this.employee.getName(), "John Smith", "Verifying employee's name.");
+ assertNotNull(employee, "The setEmployee() method should have been autowired.");
+ assertEquals(employee.getName(), "John Smith", "employee's name.");
}
@Test
@NotTransactional
public void verifyResourceAnnotationInjectedFields() {
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
@NotTransactional
public void verifyResourceAnnotationInjectedMethods() {
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
public void beforeTransaction() {
assertNumRowsInPersonTable(1, "before a transactional test method");
@@ -255,22 +224,8 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@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");
}
-
- // ------------------------------------------------------------------------|
-
- public static class DatabaseSetup {
-
- @Autowired
- void setDataSource(DataSource dataSource) {
- SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
- createPersonTable(simpleJdbcTemplate);
- clearPersonTable(simpleJdbcTemplate);
- addPerson(simpleJdbcTemplate, BOB);
- }
- }
-
}
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/DirtiesContextTransactionalTestNGSpringContextTests-context.xml b/org.springframework.test/src/test/java/org/springframework/test/context/testng/DirtiesContextTransactionalTestNGSpringContextTests-context.xml
index 3c4fc976b8e..f1786428fbf 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/testng/DirtiesContextTransactionalTestNGSpringContextTests-context.xml
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/DirtiesContextTransactionalTestNGSpringContextTests-context.xml
@@ -1,10 +1,10 @@
+ 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
+ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
-
+
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/FailingBeforeAndAfterMethodsTests-context.xml b/org.springframework.test/src/test/java/org/springframework/test/context/testng/FailingBeforeAndAfterMethodsTests-context.xml
index 3c4fc976b8e..f1786428fbf 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/testng/FailingBeforeAndAfterMethodsTests-context.xml
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/FailingBeforeAndAfterMethodsTests-context.xml
@@ -1,10 +1,10 @@
+ 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
+ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
-
+
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/TimedTransactionalTestNGSpringContextTests-context.xml b/org.springframework.test/src/test/java/org/springframework/test/context/testng/TimedTransactionalTestNGSpringContextTests-context.xml
index ed86e35a474..bbff4c9e565 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/testng/TimedTransactionalTestNGSpringContextTests-context.xml
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/TimedTransactionalTestNGSpringContextTests-context.xml
@@ -1,11 +1,10 @@
+ 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
+ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
-
+
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/data.sql b/org.springframework.test/src/test/java/org/springframework/test/context/testng/data.sql
new file mode 100644
index 00000000000..10d02a9c919
--- /dev/null
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/data.sql
@@ -0,0 +1 @@
+INSERT INTO person VALUES('bob');
\ No newline at end of file
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/testng/schema.sql b/org.springframework.test/src/test/java/org/springframework/test/context/testng/schema.sql
new file mode 100644
index 00000000000..d5bacef4fbd
--- /dev/null
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/testng/schema.sql
@@ -0,0 +1,4 @@
+CREATE TABLE person (
+ name VARCHAR(20) NOT NULL,
+ PRIMARY KEY(name)
+);
\ No newline at end of file