protected Connection getConnection() throws SQLException, IOException { return getDataSource().getConnection(); }
public void cleanupAllTables() throws IOException { for (final String tableName : allTables) { cleanupTable(tableName); } }
public void executeScript(final String script) throws IOException { final String[] rawStatements = script.replaceAll("\n", " ").replaceAll("\r", "").split(";"); for (final String statement : rawStatements) { if (!WHITESPACE_ONLY.matcher(statement).matches()) { try { // TODO consider batch approach execute(statement); } catch (SQLException e) { throw new IOException(e); } } } }
public static synchronized void start() throws IOException { final EmbeddedDB instance = get(); instance.initialize(); instance.start(); instance.executeScript("drop table if exists accounts;" + "CREATE TABLE accounts (\n" + " record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" + " PRIMARY KEY(record_id)\n" + ");"); instance.executeScript("DROP TABLE IF EXISTS tenants;\n" + "CREATE TABLE tenants (\n" + " record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" + instance.executeScript("DROP TABLE IF EXISTS bundles;\n" + "CREATE TABLE bundles (\n" + " record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" + " PRIMARY KEY(record_id)\n" + ");"); instance.executeScript("DROP TABLE IF EXISTS subscriptions;\n" + "CREATE TABLE subscriptions (\n" + " record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" + instance.executeScript("DROP TABLE IF EXISTS payments;\n" + "CREATE TABLE payments (\n" + " record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" + instance.executeScript(ddl);
public void cleanupTable(final String table) throws IOException { logger.debug("Deleting table: " + table); try { executeQuery("truncate table " + table); } catch (SQLException e) { throw new IOException(e); } }
protected void executeQuery(final String query, final ResultSetJob job) throws SQLException, IOException { final Connection connection = getConnection(); Statement statement = null; try { statement = connection.createStatement(); final ResultSet rs = statement.executeQuery(query); job.work(rs); } finally { if (statement != null) { statement.close(); } connection.close(); } }
@AfterSuite(groups = {"slow", "mysql"}) public void shutdownMysqlAfterTestSuite() throws IOException, ClassNotFoundException, SQLException, URISyntaxException { if (hasFailed()) { log.error("**********************************************************************************************"); log.error("*** TESTS HAVE FAILED - LEAVING DB RUNNING FOR DEBUGGING - MAKE SURE TO KILL IT ONCE DONE ****"); log.error(DBTestingHelper.get().getCmdLineConnectionString()); log.error("**********************************************************************************************"); return; } try { DBTestingHelper.get().stop(); } catch (Exception ignored) { } } }
@BeforeMethod(groups = {"slow", "mysql"}) public void beforeMethod() throws Exception { try { DBTestingHelper.get().cleanupAllTables(); } catch (Exception ignored) { } }
@Override protected void configure() { if (EmbeddedDB.DBEngine.MYSQL.equals(helper.getDBEngine())) { install(new GlobalLockerModule()); } else { install(new MockGlobalLockerModule()); } } }
public void cleanupTable(final String table) throws IOException { logger.debug("Deleting table: " + table); try { executeQuery("truncate table " + table); } catch (SQLException e) { throw new IOException(e); } }
protected void execute(final String query, final ResultSetJob job) throws SQLException, IOException { final Connection connection = getConnection(); Statement statement = null; try { statement = connection.createStatement(); if (statement.execute(query)) { job.work(statement.getResultSet()); } } finally { if (statement != null) { statement.close(); } connection.close(); } }
@AfterSuite(groups = {"slow", "mysql"}) public void afterSuite() throws Exception { if (hasFailed()) { log.error("**********************************************************************************************"); log.error("*** TESTS HAVE FAILED - LEAVING DB RUNNING FOR DEBUGGING - MAKE SURE TO KILL IT ONCE DONE ****"); log.error(DBTestingHelper.get().getCmdLineConnectionString()); log.error("**********************************************************************************************"); return; } try { DBTestingHelper.get().stop(); } catch (Exception ignored) { } } }
@BeforeMethod(groups = {"slow", "mysql"}) public void cleanupTablesBetweenMethods() { try { DBTestingHelper.get().cleanupAllTables(); } catch (Exception ignored) { } }
protected Connection getConnection() throws SQLException, IOException { return getDataSource().getConnection(); }
protected void executeQuery(final String query) throws SQLException, IOException { executeQuery(query, new ResultSetJob()); }
public void executeScript(final String script) throws IOException { final String[] rawStatements = script.replaceAll("\n", " ").replaceAll("\r", "").split(";"); for (final String statement : rawStatements) { if (!WHITESPACE_ONLY.matcher(statement).matches()) { try { // TODO consider batch approach execute(statement); } catch (SQLException e) { throw new IOException(e); } } } }
protected void executeQuery(final String query, final ResultSetJob job) throws SQLException, IOException { final Connection connection = getConnection(); Statement statement = null; try { statement = connection.createStatement(); final ResultSet rs = statement.executeQuery(query); job.work(rs); } finally { if (statement != null) { statement.close(); } connection.close(); } }
public void cleanupAllTables() throws IOException { for (final String tableName : allTables) { cleanupTable(tableName); } }
@Override protected void configure() { super.configure(); final EmbeddedDB instance = DBTestingHelper.get(); bind(EmbeddedDB.class).toInstance(instance); try { bind(DataSource.class).toInstance(DBTestingHelper.get().getDataSource()); bind(IDBI.class).toInstance(DBTestingHelper.getDBI()); } catch (IOException e) { Assert.fail(e.toString()); } } }
protected void executeQuery(final String query) throws SQLException, IOException { executeQuery(query, new ResultSetJob()); }