1 16 17 package org.springframework.test; 18 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.LineNumberReader ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import javax.sql.DataSource ; 27 28 import org.springframework.core.io.Resource; 29 import org.springframework.dao.DataAccessException; 30 import org.springframework.dao.DataAccessResourceFailureException; 31 import org.springframework.jdbc.core.JdbcTemplate; 32 import org.springframework.util.StringUtils; 33 34 48 public abstract class AbstractTransactionalDataSourceSpringContextTests 49 extends AbstractTransactionalSpringContextTests { 50 51 protected JdbcTemplate jdbcTemplate; 52 53 57 private boolean zappedTables; 58 59 60 63 public AbstractTransactionalDataSourceSpringContextTests() { 64 } 65 66 69 public AbstractTransactionalDataSourceSpringContextTests(String name) { 70 super(name); 71 } 72 73 74 77 public void setDataSource(DataSource dataSource) { 78 this.jdbcTemplate = new JdbcTemplate(dataSource); 79 } 80 81 84 public final JdbcTemplate getJdbcTemplate() { 85 return this.jdbcTemplate; 86 } 87 88 89 95 protected void deleteFromTables(String [] names) { 96 for (int i = 0; i < names.length; i++) { 97 int rowCount = this.jdbcTemplate.update("DELETE FROM " + names[i]); 98 if (logger.isInfoEnabled()) { 99 logger.info("Deleted " + rowCount + " rows from table " + names[i]); 100 } 101 } 102 this.zappedTables = true; 103 } 104 105 110 protected final void setComplete() { 111 if (this.zappedTables) { 112 throw new IllegalStateException ("Cannot set complete after deleting tables"); 113 } 114 super.setComplete(); 115 } 116 117 122 protected int countRowsInTable(String tableName) { 123 return this.jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName); 124 } 125 126 127 139 protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { 140 if (logger.isInfoEnabled()) { 141 logger.info("Executing SQL script '" + sqlResourcePath + "'"); 142 } 143 144 long startTime = System.currentTimeMillis(); 145 List statements = new LinkedList (); 146 Resource res = getApplicationContext().getResource(sqlResourcePath); 147 try { 148 LineNumberReader lnr = new LineNumberReader (new InputStreamReader (res.getInputStream())); 149 String currentStatement = lnr.readLine(); 150 while (currentStatement != null) { 151 currentStatement = StringUtils.replace(currentStatement, ";", ""); 152 statements.add(currentStatement); 153 currentStatement = lnr.readLine(); 154 } 155 156 for (Iterator itr = statements.iterator(); itr.hasNext(); ) { 157 String statement = (String ) itr.next(); 158 try { 159 int rowsAffected = this.jdbcTemplate.update(statement); 160 if (logger.isDebugEnabled()) { 161 logger.debug(rowsAffected + " rows affected by SQL: " + statement); 162 } 163 } 164 catch (DataAccessException ex) { 165 if (continueOnError) { 166 if (logger.isWarnEnabled()) { 167 logger.warn("SQL: " + statement + " failed", ex); 168 } 169 } 170 else { 171 throw ex; 172 } 173 } 174 } 175 long elapsedTime = System.currentTimeMillis() - startTime; 176 logger.info("Done executing SQL script '" + sqlResourcePath + "' in " + elapsedTime + " ms"); 177 } 178 catch (IOException ex) { 179 throw new DataAccessResourceFailureException("Failed to open SQL script '" + sqlResourcePath + "'", ex); 180 } 181 } 182 183 } 184 | Popular Tags |