Skip to content

Commit

Permalink
HSQLDB ftw :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
zapodot committed May 2, 2018
1 parent fdb9935 commit fbc90a2
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.zapodot.junit.db.plugin;

import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import static org.junit.Assert.*;

public class FlywayInitializerDefaultSettingsHypersqlTest {

@Rule
public final EmbeddedDatabaseRule embeddedDatabaseRule = EmbeddedDatabaseRule.hsqldb()
.initializedByPlugin(
new FlywayInitializer.Builder()
.withLocations(
"classpath:migrations/")
.build()).build();


@Test
public void checkMigrationsHasRun() throws Exception {
try (final Connection connection = embeddedDatabaseRule.getConnection();
final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery("SELECT * FROM USER")) {
assertTrue(resultSet.next());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class FlywayInitializerDefaultSettingsTest {

@Rule
public final EmbeddedDatabaseRule embeddedDatabaseRule = EmbeddedDatabaseRule.builder()
public final EmbeddedDatabaseRule embeddedDatabaseRule = EmbeddedDatabaseRule.h2()
.initializedByPlugin(
new FlywayInitializer.Builder()
.withLocations(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE User (
id INT IDENTITY PRIMARY KEY,
name VARCHAR(255),
created TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP)
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Role (
id INT IDENTITY PRIMARY KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.H2Database;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
Expand All @@ -24,20 +28,32 @@
public class LiquibaseInitializer implements InitializationPlugin {

private final Contexts contexts;

private final LabelExpression labelExpression;

private final String changeLog;

private final ResourceAccessor resourceAccessor;

private final Integer changesLimit;

private final boolean addDbNameToContext;

private final String defaultSchemaName;

public static class Builder {
private String databaseChangeLog;

private ResourceAccessor resourceAccessor;

private List<String> contexts = new LinkedList<>();

private List<String> labels = new LinkedList<>();

private Integer changesToApply;

private boolean addNameToContext = false;

private String defaultSchemaName;

public Builder() {
Expand All @@ -57,6 +73,7 @@ public Builder withChangelogResource(final String resource) {

/**
* Limit the number of changes in the changelog to be applied
*
* @param limit the number of changes
* @return the same builder
*/
Expand Down Expand Up @@ -120,14 +137,19 @@ public Builder withDefaultSchemaName(String schemaName) {
*/
public LiquibaseInitializer build() {
if (databaseChangeLog == null) {
throw new IllegalArgumentException("You must provide a changelog file to the LiquibaseIntitializer Plugin builder");
throw new IllegalArgumentException(
"You must provide a changelog file to the LiquibaseIntitializer Plugin builder");
}
try {
if(resourceAccessor.getResourcesAsStream(databaseChangeLog) == null) {
throw new IllegalArgumentException(String.format("Can not load changelog from resource \"%s\". Does it exist?", databaseChangeLog));
if (resourceAccessor.getResourcesAsStream(databaseChangeLog) == null) {
throw new IllegalArgumentException(String.format(
"Can not load changelog from resource \"%s\". Does it exist?",
databaseChangeLog));
}
} catch (IOException e) {
throw new IllegalArgumentException(String.format("An IO exception occurred while loading changelog from resource \"%s\"", databaseChangeLog), e);
throw new IllegalArgumentException(String.format(
"An IO exception occurred while loading changelog from resource \"%s\"",
databaseChangeLog), e);
}
return new LiquibaseInitializer(createContexts(), createLabels(), databaseChangeLog,
resourceAccessor, changesToApply, addNameToContext,
Expand Down Expand Up @@ -163,10 +185,9 @@ private LiquibaseInitializer(final Contexts contexts,
@Override
public void connectionMade(final String name, final Connection connection) {
if (defaultSchemaName != null) {
try {
connection.prepareStatement(
String.format("CREATE SCHEMA IF NOT EXISTS %s", defaultSchemaName))
.execute();
try (final PreparedStatement preparedStatement = connection.prepareStatement(
String.format("CREATE SCHEMA IF NOT EXISTS %s", defaultSchemaName))) {
preparedStatement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -190,17 +211,21 @@ public void connectionMade(final String name, final Connection connection) {
private Liquibase createLiquibase(final Connection connection) {
try {
JdbcConnection conn = new JdbcConnection(connection);
H2Database h2Database = new H2Database();
h2Database.setConnection(conn);
Database database = resolveDatabase(connection);
database.setConnection(conn);
if (defaultSchemaName != null) {
h2Database.setDefaultSchemaName(defaultSchemaName);
database.setDefaultSchemaName(defaultSchemaName);
}
return new Liquibase(changeLog, resourceAccessor, h2Database);
return new Liquibase(changeLog, resourceAccessor, database);
} catch (LiquibaseException e) {
throw new RuntimeException("Could not initialize Liquibase", e);
}
}

private final Database resolveDatabase(final Connection connection) throws DatabaseException {
return DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
}

/**
* Creates a builder for providing parameters for Liquibase to be run
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;
import org.zapodot.junit.db.plugin.dao.RoleDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

/**
* @author zapodot
Expand All @@ -22,24 +20,16 @@ public class LiquibaseInitializerWithContextTest {
.builder()
.withMode(EmbeddedDatabaseRule.CompatibilityMode.MSSQLServer)
.initializedByPlugin(LiquibaseInitializer.builder()
.withChangelogResource("example-changelog.sql")
.withContexts("addUsersAndRoles")
.build())
.withChangelogResource("example-changelog.sql")
.withContexts("addUsersAndRoles")
.build())
.build();

@Test
public void testFindRolesInsertedByLiquibase() throws Exception {
try(final Connection connection = embeddedDatabaseRule.getConnection()) {
try(final PreparedStatement statement = connection.prepareStatement("Select * FROM ROLE r INNER JOIN USERROLE ur on r.ID = ur.ROLE_ID INNER JOIN USER u on ur.USER_ID = u.ID where u.NAME = ?")) {
statement.setString(1, "Ada");
try(final ResultSet resultSet = statement.executeQuery()) {
final List<String> roles = new LinkedList<>();
while(resultSet.next()) {
roles.add(resultSet.getString("name"));
}
assertEquals(2, roles.size());
}
}
try (final Connection connection = embeddedDatabaseRule.getConnection()) {
final List<String> roles = new RoleDao(connection).rolesForUser("Ada");
assertEquals(2, roles.size());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.zapodot.junit.db.plugin;

import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import static org.junit.Assert.*;

/**
* @author zapodot
*/
public class LiquibaseInitializerWithSchemaNameHyperSQLTest {

@Rule
public final EmbeddedDatabaseRule embeddedDatabaseRule = EmbeddedDatabaseRule
.hsqldb()
.withMode(EmbeddedDatabaseRule.CompatibilityMode.MSSQLServer)
.initializedByPlugin(LiquibaseInitializer.builder()
.withChangelogResource("example-changelog.xml")
.withDefaultSchemaName("some_schema_name")
.build())
.build();

@Test
public void testFindUserTable() throws Exception {
try (final Connection connection = embeddedDatabaseRule.getConnection();
final PreparedStatement preparedStatement = connection
.prepareStatement("SELECT * FROM some_schema_name.USER");
final ResultSet resultSet = preparedStatement.executeQuery()
) {
assertFalse(resultSet.next());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;
import org.zapodot.junit.db.plugin.dao.RoleDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -29,16 +30,8 @@ public class LiquibaseInitializerWithoutContextTest {
@Test
public void testFindRolesInsertedByLiquibase() throws Exception {
try(final Connection connection = embeddedDatabaseRule.getConnection()) {
try(final PreparedStatement statement = connection.prepareStatement("Select * FROM ROLE r INNER JOIN USERROLE ur on r.ID = ur.ROLE_ID INNER JOIN USER u on ur.USER_ID = u.ID where u.NAME = ?")) {
statement.setString(1, "Ada");
try(final ResultSet resultSet = statement.executeQuery()) {
final List<String> roles = new LinkedList<>();
while(resultSet.next()) {
roles.add(resultSet.getString("name"));
}
assertEquals(2, roles.size());
}
}
final List<String> roles = new RoleDao(connection).rolesForUser("Ada");
assertEquals(2, roles.size());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.zapodot.junit.db.plugin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class RoleDao {

private final Connection connection;

public RoleDao(final Connection connection) {
this.connection = connection;
}

public List<String> rolesForUser(final String userName) throws SQLException {
try(final PreparedStatement statement = connection.prepareStatement("Select * FROM ROLE r INNER JOIN USERROLE ur on r.ID = ur.ROLE_ID INNER JOIN USER u on ur.USER_ID = u.ID where u.NAME = ?")) {
statement.setString(1, userName);
try(final ResultSet resultSet = statement.executeQuery()) {
final List<String> roles = new LinkedList<>();
while(resultSet.next()) {
roles.add(resultSet.getString("name"));
}
return roles;
}
}
}
}
Loading

0 comments on commit fbc90a2

Please sign in to comment.