@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); }
@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) { } } }
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" +
final List<String> tables = embeddedDB.getAllTables(); final Connection connection = embeddedDB.getDataSource().getConnection(); try { for (final String table : tables) {
embeddedDB.initialize(); embeddedDB.start(); if (embeddedDB.getDBEngine() == EmbeddedDB.DBEngine.POSTGRESQL) { embeddedDB.executeScript("CREATE DOMAIN datetime AS timestamp without time zone;" + "CREATE OR REPLACE FUNCTION last_insert_id() RETURNS BIGINT AS $$\n" + " DECLARE\n" + embeddedDB.executeScript(ddl); embeddedDB.refreshTableNames(); dbi = new DBI(embeddedDB.getDataSource()); InTransaction.setupDBI(dbi); dbi.setTransactionHandler(new NotificationTransactionHandler(databaseTransactionNotificationApi));
private DataSource buildDataSource() { switch(config.getConnectionPoolingType()) { case C3P0: loadDriver(); return new C3P0DataSourceBuilder().buildDataSource(); case HIKARICP: if (dataSourceClassName != null) { loadDriver(); } return new HikariDataSourceBuilder().buildDataSource(); case NONE: if (embeddedDB != null) { try { embeddedDB.initialize(); embeddedDB.start(); return embeddedDB.getDataSource(); } catch (final IOException e) { throw new RuntimeException(e); } catch (final SQLException e) { throw new RuntimeException(e); } } default: break; } throw new IllegalArgumentException("DataSource " + config.getConnectionPoolingType() + " unsupported"); }
@BeforeClass(groups = "slow") public void setUpBeforeClass() throws Exception { embeddedDB = PlatformDBTestingHelper.get().getInstance(); embeddedDB.initialize(); embeddedDB.start(); final String ddl = TestUtils.toString(getDdlFileName()); embeddedDB.executeScript(ddl); embeddedDB.refreshTableNames(); }
@Override public void stop() throws IOException { started.set(false); super.stop(); } }
@BeforeMethod(groups = "slow") public void setUp() throws Exception { SpyLogFactory.loadSpyLogDelegator("net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator"); final String databaseName = "killbillosgitests"; embeddedDB = new H2EmbeddedDB(databaseName, UUID.randomUUID().toString(), UUID.randomUUID().toString(), "jdbc:h2:mem:" + databaseName + ";DB_CLOSE_ON_EXIT=FALSE"); embeddedDB.initialize(); embeddedDB.start(); }
@BeforeMethod(groups = "slow") public void cleanupTablesBetweenMethods() { try { DBTestingHelper.get().getInstance().cleanupAllTables(); } catch (final Exception ignored) { } }
@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(); } }
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 } } }
@AfterClass(groups = "slow") public void tearDownAfterClass() throws Exception { embeddedDB.stop(); } }
final List<String> tables = embeddedDB.getAllTables(); final Connection connection = embeddedDB.getDataSource().getConnection(); try { for (final String table : tables) {
protected void cleanupAllTables() { try { DBTestingHelper.get().getInstance().cleanupAllTables(); } catch (final Exception e) { Assert.fail("Unable to clean database", e); } }
@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(); } }
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 Connection getConnection() throws SQLException, IOException { return getDataSource().getConnection(); }
@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) { } }
@AfterClass(groups = "slow") public void afterClass() throws Exception { embeddedDB.stop(); }