View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  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                                      // ignore comment lines in our sql reader.
60                      			    statement.addBatch(sql);
61                                  }
62                      		}
63                          }
64                  		statement.executeBatch();
65                  		return null;
66                  	}
67              	});
68              }
69          });
70  	}
71  }