1 16 package scriptella; 17 18 import junit.framework.AssertionFailedError; 19 import scriptella.jdbc.JdbcException; 20 import scriptella.jdbc.JdbcUtils; 21 22 import java.sql.Connection ; 23 import java.sql.DriverManager ; 24 import java.sql.SQLException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 29 30 36 public abstract class DBTestCase extends AbstractTestCase { 37 static { 38 try { 39 Class.forName("org.hsqldb.jdbcDriver"); 40 } catch (ClassNotFoundException e) { 41 throw new AssertionFailedError(e.getMessage()); 42 } 43 } 44 45 private Collection <String > dbNames = new HashSet <String >(); 46 private Collection <Connection > connections = new ArrayList <Connection >(); 47 48 protected Connection getConnection(final String db) { 49 dbNames.add(db); 50 51 try { 52 final Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:" + 53 db, "sa", ""); 54 connections.add(c); 55 56 return c; 57 } catch (SQLException e) { 58 throw new JdbcException(e.getMessage(), e); 59 } 60 } 61 62 protected void tearDown() throws Exception { 63 super.tearDown(); 64 65 for (String s : dbNames) { 66 try { 67 getConnection(s).createStatement().execute("SHUTDOWN"); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 } 72 73 for (Connection connection : connections) { 74 JdbcUtils.closeSilent(connection); 75 } 76 77 dbNames.clear(); 78 connections.clear(); 79 } 80 } 81 | Popular Tags |