|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.junit4.spr9051; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction; |
| 22 | +import static org.springframework.test.transaction.TransactionTestUtils.inTransaction; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.springframework.beans.Employee; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 35 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 36 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 37 | +import org.springframework.test.context.transaction.AfterTransaction; |
| 38 | +import org.springframework.test.context.transaction.BeforeTransaction; |
| 39 | +import org.springframework.transaction.annotation.Transactional; |
| 40 | + |
| 41 | +/** |
| 42 | + * This set of tests investigates the claims made in |
| 43 | + * <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a> |
| 44 | + * with regard to transactional tests. |
| 45 | + * |
| 46 | + * @author Sam Brannen |
| 47 | + * @since 3.2 |
| 48 | + * @see org.springframework.test.context.testng.AnnotationConfigTransactionalTestNGSpringContextTests |
| 49 | + */ |
| 50 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 51 | +public abstract class AbstractTransactionalAnnotatedConfigClassTests { |
| 52 | + |
| 53 | + protected static final String JANE = "jane"; |
| 54 | + protected static final String SUE = "sue"; |
| 55 | + protected static final String YODA = "yoda"; |
| 56 | + |
| 57 | + protected static final int NUM_TESTS = 2; |
| 58 | + protected static final int NUM_TX_TESTS = 1; |
| 59 | + |
| 60 | + private static int numSetUpCalls = 0; |
| 61 | + private static int numSetUpCallsInTransaction = 0; |
| 62 | + private static int numTearDownCalls = 0; |
| 63 | + private static int numTearDownCallsInTransaction = 0; |
| 64 | + |
| 65 | + protected DataSource dataSourceFromTxManager; |
| 66 | + protected DataSource dataSourceViaInjection; |
| 67 | + |
| 68 | + protected JdbcTemplate jdbcTemplate; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private Employee employee; |
| 72 | + |
| 73 | + |
| 74 | + @Autowired |
| 75 | + public void setTransactionManager(DataSourceTransactionManager transactionManager) { |
| 76 | + this.dataSourceFromTxManager = transactionManager.getDataSource(); |
| 77 | + } |
| 78 | + |
| 79 | + @Autowired |
| 80 | + public void setDataSource(DataSource dataSource) { |
| 81 | + this.dataSourceViaInjection = dataSource; |
| 82 | + this.jdbcTemplate = new JdbcTemplate(dataSource); |
| 83 | + } |
| 84 | + |
| 85 | + protected int countRowsInTable(String tableName) { |
| 86 | + return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName); |
| 87 | + } |
| 88 | + |
| 89 | + protected int createPerson(String name) { |
| 90 | + return jdbcTemplate.update("INSERT INTO person VALUES(?)", name); |
| 91 | + } |
| 92 | + |
| 93 | + protected int deletePerson(String name) { |
| 94 | + return jdbcTemplate.update("DELETE FROM person WHERE name=?", name); |
| 95 | + } |
| 96 | + |
| 97 | + protected void assertNumRowsInPersonTable(int expectedNumRows, String testState) { |
| 98 | + assertEquals("the number of rows in the person table (" + testState + ").", expectedNumRows, |
| 99 | + countRowsInTable("person")); |
| 100 | + } |
| 101 | + |
| 102 | + protected void assertAddPerson(final String name) { |
| 103 | + assertEquals("Adding '" + name + "'", 1, createPerson(name)); |
| 104 | + } |
| 105 | + |
| 106 | + @BeforeClass |
| 107 | + public static void beforeClass() { |
| 108 | + numSetUpCalls = 0; |
| 109 | + numSetUpCallsInTransaction = 0; |
| 110 | + numTearDownCalls = 0; |
| 111 | + numTearDownCallsInTransaction = 0; |
| 112 | + } |
| 113 | + |
| 114 | + @AfterClass |
| 115 | + public static void afterClass() { |
| 116 | + assertEquals("number of calls to setUp().", NUM_TESTS, numSetUpCalls); |
| 117 | + assertEquals("number of calls to setUp() within a transaction.", NUM_TX_TESTS, numSetUpCallsInTransaction); |
| 118 | + assertEquals("number of calls to tearDown().", NUM_TESTS, numTearDownCalls); |
| 119 | + assertEquals("number of calls to tearDown() within a transaction.", NUM_TX_TESTS, numTearDownCallsInTransaction); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void autowiringFromConfigClass() { |
| 124 | + assertNotNull("The employee should have been autowired.", employee); |
| 125 | + assertEquals("John Smith", employee.getName()); |
| 126 | + } |
| 127 | + |
| 128 | + @BeforeTransaction |
| 129 | + public void beforeTransaction() { |
| 130 | + assertNumRowsInPersonTable(0, "before a transactional test method"); |
| 131 | + assertAddPerson(YODA); |
| 132 | + } |
| 133 | + |
| 134 | + @Before |
| 135 | + public void setUp() throws Exception { |
| 136 | + numSetUpCalls++; |
| 137 | + if (inTransaction()) { |
| 138 | + numSetUpCallsInTransaction++; |
| 139 | + } |
| 140 | + assertNumRowsInPersonTable((inTransaction() ? 1 : 0), "before a test method"); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @Transactional |
| 145 | + public void modifyTestDataWithinTransaction() { |
| 146 | + assertInTransaction(true); |
| 147 | + assertAddPerson(JANE); |
| 148 | + assertAddPerson(SUE); |
| 149 | + assertNumRowsInPersonTable(3, "in modifyTestDataWithinTransaction()"); |
| 150 | + } |
| 151 | + |
| 152 | + @After |
| 153 | + public void tearDown() throws Exception { |
| 154 | + numTearDownCalls++; |
| 155 | + if (inTransaction()) { |
| 156 | + numTearDownCallsInTransaction++; |
| 157 | + } |
| 158 | + assertNumRowsInPersonTable((inTransaction() ? 3 : 0), "after a test method"); |
| 159 | + } |
| 160 | + |
| 161 | + @AfterTransaction |
| 162 | + public void afterTransaction() { |
| 163 | + assertEquals("Deleting yoda", 1, deletePerson(YODA)); |
| 164 | + assertNumRowsInPersonTable(0, "after a transactional test method"); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments