private void stopMysql() throws IOException { if (mysqldResource != null) { try { mysqldResource.shutdown(); } catch (NullPointerException npe) { logger.warn("Failed to shutdown mysql properly ", npe); } try { deleteRecursive(dataDir); deleteRecursive(dbDir); } catch (FileNotFoundException e) { throw new IOException(e); } started.set(false); logger.info("MySQL stopped: " + getCmdLineConnectionString()); } }
@Override public void refreshTableNames() throws IOException { final String query = String.format("select table_name from information_schema.tables where table_schema = '%s' and table_type = 'BASE TABLE';", databaseName); try { executeQuery(query, new ResultSetJob() { @Override public void work(final ResultSet resultSet) throws SQLException { allTables.clear(); while (resultSet.next()) { allTables.add(resultSet.getString(1)); } } }); } catch (SQLException e) { throw new IOException(e); } }
@Override public void start() throws IOException { if (started.get()) { throw new IOException("MySQL is already running: " + jdbcConnectionString); } startMysql(); refreshTableNames(); }
@BeforeClass(groups = "slow") public void beforeClass() throws Exception { loadSystemPropertiesFromClasspath("/queue.properties"); clock = new ClockMock(); embeddedDB = new MySQLEmbeddedDB("killbillq", "killbillq", "killbillq"); embeddedDB.initialize(); embeddedDB.start(); final String ddl = toString(Resources.getResource("com/ning/billing/queue/ddl.sql").openStream()); embeddedDB.executeScript(ddl); dbi = new DBI(embeddedDB.getDataSource()); dbi.registerArgumentFactory(new UUIDArgumentFactory()); dbi.registerArgumentFactory(new DateTimeZoneArgumentFactory()); dbi.registerArgumentFactory(new DateTimeArgumentFactory()); dbi.registerArgumentFactory(new LocalDateArgumentFactory()); dbi.registerArgumentFactory(new EnumArgumentFactory()); dbi.registerMapper(new UUIDMapper()); final ConfigSource configSource = new SimplePropertyConfigSource(System.getProperties()); persistentBusConfig = new ConfigurationObjectFactory(configSource).buildWithReplacements(PersistentBusConfig.class, ImmutableMap.<String, String>of("instanceName", "main")); notificationQueueConfig = new ConfigurationObjectFactory(configSource).buildWithReplacements(NotificationQueueConfig.class, ImmutableMap.<String, String>of("instanceName", "main")); }
@BeforeClass(groups = "mysql") public void setUp() throws Exception { embeddedDB = new MySQLEmbeddedDB(); embeddedDB.initialize(); embeddedDB.start(); }
public static synchronized EmbeddedDB get() { if (instance == null) { if ("true".equals(System.getProperty("com.ning.billing.dbi.test.h2"))) { log.info("Using h2 as the embedded database"); instance = new H2EmbeddedDB(); } else { if (isUsingLocalInstance()) { log.info("Using MySQL local database"); final String databaseName = System.getProperty("com.ning.billing.dbi.test.localDb.database", "killbill"); final String username = System.getProperty("com.ning.billing.dbi.test.localDb.password", "root"); final String password = System.getProperty("com.ning.billing.dbi.test.localDb.username", "root"); instance = new MySQLStandaloneDB(databaseName, username, password); } else { log.info("Using MySQL as the embedded database"); instance = new MySQLEmbeddedDB(); } } } return instance; }
public MySQLEmbeddedDB(final String databaseName, final String username, final String password) { super(databaseName, username, password, null); setPort(); this.jdbcConnectionString = "jdbc:mysql://localhost:" + port + "/" + databaseName + "?createDatabaseIfNotExist=true&allowMultiQueries=true"; }
private static boolean deleteRecursive(final File path) throws FileNotFoundException { if (!path.exists()) { throw new FileNotFoundException(path.getAbsolutePath()); } boolean ret = true; if (path.isDirectory()) { final File[] files = path.listFiles(); if (files != null) { for (final File f : files) { ret = ret && deleteRecursive(f); } } } return ret && path.delete(); }
} else { started.set(true); logger.info("MySQL started: " + getCmdLineConnectionString());
@Test(groups = "mysql") public void testSimpleLocking() throws IOException, LockFailedException { final String serviceLock = "MY_AWESOME_LOCK"; final String lockName = UUID.randomUUID().toString(); final GlobalLocker locker = new MySqlGlobalLocker(embeddedDB.getDataSource()); final GlobalLock lock = locker.lockWithNumberOfTries(serviceLock, lockName, 3); Assert.assertFalse(locker.isFree(serviceLock, lockName)); boolean gotException = false; try { locker.lockWithNumberOfTries(serviceLock, lockName, 1); } catch (LockFailedException e) { gotException = true; } Assert.assertTrue(gotException); lock.release(); Assert.assertTrue(locker.isFree(serviceLock, lockName)); } }
public MySQLEmbeddedDB(final String databaseName, final String username, final String password) { super(databaseName, username, password, null); setPort(); this.jdbcConnectionString = "jdbc:mysql://localhost:" + port + "/" + databaseName + "?createDatabaseIfNotExist=true&allowMultiQueries=true"; }
private static boolean deleteRecursive(final File path) throws FileNotFoundException { if (!path.exists()) { throw new FileNotFoundException(path.getAbsolutePath()); } boolean ret = true; if (path.isDirectory()) { final File[] files = path.listFiles(); if (files != null) { for (final File f : files) { ret = ret && deleteRecursive(f); } } } return ret && path.delete(); }
} else { started.set(true); logger.info("MySQL started: " + getCmdLineConnectionString());
@Override public void start() throws IOException { if (started.get()) { throw new IOException("MySQL is already running: " + jdbcConnectionString); } startMysql(); refreshTableNames(); }
private void stopMysql() throws IOException { if (mysqldResource != null) { try { mysqldResource.shutdown(); } catch (NullPointerException npe) { logger.warn("Failed to shutdown mysql properly ", npe); } try { deleteRecursive(dataDir); deleteRecursive(dbDir); } catch (FileNotFoundException e) { throw new IOException(e); } started.set(false); logger.info("MySQL stopped: " + getCmdLineConnectionString()); } }
@Override public void refreshTableNames() throws IOException { final String query = String.format("select table_name from information_schema.tables where table_schema = '%s' and table_type = 'BASE TABLE';", databaseName); try { executeQuery(query, new ResultSetJob() { @Override public void work(final ResultSet resultSet) throws SQLException { allTables.clear(); while (resultSet.next()) { allTables.add(resultSet.getString(1)); } } }); } catch (SQLException e) { throw new IOException(e); } }