|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 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.testng.transaction.programmatic; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 29 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 30 | +import org.springframework.test.annotation.Rollback; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests; |
| 33 | +import org.springframework.test.context.transaction.AfterTransaction; |
| 34 | +import org.springframework.test.context.transaction.BeforeTransaction; |
| 35 | +import org.springframework.test.context.transaction.TestTransaction; |
| 36 | +import org.springframework.test.context.transaction.programmatic.ProgrammaticTxMgmtTests; |
| 37 | +import org.springframework.transaction.PlatformTransactionManager; |
| 38 | +import org.springframework.transaction.annotation.Propagation; |
| 39 | +import org.springframework.transaction.annotation.Transactional; |
| 40 | +import org.testng.IHookCallBack; |
| 41 | +import org.testng.ITestResult; |
| 42 | +import org.testng.annotations.Test; |
| 43 | + |
| 44 | +import static org.junit.Assert.*; |
| 45 | +import static org.springframework.test.transaction.TransactionTestUtils.*; |
| 46 | + |
| 47 | +/** |
| 48 | + * This class is a copy of {@link ProgrammaticTxMgmtTests} that has been modified |
| 49 | + * to run with TestNG. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @since 4.1 |
| 53 | + */ |
| 54 | +@ContextConfiguration |
| 55 | +public class ProgrammaticTxMgmtTestNGTests extends AbstractTransactionalTestNGSpringContextTests { |
| 56 | + |
| 57 | + private String testName; |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + public void run(IHookCallBack callBack, ITestResult testResult) { |
| 62 | + this.testName = testResult.getMethod().getMethodName(); |
| 63 | + super.run(callBack, testResult); |
| 64 | + } |
| 65 | + |
| 66 | + @BeforeTransaction |
| 67 | + public void beforeTransaction() { |
| 68 | + deleteFromTables("user"); |
| 69 | + executeSqlScript("classpath:/org/springframework/test/context/jdbc/data.sql", false); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterTransaction |
| 73 | + public void afterTransaction() { |
| 74 | + switch (testName) { |
| 75 | + case "commitTxAndStartNewTx": { |
| 76 | + assertUsers("Dogbert"); |
| 77 | + break; |
| 78 | + } |
| 79 | + case "commitTxButDoNotStartNewTx": { |
| 80 | + assertUsers("Dogbert"); |
| 81 | + break; |
| 82 | + } |
| 83 | + case "rollbackTxAndStartNewTx": { |
| 84 | + assertUsers("Dilbert"); |
| 85 | + break; |
| 86 | + } |
| 87 | + case "rollbackTxButDoNotStartNewTx": { |
| 88 | + assertUsers("Dilbert"); |
| 89 | + break; |
| 90 | + } |
| 91 | + case "rollbackTxAndStartNewTxWithDefaultCommitSemantics": { |
| 92 | + assertUsers("Dilbert", "Dogbert"); |
| 93 | + break; |
| 94 | + } |
| 95 | + case "startTxWithExistingTransaction": { |
| 96 | + assertUsers("Dilbert"); |
| 97 | + break; |
| 98 | + } |
| 99 | + default: { |
| 100 | + fail("missing 'after transaction' assertion for test method: " + testName); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 107 | + public void isActiveWithNonExistentTransactionContext() { |
| 108 | + assertFalse(TestTransaction.isActive()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test(expectedExceptions = IllegalStateException.class) |
| 112 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 113 | + public void flagForRollbackWithNonExistentTransactionContext() { |
| 114 | + TestTransaction.flagForRollback(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test(expectedExceptions = IllegalStateException.class) |
| 118 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 119 | + public void flagForCommitWithNonExistentTransactionContext() { |
| 120 | + TestTransaction.flagForCommit(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test(expectedExceptions = IllegalStateException.class) |
| 124 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 125 | + public void isFlaggedForRollbackWithNonExistentTransactionContext() { |
| 126 | + TestTransaction.isFlaggedForRollback(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test(expectedExceptions = IllegalStateException.class) |
| 130 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 131 | + public void startTxWithNonExistentTransactionContext() { |
| 132 | + TestTransaction.start(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test(expectedExceptions = IllegalStateException.class) |
| 136 | + public void startTxWithExistingTransaction() { |
| 137 | + TestTransaction.start(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test(expectedExceptions = IllegalStateException.class) |
| 141 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 142 | + public void endTxWithNonExistentTransactionContext() { |
| 143 | + TestTransaction.end(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void commitTxAndStartNewTx() { |
| 148 | + assertInTransaction(true); |
| 149 | + assertTrue(TestTransaction.isActive()); |
| 150 | + assertUsers("Dilbert"); |
| 151 | + deleteFromTables("user"); |
| 152 | + assertUsers(); |
| 153 | + |
| 154 | + // Commit |
| 155 | + TestTransaction.flagForCommit(); |
| 156 | + assertFalse(TestTransaction.isFlaggedForRollback()); |
| 157 | + TestTransaction.end(); |
| 158 | + assertInTransaction(false); |
| 159 | + assertFalse(TestTransaction.isActive()); |
| 160 | + assertUsers(); |
| 161 | + |
| 162 | + executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false); |
| 163 | + assertUsers("Dogbert"); |
| 164 | + |
| 165 | + TestTransaction.start(); |
| 166 | + assertInTransaction(true); |
| 167 | + assertTrue(TestTransaction.isActive()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void commitTxButDoNotStartNewTx() { |
| 172 | + assertInTransaction(true); |
| 173 | + assertTrue(TestTransaction.isActive()); |
| 174 | + assertUsers("Dilbert"); |
| 175 | + deleteFromTables("user"); |
| 176 | + assertUsers(); |
| 177 | + |
| 178 | + // Commit |
| 179 | + TestTransaction.flagForCommit(); |
| 180 | + assertFalse(TestTransaction.isFlaggedForRollback()); |
| 181 | + TestTransaction.end(); |
| 182 | + assertFalse(TestTransaction.isActive()); |
| 183 | + assertInTransaction(false); |
| 184 | + assertUsers(); |
| 185 | + |
| 186 | + executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false); |
| 187 | + assertUsers("Dogbert"); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void rollbackTxAndStartNewTx() { |
| 192 | + assertInTransaction(true); |
| 193 | + assertTrue(TestTransaction.isActive()); |
| 194 | + assertUsers("Dilbert"); |
| 195 | + deleteFromTables("user"); |
| 196 | + assertUsers(); |
| 197 | + |
| 198 | + // Rollback (automatically) |
| 199 | + assertTrue(TestTransaction.isFlaggedForRollback()); |
| 200 | + TestTransaction.end(); |
| 201 | + assertFalse(TestTransaction.isActive()); |
| 202 | + assertInTransaction(false); |
| 203 | + assertUsers("Dilbert"); |
| 204 | + |
| 205 | + // Start new transaction with default rollback semantics |
| 206 | + TestTransaction.start(); |
| 207 | + assertInTransaction(true); |
| 208 | + assertTrue(TestTransaction.isFlaggedForRollback()); |
| 209 | + assertTrue(TestTransaction.isActive()); |
| 210 | + |
| 211 | + executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false); |
| 212 | + assertUsers("Dilbert", "Dogbert"); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void rollbackTxButDoNotStartNewTx() { |
| 217 | + assertInTransaction(true); |
| 218 | + assertTrue(TestTransaction.isActive()); |
| 219 | + assertUsers("Dilbert"); |
| 220 | + deleteFromTables("user"); |
| 221 | + assertUsers(); |
| 222 | + |
| 223 | + // Rollback (automatically) |
| 224 | + assertTrue(TestTransaction.isFlaggedForRollback()); |
| 225 | + TestTransaction.end(); |
| 226 | + assertFalse(TestTransaction.isActive()); |
| 227 | + assertInTransaction(false); |
| 228 | + assertUsers("Dilbert"); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + @Rollback(false) |
| 233 | + public void rollbackTxAndStartNewTxWithDefaultCommitSemantics() { |
| 234 | + assertInTransaction(true); |
| 235 | + assertTrue(TestTransaction.isActive()); |
| 236 | + assertUsers("Dilbert"); |
| 237 | + deleteFromTables("user"); |
| 238 | + assertUsers(); |
| 239 | + |
| 240 | + // Rollback |
| 241 | + TestTransaction.flagForRollback(); |
| 242 | + assertTrue(TestTransaction.isFlaggedForRollback()); |
| 243 | + TestTransaction.end(); |
| 244 | + assertFalse(TestTransaction.isActive()); |
| 245 | + assertInTransaction(false); |
| 246 | + assertUsers("Dilbert"); |
| 247 | + |
| 248 | + // Start new transaction with default commit semantics |
| 249 | + TestTransaction.start(); |
| 250 | + assertInTransaction(true); |
| 251 | + assertFalse(TestTransaction.isFlaggedForRollback()); |
| 252 | + assertTrue(TestTransaction.isActive()); |
| 253 | + |
| 254 | + executeSqlScript("classpath:/org/springframework/test/context/jdbc/data-add-dogbert.sql", false); |
| 255 | + assertUsers("Dilbert", "Dogbert"); |
| 256 | + } |
| 257 | + |
| 258 | + // ------------------------------------------------------------------------- |
| 259 | + |
| 260 | + private void assertUsers(String... users) { |
| 261 | + List<Map<String, Object>> results = jdbcTemplate.queryForList("select name from user"); |
| 262 | + List<String> names = new ArrayList<String>(); |
| 263 | + for (Map<String, Object> map : results) { |
| 264 | + names.add((String) map.get("name")); |
| 265 | + } |
| 266 | + assertEquals(Arrays.asList(users), names); |
| 267 | + } |
| 268 | + |
| 269 | + |
| 270 | + // ------------------------------------------------------------------------- |
| 271 | + |
| 272 | + @Configuration |
| 273 | + static class Config { |
| 274 | + |
| 275 | + @Bean |
| 276 | + public PlatformTransactionManager transactionManager() { |
| 277 | + return new DataSourceTransactionManager(dataSource()); |
| 278 | + } |
| 279 | + |
| 280 | + @Bean |
| 281 | + public DataSource dataSource() { |
| 282 | + return new EmbeddedDatabaseBuilder()// |
| 283 | + .setName("programmatic-tx-mgmt-test-db")// |
| 284 | + .addScript("classpath:/org/springframework/test/context/jdbc/schema.sql") // |
| 285 | + .build(); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | +} |
0 commit comments