Clover Coverage Report - KS Common 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 06:00:36 EDT
../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
44   122   15   6.29
14   93   0.34   7
7     2.14  
1    
 
  SQLDataLoader       Line # 30 44 0% 15 65 0% 0.0
 
No Tests
 
1    /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.opensource.org/licenses/ecl2.php
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
10    * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
11    * language governing permissions and limitations under the License.
12    */
13    package org.kuali.rice.test;
14   
15    import java.io.BufferedReader;
16    import java.io.InputStreamReader;
17    import java.sql.Connection;
18    import java.sql.SQLException;
19    import java.sql.Statement;
20   
21    import org.apache.commons.lang.StringUtils;
22    import org.apache.log4j.Logger;
23    import org.springframework.core.io.DefaultResourceLoader;
24    import org.springframework.jdbc.core.ConnectionCallback;
25    import org.springframework.jdbc.core.JdbcTemplate;
26    import org.springframework.transaction.TransactionStatus;
27    import org.springframework.transaction.support.TransactionCallback;
28    import org.springframework.transaction.support.TransactionTemplate;
29   
 
30    public class SQLDataLoader {
31   
32    private static final Logger LOG = Logger.getLogger(SQLDataLoader.class);
33    public static final String SQL_LINE_COMMENT_PREFIX = "--";
34   
35    private String fileLoc;
36    private String seperatorChar;
37    private String statement;
38   
 
39  0 toggle public SQLDataLoader(String statement) {
40  0 this.fileLoc = null;
41  0 this.seperatorChar = null;
42  0 this.statement = statement;
43    }
44   
 
45  0 toggle public SQLDataLoader(String fileLoc, String seperatorChar) {
46  0 this.fileLoc = fileLoc;
47  0 this.seperatorChar = seperatorChar;
48  0 this.statement = null;
49    }
50   
 
51  0 toggle public void runSql() throws Exception {
52  0 String[] sqlStatements = null;
53  0 if (statement == null) {
54  0 String sqlStatementsContent = getContentsAsString(fileLoc);
55    // separator char must be the last non-whitespace char on the line
56    // to avoid splitting in the middle of data that might contain the separator char
57  0 sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$");
58    } else {
59  0 sqlStatements = new String[]{statement};
60    }
61  0 final String[] finalSqlStatements = sqlStatements;
62  0 new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() {
 
63  0 toggle public Object doInTransaction(TransactionStatus status) {
64  0 return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() {
 
65  0 toggle public Object doInConnection(Connection connection) throws SQLException {
66  0 Statement statement = connection.createStatement();
67  0 LOG.info("################################");
68  0 LOG.info(fileLoc != null ? "#" + fileLoc : "#");
69  0 LOG.info("#");
70  0 for (String sqlStatement : finalSqlStatements) {
71  0 if (StringUtils.isNotBlank(sqlStatement)) {
72  0 LOG.info("# Executing sql statement ->" + sqlStatement + "<-");
73  0 statement.execute(sqlStatement);
74    }
75    }
76  0 LOG.info("#");
77  0 LOG.info("#");
78  0 LOG.info("################################");
79  0 statement.close();
80  0 return null;
81    }
82    });
83    }
84    });
85    }
86   
 
87  0 toggle private String getContentsAsString(String fileLoc) throws Exception {
88  0 DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
89  0 String data = "";
90  0 BufferedReader reader = null;
91  0 try {
92  0 reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream()));
93  0 String line = "";
94  0 while ((line = reader.readLine()) != null) {
95    // discard comments...commented single line statements
96    // will result in errors when executed because there are no
97    // results
98  0 if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) {
99  0 data += line + "\r\n ";
100    }
101    }
102    } finally {
103  0 if (reader != null) {
104  0 try {
105  0 reader.close();
106    } catch (Exception e) {
107  0 LOG.error(e);
108    }
109    }
110   
111    }
112  0 return data;
113    }
114   
 
115  0 toggle public String getSeperatorChar() {
116  0 if (this.seperatorChar == null) {
117  0 return ";";
118    }
119  0 return seperatorChar;
120    }
121   
122    }