Coverage Report - org.kuali.rice.test.SQLDataLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
SQLDataLoader
0%
0/38
0%
0/10
2.429
SQLDataLoader$1
0%
0/2
N/A
2.429
SQLDataLoader$1$1
0%
0/14
0%
0/6
2.429
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.rice.test;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.springframework.core.io.DefaultResourceLoader;
 21  
 import org.springframework.jdbc.core.ConnectionCallback;
 22  
 import org.springframework.jdbc.core.JdbcTemplate;
 23  
 import org.springframework.transaction.TransactionStatus;
 24  
 import org.springframework.transaction.support.TransactionCallback;
 25  
 import org.springframework.transaction.support.TransactionTemplate;
 26  
 
 27  
 import java.io.BufferedReader;
 28  
 import java.io.InputStreamReader;
 29  
 import java.sql.Connection;
 30  
 import java.sql.SQLException;
 31  
 import java.sql.Statement;
 32  
 
 33  0
 public class SQLDataLoader {
 34  
 
 35  0
     private static final Logger LOG = Logger.getLogger(SQLDataLoader.class);
 36  
     public static final String SQL_LINE_COMMENT_PREFIX = "--";
 37  
 
 38  
     private String fileLoc;
 39  
     private String seperatorChar;
 40  
     private String statement;
 41  
 
 42  0
     public SQLDataLoader(String statement) {
 43  0
         this.fileLoc = null;
 44  0
         this.seperatorChar = null;
 45  0
         this.statement = statement;
 46  0
     }
 47  
 
 48  0
     public SQLDataLoader(String fileLoc, String seperatorChar) {
 49  0
         this.fileLoc = fileLoc;
 50  0
         this.seperatorChar = seperatorChar;
 51  0
         this.statement = null;
 52  0
     }
 53  
 
 54  
     public void runSql() throws Exception {
 55  0
         String[] sqlStatements = null;
 56  0
         if (statement == null) {
 57  0
             String sqlStatementsContent = getContentsAsString(fileLoc);
 58  
             // separator char must be the last non-whitespace char on the line
 59  
             // to avoid splitting in the middle of data that might contain the separator char
 60  0
             sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$");
 61  0
         } else {
 62  0
             sqlStatements = new String[]{statement};
 63  
         }
 64  0
         final String[] finalSqlStatements = sqlStatements;
 65  0
         new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() {
 66  
             public Object doInTransaction(TransactionStatus status) {
 67  0
                 return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() {
 68  
                     public Object doInConnection(Connection connection) throws SQLException {
 69  0
                         Statement statement = connection.createStatement();
 70  0
                         LOG.info("################################");
 71  0
                         LOG.info(fileLoc != null ? "#" + fileLoc : "#");
 72  0
                         LOG.info("#");
 73  0
                         for (String sqlStatement : finalSqlStatements) {
 74  0
                             if (StringUtils.isNotBlank(sqlStatement)) {
 75  0
                                 LOG.info("# Executing sql statement ->" + sqlStatement + "<-");
 76  0
                                 statement.execute(sqlStatement);
 77  
                             }
 78  
                         }
 79  0
                         LOG.info("#");
 80  0
                         LOG.info("#");
 81  0
                         LOG.info("################################");
 82  0
                         statement.close();
 83  0
                         return null;
 84  
                     }
 85  
                 });
 86  
             }
 87  
         });
 88  0
     }
 89  
 
 90  
     private String getContentsAsString(String fileLoc) throws Exception {
 91  0
         DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
 92  0
         String data = "";
 93  0
         BufferedReader reader = null;
 94  
         try {
 95  0
             reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream()));
 96  0
             String line = "";
 97  0
             while ((line = reader.readLine()) != null) {
 98  
                 // discard comments...commented single line statements
 99  
                 // will result in errors when executed because there are no
 100  
                 // results
 101  0
                 if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) {
 102  0
                     data += line + "\r\n ";
 103  
                 }
 104  
             }
 105  
         } finally {
 106  0
             if (reader != null) {
 107  
                 try {
 108  0
                     reader.close();
 109  0
                 } catch (Exception e) {
 110  0
                     LOG.error(e);
 111  0
                 }
 112  
             }
 113  
 
 114  
         }
 115  0
         return data;
 116  
     }
 117  
 
 118  
     public String getSeperatorChar() {
 119  0
         if (this.seperatorChar == null) {
 120  0
             return ";";
 121  
         }
 122  0
         return seperatorChar;
 123  
     }
 124  
 
 125  
 }