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.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                          // djunk - add a per-class special test data loader,
55                          // loads <testclassname>.sql from the same directory
56                          // as the other SQL loaded.
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                                  // ignore comment lines in our sql reader.
63                                  statement.addBatch(sql);
64                              }
65                          }
66                          statement.executeBatch();
67                          return null;
68                      }
69                  });
70              }
71          });
72      }
73  }