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.io.BufferedReader;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.sql.Connection;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.sql.DataSource;
30
31 import junit.framework.Assert;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.log4j.Logger;
35 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
36 import org.kuali.hr.time.service.base.TkServiceLocator;
37 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
38 import org.springframework.jdbc.core.ConnectionCallback;
39 import org.springframework.jdbc.core.JdbcTemplate;
40 import org.springframework.jdbc.core.StatementCallback;
41 import org.springframework.transaction.PlatformTransactionManager;
42 import org.springframework.transaction.TransactionStatus;
43 import org.springframework.transaction.support.TransactionCallback;
44 import org.springframework.transaction.support.TransactionTemplate;
45
46 public class SQLDataLifeCycle extends BaseLifecycle {
47 protected static final Logger LOG = Logger.getLogger(SQLDataLifeCycle.class);
48
49 public static final String TEST_TABLE_NAME = "KR_UNITTEST_T";
50 Class callingTestClass = null;
51
52 public SQLDataLifeCycle() {
53
54 }
55
56 public SQLDataLifeCycle(Class caller) {
57 this.callingTestClass = caller;
58 }
59
60 public void start() throws Exception {
61 final StandardXAPoolDataSource dataSource = (StandardXAPoolDataSource) TkServiceLocator.CONTEXT.getBean("kpmeDataSource");
62 final PlatformTransactionManager transactionManager = (PlatformTransactionManager) TkServiceLocator.CONTEXT.getBean("transactionManager");
63 final String schemaName = dataSource.getUser().toUpperCase();
64 loadData(transactionManager, dataSource, schemaName);
65 super.start();
66 }
67
68 public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) {
69 LOG.info("Clearing tables for schema " + schemaName);
70 Assert.assertNotNull("DataSource could not be located.", dataSource);
71
72 if (schemaName == null || schemaName.equals("")) {
73 Assert.fail("Empty schema name given");
74 }
75 new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() {
76 public Object doInTransaction(final TransactionStatus status) {
77 verifyTestEnvironment(dataSource);
78 return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() {
79 public Object doInStatement(Statement statement) throws SQLException {
80 if (callingTestClass != null) {
81 List<String> sqlStatements = getTestDataSQLStatements("src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql");
82
83 for(String sql : sqlStatements){
84 if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) {
85
86 statement.addBatch(sql);
87 }
88 }
89 }
90 statement.executeBatch();
91 return null;
92 }
93 });
94 }
95 });
96 }
97
98 void verifyTestEnvironment(final DataSource dataSource) {
99 Assert.assertTrue("No table named '" + TEST_TABLE_NAME + "' was found in the configured database. " + "You are attempting to run tests against a non-test database!!!",
100 isTestTableInSchema(dataSource));
101 }
102
103 Boolean isTestTableInSchema(final DataSource dataSource) {
104 Assert.assertNotNull("DataSource could not be located.", dataSource);
105 return (Boolean) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
106 public Object doInConnection(final Connection connection) throws SQLException {
107 final ResultSet resultSet = connection.getMetaData().getTables(null, null, TEST_TABLE_NAME, null);
108 return new Boolean(resultSet.next());
109 }
110 });
111 }
112
113 List<String> getTestDataSQLStatements(String fname){
114 List<String> testDataSqlStatements = new ArrayList<String>();
115 try {
116 BufferedReader in = new BufferedReader(new FileReader(fname));
117 String str;
118 while ((str = in.readLine()) != null) {
119 testDataSqlStatements.add(str);
120 }
121 } catch (FileNotFoundException e) {
122 LOG.warn("No file found for " + fname);
123 } catch (IOException e) {
124 LOG.error("IO exception in test data loading");
125 }
126 return testDataSqlStatements;
127 }
128
129 }