use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.
the class JdbcUtilsTest method initDataSource_NonTransactional_ShouldReturnProperDataSource.
@Test
public void initDataSource_NonTransactional_ShouldReturnProperDataSource() throws SQLException {
// Arrange
Properties properties = new Properties();
properties.setProperty(DatabaseConfig.CONTACT_POINTS, "jdbc:mysql://localhost:3306/");
properties.setProperty(DatabaseConfig.USERNAME, "root");
properties.setProperty(DatabaseConfig.PASSWORD, "mysql");
properties.setProperty(DatabaseConfig.STORAGE, "jdbc");
properties.setProperty(JdbcConfig.ISOLATION_LEVEL, "SERIALIZABLE");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MIN_IDLE, "10");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_IDLE, "20");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_TOTAL, "30");
properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_ENABLED, "true");
properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_MAX_OPEN, "100");
JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties));
// Act
BasicDataSource dataSource = JdbcUtils.initDataSource(config);
// Assert
assertThat(dataSource.getUrl()).isEqualTo("jdbc:mysql://localhost:3306/");
assertThat(dataSource.getDriver().getClass().getName()).isEqualTo("com.mysql.cj.jdbc.Driver");
assertThat(dataSource.getUsername()).isEqualTo("root");
assertThat(dataSource.getPassword()).isEqualTo("mysql");
assertThat(dataSource.getDefaultAutoCommit()).isEqualTo(null);
assertThat(dataSource.getAutoCommitOnReturn()).isEqualTo(true);
assertThat(dataSource.getDefaultTransactionIsolation()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE);
assertThat(dataSource.getMinIdle()).isEqualTo(10);
assertThat(dataSource.getMaxIdle()).isEqualTo(20);
assertThat(dataSource.getMaxTotal()).isEqualTo(30);
assertThat(dataSource.isPoolPreparedStatements()).isEqualTo(true);
assertThat(dataSource.getMaxOpenPreparedStatements()).isEqualTo(100);
dataSource.close();
}
use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.
the class JdbcUtilsTest method initDataSource_Transactional_ShouldReturnProperDataSource.
@Test
public void initDataSource_Transactional_ShouldReturnProperDataSource() throws SQLException {
// Arrange
Properties properties = new Properties();
properties.setProperty(DatabaseConfig.CONTACT_POINTS, "jdbc:postgresql://localhost:5432/");
properties.setProperty(DatabaseConfig.USERNAME, "user");
properties.setProperty(DatabaseConfig.PASSWORD, "postgres");
properties.setProperty(DatabaseConfig.STORAGE, "jdbc");
properties.setProperty(JdbcConfig.ISOLATION_LEVEL, "READ_COMMITTED");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MIN_IDLE, "30");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_IDLE, "40");
properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_TOTAL, "50");
properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_ENABLED, "true");
properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_MAX_OPEN, "200");
JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties));
// Act
BasicDataSource dataSource = JdbcUtils.initDataSource(config, true);
// Assert
assertThat(dataSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/");
assertThat(dataSource.getDriver().getClass().getName()).isEqualTo("org.postgresql.Driver");
assertThat(dataSource.getUsername()).isEqualTo("user");
assertThat(dataSource.getPassword()).isEqualTo("postgres");
assertThat(dataSource.getDefaultAutoCommit()).isEqualTo(false);
assertThat(dataSource.getAutoCommitOnReturn()).isEqualTo(false);
assertThat(dataSource.getDefaultTransactionIsolation()).isEqualTo(Connection.TRANSACTION_READ_COMMITTED);
assertThat(dataSource.getMinIdle()).isEqualTo(30);
assertThat(dataSource.getMaxIdle()).isEqualTo(40);
assertThat(dataSource.getMaxTotal()).isEqualTo(50);
assertThat(dataSource.isPoolPreparedStatements()).isEqualTo(true);
assertThat(dataSource.getMaxOpenPreparedStatements()).isEqualTo(200);
dataSource.close();
}
use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.
the class MultiStorageConfigTest method constructor_AllPropertiesGiven_ShouldLoadProperly.
@Test
public void constructor_AllPropertiesGiven_ShouldLoadProperly() {
// Arrange
Properties props = new Properties();
props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
props.setProperty(MultiStorageConfig.STORAGES, "cassandra,mysql");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.storage", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_points", "localhost");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_port", "7000");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.username", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.password", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql,coordinator.state:cassandra");
props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
// Act
MultiStorageConfig config = new MultiStorageConfig(new DatabaseConfig(props));
// Assert
assertThat(config.getDatabasePropertiesMap().size()).isEqualTo(2);
assertThat(config.getDatabasePropertiesMap().containsKey("cassandra")).isTrue();
DatabaseConfig c = new DatabaseConfig(config.getDatabasePropertiesMap().get("cassandra"));
assertThat(c.getStorageClass()).isEqualTo(Cassandra.class);
assertThat(c.getContactPoints()).isEqualTo(Collections.singletonList("localhost"));
assertThat(c.getContactPort()).isEqualTo(7000);
assertThat(c.getUsername().isPresent()).isTrue();
assertThat(c.getUsername().get()).isEqualTo("cassandra");
assertThat(c.getPassword().isPresent()).isTrue();
assertThat(c.getPassword().get()).isEqualTo("cassandra");
assertThat(config.getDatabasePropertiesMap().containsKey("mysql")).isTrue();
c = new DatabaseConfig(config.getDatabasePropertiesMap().get("mysql"));
assertThat(c.getStorageClass()).isEqualTo(JdbcDatabase.class);
assertThat(c.getContactPoints()).isEqualTo(Collections.singletonList("jdbc:mysql://localhost:3306/"));
assertThat(c.getUsername().isPresent()).isTrue();
assertThat(c.getUsername().get()).isEqualTo("root");
assertThat(c.getPassword().isPresent()).isTrue();
assertThat(c.getPassword().get()).isEqualTo("mysql");
assertThat(config.getTableStorageMap().size()).isEqualTo(3);
assertThat(config.getTableStorageMap().get("user.order")).isEqualTo("cassandra");
assertThat(config.getTableStorageMap().get("user.customer")).isEqualTo("mysql");
assertThat(config.getTableStorageMap().get("coordinator.state")).isEqualTo("cassandra");
assertThat(config.getNamespaceStorageMap().size()).isEqualTo(2);
assertThat(config.getNamespaceStorageMap().get("namespace1")).isEqualTo("cassandra");
assertThat(config.getNamespaceStorageMap().get("namespace2")).isEqualTo("mysql");
assertThat(config.getDefaultStorage()).isEqualTo("cassandra");
}
use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.
the class MultiStorageConfigTest method constructor_NestedMultiStorageGiven_ShouldThrowIllegalArgumentException.
@Test
public void constructor_NestedMultiStorageGiven_ShouldThrowIllegalArgumentException() {
// Arrange
Properties props = new Properties();
props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
props.setProperty(MultiStorageConfig.STORAGES, "db,mysql");
// nested
props.setProperty(MultiStorageConfig.STORAGES + ".db.storage", "multis-torage");
props.setProperty(MultiStorageConfig.STORAGES + ".db.contact_points", "localhost");
props.setProperty(MultiStorageConfig.STORAGES + ".db.username", "user");
props.setProperty(MultiStorageConfig.STORAGES + ".db.password", "pass");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql,coordinator.state:cassandra");
props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
// Act Assert
assertThatThrownBy(() -> new MultiStorageConfig(new DatabaseConfig(props))).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.
the class MultiStorageConfigTest method constructor_NonExistentStorageGivenInTableMapping_ShouldThrowIllegalArgumentException.
@Test
public void constructor_NonExistentStorageGivenInTableMapping_ShouldThrowIllegalArgumentException() {
// Arrange
Properties props = new Properties();
props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
props.setProperty(MultiStorageConfig.STORAGES, "cassandra,mysql");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.storage", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_points", "localhost");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_port", "7000");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.username", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.password", "cassandra");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql," + // non-existent storage
"coordinator.state:dynamo");
props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
// Act Assert
assertThatThrownBy(() -> new MultiStorageConfig(new DatabaseConfig(props))).isInstanceOf(IllegalArgumentException.class);
}
Aggregations