@Provides @Singleton // Note: we need to inject the pooled DataSource here, not the (direct) one from EmbeddedDB protected GlobalLocker provideGlobalLocker(final DataSource dataSource, final EmbeddedDB embeddedDB) throws IOException { if (EmbeddedDB.DBEngine.MYSQL.equals(embeddedDB.getDBEngine())) { return new MySqlGlobalLocker(dataSource); } else if (EmbeddedDB.DBEngine.POSTGRESQL.equals(embeddedDB.getDBEngine())) { return new PostgreSQLGlobalLocker(dataSource); } else { return new MemoryGlobalLocker(); } }
protected synchronized void executePostStartupScripts() throws IOException { final String databaseSpecificDDL = "org/killbill/billing/util/" + "ddl-" + instance.getDBEngine().name().toLowerCase() + ".sql"; installDDLSilently(databaseSpecificDDL); instance.executeScript("drop table if exists accounts;" + "CREATE TABLE accounts (\n" + " record_id serial unique,\n" + " PRIMARY KEY(record_id)\n" + ");"); instance.executeScript("DROP TABLE IF EXISTS tenants;\n" + "CREATE TABLE tenants (\n" + " record_id serial unique,\n" + instance.executeScript("DROP TABLE IF EXISTS bundles;\n" + "CREATE TABLE bundles (\n" + " record_id serial unique,\n" + " PRIMARY KEY(record_id)\n" + ");"); instance.executeScript("DROP TABLE IF EXISTS subscriptions;\n" + "CREATE TABLE subscriptions (\n" + " record_id serial unique,\n" + instance.executeScript("DROP TABLE IF EXISTS payments;\n" + "CREATE TABLE payments (\n" + " record_id serial unique,\n" +
protected void cleanupAllTables() { try { DBTestingHelper.get().getInstance().cleanupAllTables(); } catch (final Exception e) { Assert.fail("Unable to clean database", e); } }
@AfterSuite(groups = "slow") 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().getInstance().getCmdLineConnectionString()); log.error("**********************************************************************************************"); return; } try { DBTestingHelper.get().getInstance().stop(); } catch (final Exception ignored) { } } }
@Override @BeforeMethod(groups = "slow") public void beforeMethod() throws Exception { if (hasFailed()) { return; } super.beforeMethod(); final KillBillJdbcRealm realm = new KillBillJdbcRealm(helper.getDataSource(), securityConfig); securityManager = new DefaultSecurityManager(realm); SecurityUtils.setSecurityManager(securityManager); }
@Override public void stop() throws IOException { started.set(false); super.stop(); } }
final List<String> tables = embeddedDB.getAllTables(); final Connection connection = embeddedDB.getDataSource().getConnection(); try { for (final String table : tables) {
private void installDDLSilently(final String resourceName) throws IOException { final ClassLoader classLoader = MoreObjects.firstNonNull(Thread.currentThread().getContextClassLoader(), DBTestingHelper.class.getClassLoader()); final Enumeration<URL> resources = classLoader.getResources(resourceName); while (resources.hasMoreElements()) { final URL inputStream = resources.nextElement(); final String ddl; try { ddl = IOUtils.toString(inputStream.openStream()); getInstance().executeScript(ddl); } catch (final Exception ignored) { // The test doesn't have this module ddl in the classpath - that's fine } } }
protected void execute(final String query) throws SQLException, IOException { execute(query, new ResultSetJob()); }
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(); } }
@Override @BeforeClass(groups = "slow") public void beforeClass() throws Exception { if (hasFailed()) { return; } super.beforeClass(); final DatabaseSchemaDao dao = new DatabaseSchemaDao(dbi, roDbi); vm = new ValidationManager(dao); vm.loadSchemaInformation(helper.getDatabaseName()); }
public void cleanupAllTables() throws IOException { for (final String tableName : allTables) { cleanupTable(tableName); } }
public void executeScript(final String script) throws IOException { try { execute(script); } catch (final SQLException e) { throw new IOException(e); } }
@Override public void start() throws IOException { started.set(true); refreshTableNames(); }
@Override public DataSource get() { final DataSource dataSource = buildDataSource(); if (embeddedDB != null) { embeddedDB.setDataSource(dataSource); } return dataSource; }
@Override public void initialize() throws IOException, SQLException { super.initialize(); dataSource = new PGSimpleDataSource(); ((PGSimpleDataSource) dataSource).setDatabaseName(databaseName); ((PGSimpleDataSource) dataSource).setUser(username); ((PGSimpleDataSource) dataSource).setPassword(password); ((PGSimpleDataSource) dataSource).setUrl(jdbcConnectionString); }
@AfterSuite(groups = "slow") public void afterSuite() throws Exception { if (hasFailed()) { threadDump(); dumpDB(); log.error("**********************************************************************************************"); log.error("*** TESTS HAVE FAILED - LEAVING DB RUNNING FOR DEBUGGING - MAKE SURE TO KILL IT ONCE DONE ****"); log.error(DBTestingHelper.get().getInstance().getCmdLineConnectionString()); log.error("**********************************************************************************************"); return; } if (cacheManager != null) { cacheManager.close(); } if (redissonCachingClient != null) { redissonCachingClient.shutdown(); } try { DBTestingHelper.get().getInstance().stop(); } catch (final Exception ignored) { } }
public void stop() throws IOException { final DataSource dataSource = getDataSource(); if (dataSource instanceof HikariDataSource) { ((HikariDataSource) dataSource).shutdown(); } }
@AfterClass(groups = "slow") public void tearDownAfterClass() throws Exception { embeddedDB.stop(); } }
@BeforeMethod(groups = "slow") public void cleanupTablesBetweenMethods() { try { DBTestingHelper.get().getInstance().cleanupAllTables(); } catch (final Exception ignored) { } }