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.List;
21  
22  import javax.sql.DataSource;
23  
24  import junit.framework.Assert;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.springframework.jdbc.core.JdbcTemplate;
28  import org.springframework.jdbc.core.StatementCallback;
29  import org.springframework.transaction.PlatformTransactionManager;
30  import org.springframework.transaction.TransactionStatus;
31  import org.springframework.transaction.support.TransactionCallback;
32  import org.springframework.transaction.support.TransactionTemplate;
33  
34  public class LoadDatabaseDataLifeCycle extends SQLDataLifeCycle {
35      public LoadDatabaseDataLifeCycle() {
36      }
37  
38      public LoadDatabaseDataLifeCycle(Class caller) {
39          super(caller);
40      }
41  
42  	public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) {
43  		LOG.info("Clearing tables for schema " + schemaName);
44  		Assert.assertNotNull("DataSource could not be located.", dataSource);
45  
46  		if (schemaName == null || schemaName.equals("")) {
47  			Assert.fail("Empty schema name given");
48  		}
49  		new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() {
50              public Object doInTransaction(final TransactionStatus status) {
51              	verifyTestEnvironment(dataSource);
52              	return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() {
53                  	public Object doInStatement(Statement statement) throws SQLException {
54                          if (callingTestClass != null) {
55                          	List<String> sqlStatements = getTestDataSQLStatements("src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql");
56                          	
57                          	for(String sql : sqlStatements){
58                                  if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) {
59                                      
60                      			    statement.addBatch(sql);
61                                  }
62                      		}
63                          }
64                  		statement.executeBatch();
65                  		return null;
66                  	}
67              	});
68              }
69          });
70  	}
71  }