Skip to content

Add tests for Junit 5 and fix PreparedDbExtension instance(non-static) lifecycle. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [8, 11, 13, 14]
java: [8, 11, 13, 14, 15]
steps:
- name: Checkout project
uses: actions/checkout@v1
Expand Down
20 changes: 18 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,22 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<version>5.7.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -179,6 +191,10 @@

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
Expand Down Expand Up @@ -239,7 +255,7 @@
</execution>
</executions>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package io.zonky.test.db.postgres.junit5;

import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import org.junit.rules.TestRule;
import org.junit.jupiter.api.extension.Extension;

public final class EmbeddedPostgresExtension {

Expand All @@ -28,7 +28,7 @@ public static SingleInstancePostgresExtension singleInstance() {
}

/**
* Returns a {@link TestRule} to create a Postgres cluster, shared amongst all test cases in this JVM.
* Returns a {@link Extension} to create a Postgres cluster, shared amongst all test cases in this JVM.
* The rule contributes Config switches to configure each test case to get its own database.
*/
public static PreparedDbExtension preparedDatabase(DatabasePreparer preparer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
import io.zonky.test.db.postgres.embedded.PreparedDbProvider;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.*;

import javax.sql.DataSource;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

public class PreparedDbExtension implements BeforeAllCallback, AfterAllCallback {
public class PreparedDbExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {

private final DatabasePreparer preparer;
private boolean perClass = false;
private volatile DataSource dataSource;
private volatile PreparedDbProvider provider;
private volatile ConnectionInfo connectionInfo;
Expand All @@ -55,13 +54,33 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
provider = PreparedDbProvider.forPreparer(preparer, builderCustomizers);
connectionInfo = provider.createNewDatabase();
dataSource = provider.createDataSourceFromConnectionInfo(connectionInfo);
perClass = true;
}

@Override
public void afterAll(ExtensionContext extensionContext) {
dataSource = null;
connectionInfo = null;
provider = null;
perClass = false;
}

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
if (!perClass) {
provider = PreparedDbProvider.forPreparer(preparer, builderCustomizers);
connectionInfo = provider.createNewDatabase();
dataSource = provider.createDataSourceFromConnectionInfo(connectionInfo);
}
}

@Override
public void afterEach(ExtensionContext extensionContext) {
if (!perClass) {
dataSource = null;
connectionInfo = null;
provider = null;
}
}

public DataSource getTestDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
*/
package io.zonky.test.db.postgres.embedded;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class EmbeddedPostgresTest
{
@Rule
public TemporaryFolder tf = new TemporaryFolder();
@TempDir
public Path tf;

@Test
public void testEmbeddedPg() throws Exception
Expand All @@ -47,7 +48,7 @@ public void testEmbeddedPg() throws Exception
public void testEmbeddedPgCreationWithNestedDataDirectory() throws Exception
{
try (EmbeddedPostgres pg = EmbeddedPostgres.builder()
.setDataDirectory(tf.newFolder("data-dir-parent") + "/data-dir")
.setDataDirectory(Files.createDirectories(tf.resolve("data-dir-parent").resolve("data-dir")))
.start()) {
// nothing to do
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;
import io.zonky.test.db.postgres.embedded.ConnectionInfo;
import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import org.junit.Rule;
import org.junit.Test;
import org.postgresql.ds.common.BaseDataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import static org.junit.Assert.assertEquals;

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

import io.zonky.test.db.postgres.embedded.FlywayPreparer;
import org.junit.Rule;
import org.junit.Test;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;

public class FlywayPreparerTest {
@Rule
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.forClasspathLocation("db/testing"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -20,9 +20,6 @@
import org.junit.Rule;
import org.junit.Test;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.SingleInstancePostgresRule;

public class IsolationTest
{
@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
import liquibase.Contexts;
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
import org.junit.Rule;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;
import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import org.junit.Rule;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import static org.junit.Assert.assertEquals;

Expand All @@ -24,12 +24,12 @@

import javax.sql.DataSource;

import io.zonky.test.db.postgres.embedded.ConnectionInfo;
import io.zonky.test.db.postgres.embedded.DatabaseConnectionPreparer;
import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import org.junit.Rule;
import org.junit.Test;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.PreparedDbRule;

public class PreparedDbTest {

private final DatabasePreparer prepA = new SimplePreparer("a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zonky.test.db.postgres.embedded;
package io.zonky.test.db.postgres.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -24,9 +24,6 @@
import org.junit.Rule;
import org.junit.Test;

import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
import io.zonky.test.db.postgres.junit.SingleInstancePostgresRule;

public class SingleInstanceRuleTest
{
@Rule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.zonky.test.db.postgres.junit5;

import io.zonky.test.db.postgres.embedded.ConnectionInfo;
import io.zonky.test.db.postgres.embedded.DatabasePreparer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.postgresql.ds.common.BaseDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ConnectConfigTest {

private final CapturingDatabasePreparer preparer = new CapturingDatabasePreparer();

@RegisterExtension
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(preparer)
.customize(builder -> builder.setConnectConfig("connectTimeout", "20"));

@Test
public void test() throws SQLException {
ConnectionInfo connectionInfo = db.getConnectionInfo();

Map<String, String> properties = connectionInfo.getProperties();
assertEquals(1, properties.size());
assertEquals("20", properties.get("connectTimeout"));

BaseDataSource testDatabase = (BaseDataSource) db.getTestDatabase();
assertEquals("20", testDatabase.getProperty("connectTimeout"));

BaseDataSource preparerDataSource = (BaseDataSource) preparer.getDataSource();
assertEquals("20", preparerDataSource.getProperty("connectTimeout"));
}

private class CapturingDatabasePreparer implements DatabasePreparer {

private DataSource dataSource;

@Override
public void prepare(DataSource ds) {
if (dataSource != null)
throw new IllegalStateException("database preparer has been called multiple times");
dataSource = ds;
}

public DataSource getDataSource() {
return dataSource;
}
}
}
Loading