1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.util;
17
18 import java.sql.SQLException;
19 import java.sql.Statement;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.sql.DataSource;
24
25 import junit.framework.Assert;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.springframework.jdbc.core.JdbcTemplate;
29 import org.springframework.jdbc.core.StatementCallback;
30 import org.springframework.transaction.PlatformTransactionManager;
31 import org.springframework.transaction.TransactionStatus;
32 import org.springframework.transaction.support.TransactionCallback;
33 import org.springframework.transaction.support.TransactionTemplate;
34
35 public class DatabaseCleanupDataLifecycle extends SQLDataLifeCycle {
36
37 public DatabaseCleanupDataLifecycle(Class caller) {
38 super(caller);
39 }
40
41 public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) {
42 Assert.assertNotNull("DataSource could not be located.", dataSource);
43
44 if (schemaName == null || schemaName.equals("")) {
45 Assert.fail("Empty schema name given");
46 }
47 new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
48 public Object doInTransaction(final TransactionStatus status) {
49 verifyTestEnvironment(dataSource);
50 return new JdbcTemplate(dataSource).execute(new StatementCallback() {
51 public Object doInStatement(Statement statement) throws SQLException {
52 List<String> sqlStatements = new ArrayList<String>();
53
54
55
56
57 if (callingTestClass != null) {
58 sqlStatements.addAll(getTestDataSQLStatements("src/test/config/sql/" + callingTestClass.getSimpleName() + "-cleanup.sql"));
59 }
60 for(String sql : sqlStatements){
61 if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) {
62
63 statement.addBatch(sql);
64 }
65 }
66 statement.executeBatch();
67 return null;
68 }
69 });
70 }
71 });
72 }
73 }