diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..c9b2105b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + - package-ecosystem: "maven" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c191acc4..096409a0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: master fetch-depth: 0 @@ -17,7 +17,7 @@ jobs: git config user.name "Zonky Bot" git config user.email "bot@zonky.com" - name: Set up JDK - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: 8 @@ -32,4 +32,4 @@ jobs: MAVEN_USER: ${{ secrets.MAVEN_USER }} MAVEN_PASS: ${{ secrets.MAVEN_PASS }} SIGNING_PASS: ${{ secrets.SIGNING_PASS }} - run: mvn -B release:prepare release:perform -DreleaseVersion=$RELEASE_VERSION \ No newline at end of file + run: mvn -B release:prepare release:perform -DreleaseVersion=$RELEASE_VERSION diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dad6783a..84b53f3a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,12 +6,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - java: [8, 11, 13, 14, 15] + java: [8, 11, 17, 21, 22] # LTS and newer versions steps: - name: Checkout project - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up JDK - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: ${{ matrix.java }} diff --git a/README.md b/README.md index 1b3ba398..54d91e0a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ ## Introduction -This project is a fork of [OpenTable Embedded PostgreSQL Component](https://github.com/opentable/otj-pg-embedded) created due to inactivity of maintainers. +This project is a fork of [OpenTable Embedded PostgreSQL Component](https://github.com/opentable/otj-pg-embedded) created back in 2018. The original +project continues, but with a very different philosophy - wrapping the postgres instance in a docker container. +Whereas this project follows the original approach of using native postgres binaries running directly on the target platform without the overhead of virtualization. The library allows embedding PostgreSQL into Java application code with no external dependencies. Excellent for allowing you to unit test with a "real" Postgres without requiring end users to install and set up a database cluster. @@ -24,12 +26,12 @@ Add the following Maven dependency: io.zonky.test embedded-postgres - 2.0.0 + 2.1.0 test ``` -The default version of the embedded postgres is `PostgreSQL 14.3`, but you can change it by following the instructions described in [Postgres version](#postgres-version). +The default version of the embedded postgres is `PostgreSQL 14.15`, but you can change it by following the instructions described in [Postgres version](#postgres-version). ## Basic Usage @@ -71,7 +73,7 @@ independent databases gives you. ## Postgres version -The default version of the embedded postgres is `PostgreSQL 14.3`, but it can be changed by importing `embedded-postgres-binaries-bom` in a required version into your dependency management section. +The default version of the embedded postgres is `PostgreSQL 14.15`, but it can be changed by importing `embedded-postgres-binaries-bom` in a required version into your dependency management section. ```xml @@ -79,7 +81,7 @@ The default version of the embedded postgres is `PostgreSQL 14.3`, but it can be io.zonky.test.postgres embedded-postgres-binaries-bom - 13.7.0 + 17.2.0 pom import @@ -97,7 +99,7 @@ The default version of the embedded postgres is `PostgreSQL 14.3`, but it can be configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'io.zonky.test.postgres') { - details.useVersion '13.7.0' + details.useVersion '17.2.0' } } } @@ -105,18 +107,18 @@ The default version of the embedded postgres is `PostgreSQL 14.3`, but it can be 2. If you use Gradle 5+, [Maven BOMs are supported out of the box](https://docs.gradle.org/5.0/userguide/managing_transitive_dependencies.html#sec:bom_import), so you can import the bom: dependencies { - implementation enforcedPlatform('io.zonky.test.postgres:embedded-postgres-binaries-bom:13.7.0') + testImplementation enforcedPlatform('io.zonky.test.postgres:embedded-postgres-binaries-bom:17.2.0') } 3. Or, you can use [Spring's dependency management plugin](https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#dependency-management-configuration-bom-import) that provides Maven-like dependency management to Gradle: plugins { - id "io.spring.dependency-management" version "1.0.6.RELEASE" + id "io.spring.dependency-management" version "1.1.0" } dependencyManagement { imports { - mavenBom 'io.zonky.test.postgres:embedded-postgres-binaries-bom:13.7.0' + mavenBom 'io.zonky.test.postgres:embedded-postgres-binaries-bom:17.2.0' } } @@ -210,5 +212,8 @@ Below are some examples of how to prepare a docker image running with a non-root + +If the above do not resolve your error, verify that the correct locales are available in your container. For example, many variants of AlmaLinux:9 do not come with `glibc-langpack-en`. This will lead to misleading errors during `initdb`. Additionally, you can optionally set your locale with `setLocaleConfig()` when building your EmbeddedPostgres instance. + ## License The project is released under version 2.0 of the [Apache License](http://www.apache.org/licenses/LICENSE-2.0.html). diff --git a/pom.xml b/pom.xml index 98dde0ab..9e4b59ac 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ io.zonky.test embedded-postgres - 2.0.1 + 2.1.1-SNAPSHOT embedded-postgres Embedded PostgreSQL Server @@ -30,7 +30,27 @@ UTF-8 UTF-8 - 14.5.0 + 14.15.0 + 1.17.1 + 1.26.2 + 2.16.1 + 3.15.0 + 9.22.3 + 5.10.5 + 4.13.2 + 4.30.0 + 4.11.0 + 7.4.0 + 42.7.4 + 1.7.36 + 1.10 + 3.5.0 + 1.6 + 3.8.0 + 3.24.0 + 3.1.1 + 3.3.1 + 3.3.1 https://github.com/zonkyio/embedded-postgres @@ -39,7 +59,7 @@ scm:git:https://github.com/zonkyio/embedded-postgres.git scm:git:https://github.com/zonkyio/embedded-postgres.git https://github.com/zonkyio/embedded-postgres - v2.0.1 + HEAD @@ -71,6 +91,37 @@ + + + + commons-io + commons-io + ${commons-io.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.apache.commons + commons-compress + ${commons-compress.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + org.junit + junit-bom + ${junit5.version} + pom + import + + + io.zonky.test.postgres @@ -100,100 +151,117 @@ org.slf4j slf4j-api - 1.7.36 + ${slf4j.version} org.apache.commons commons-lang3 - 3.12.0 org.apache.commons commons-compress - 1.21 org.tukaani xz - 1.9 + ${xz.version} commons-io commons-io - 2.11.0 commons-codec commons-codec - 1.15 org.flywaydb flyway-core - 7.15.0 + ${flyway.version} true org.liquibase liquibase-core - 4.10.0 + ${liquibase.version} true org.postgresql postgresql - 42.3.5 + ${postgresql.version} junit junit - 4.13.2 + ${junit4.version} provided true org.junit.jupiter junit-jupiter-api - 5.8.2 provided true org.junit.vintage junit-vintage-engine - 5.8.2 test org.junit.jupiter junit-jupiter-engine - 5.8.2 test org.slf4j slf4j-simple - 1.7.36 + ${slf4j.version} test org.mockito mockito-core - 3.12.4 + ${mockito.version} test + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-versions + validate + + enforce + + + + + + + + + maven-surefire-plugin - 2.22.2 + ${maven-surefire-plugin.version} + + + ${env.LC_ALL} + + maven-pmd-plugin - 3.13.0 + ${maven-pmd-plugin.version} verify @@ -206,13 +274,13 @@ net.sourceforge.pmd pmd-core - 6.25.0 + ${pmd.version} compile net.sourceforge.pmd pmd-java - 6.25.0 + ${pmd.version} compile @@ -220,7 +288,7 @@ false true 1.8 - UTF-8 + UTF-8 100 4 @@ -228,7 +296,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + ${maven-source-plugin.version} attach-sources @@ -241,7 +309,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + ${maven-javadoc-plugin.version} attach-javadocs @@ -257,7 +325,7 @@ org.apache.maven.plugins maven-release-plugin - 2.5.3 + ${maven-release-plugin.version} false release @@ -268,6 +336,21 @@ + + + default-lc-all + + + !env.LC_ALL + + + Unix + + + + en_US.UTF-8 + + release @@ -275,7 +358,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.6 + ${maven-gpg-plugin.version} sign-artifacts diff --git a/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java b/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java index aa295a7e..b90fda68 100644 --- a/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java +++ b/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java @@ -63,12 +63,13 @@ public InputStream getPgBinary(String system, String machineHardware) throws IOE return resource.getInputStream(); } - if (StringUtils.equals(system, "Darwin") && StringUtils.equals(machineHardware, "aarch64")) { + if ((StringUtils.equals(system, "Darwin") && StringUtils.equals(machineHardware, "aarch64")) // NOPMD + || (StringUtils.equals(system, "Windows") && StringUtils.equals(architecture, "arm_64"))) { // NOPMD resource = findPgBinary(normalize(format("postgres-%s-%s.txz", system, "x86_64"))); if (resource != null) { - logger.warn("No native binaries supporting aarch64 architecture found. " + - "Trying to use binaries for amd64 architecture instead: '{}'. " + - "Make sure you have Rosetta 2 emulation enabled. " + + logger.warn("No native binaries supporting ARM architecture found. " + + "Trying to use binaries for x64 architecture instead: '{}'. " + + "Make sure you have enabled emulation for this purpose. " + "Note that performance may be degraded.", resource.getFilename()); return resource.getInputStream(); } diff --git a/src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java b/src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java index 434f7970..9e6b0c0d 100644 --- a/src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java +++ b/src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java @@ -13,6 +13,21 @@ */ package io.zonky.test.db.postgres.embedded; +import io.zonky.test.db.postgres.util.LinuxUtils; +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.lang3.time.StopWatch; +import org.postgresql.ds.PGSimpleDataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.tukaani.xz.XZInputStream; + +import javax.sql.DataSource; import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.File; @@ -58,23 +73,6 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Stream; -import javax.sql.DataSource; - -import org.apache.commons.codec.binary.Hex; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.output.ByteArrayOutputStream; -import org.apache.commons.lang3.SystemUtils; -import org.apache.commons.lang3.time.StopWatch; -import org.postgresql.ds.PGSimpleDataSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.tukaani.xz.XZInputStream; - -import io.zonky.test.db.postgres.util.LinuxUtils; - import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.WRITE; import static java.util.Collections.unmodifiableMap; @@ -110,6 +108,7 @@ public class EmbeddedPostgres implements Closeable private final ProcessBuilder.Redirect errorRedirector; private final ProcessBuilder.Redirect outputRedirector; + private final Process process; EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory, Map postgresConfig, Map localeConfig, int port, Map connectConfig, @@ -160,7 +159,11 @@ public class EmbeddedPostgres implements Closeable } lock(); - startPostmaster(); + this.process = startPostmaster(); + } + + public Process getProcess() { + return this.process; } public DataSource getTemplateDatabase() @@ -222,7 +225,7 @@ Map getConnectConfig() private static int detectPort() throws IOException { try (ServerSocket socket = new ServerSocket(0)) { - while(!socket.isBound()) { + while (!socket.isBound()) { Thread.sleep(50); } return socket.getLocalPort(); @@ -253,7 +256,7 @@ private void initdb() LOG.info("{} initdb completed in {}", instanceId, watch); } - private void startPostmaster() throws IOException + private Process startPostmaster() throws IOException { final StopWatch watch = new StopWatch(); watch.start(); @@ -262,11 +265,14 @@ private void startPostmaster() throws IOException } final List args = new ArrayList<>(); - args.addAll(Arrays.asList("-D", dataDirectory.getPath())); - args.addAll(createInitOptions()); + args.addAll(Arrays.asList( + "-D", dataDirectory.getPath(), + "-o", String.join(" ", createInitOptions()), + "-w", "start" + )); final ProcessBuilder builder = new ProcessBuilder(); - POSTGRES.applyTo(builder, args); + PG_CTL.applyTo(builder, args); builder.redirectErrorStream(true); builder.redirectError(errorRedirector); @@ -282,6 +288,7 @@ private void startPostmaster() throws IOException Runtime.getRuntime().addShutdownHook(newCloserThread()); waitForServerStartup(watch); + return postmaster; } private List createInitOptions() @@ -802,23 +809,19 @@ private static File prepareBinaries(PgBinaryResolver pgBinaryResolver, File over mkdirs(pgDir); workingDirectory.setWritable(true, false); - final File unpackLockFile = new File(pgDir, LOCK_FILE_NAME); final File pgDirExists = new File(pgDir, ".exists"); if (!isPgBinReady(pgDirExists)) { + File unpackLockFile = new File(pgDir, LOCK_FILE_NAME); try (FileOutputStream lockStream = new FileOutputStream(unpackLockFile); FileLock unpackLock = lockStream.getChannel().tryLock()) { if (unpackLock != null) { - try { - LOG.info("Extracting Postgres..."); - try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { - extractTxz(bais, pgDir); - } - if (!pgDirExists.createNewFile()) { - pgDirExists.setLastModified(System.currentTimeMillis()); - } - } catch (Exception e) { - LOG.error("while unpacking Postgres", e); + LOG.info("Extracting Postgres..."); + try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { + extractTxz(bais, pgDir); + } + if (!pgDirExists.createNewFile()) { + pgDirExists.setLastModified(System.currentTimeMillis()); } } else { // the other guy is unpacking for us. @@ -837,6 +840,7 @@ private static File prepareBinaries(PgBinaryResolver pgBinaryResolver, File over } } } catch (final IOException | NoSuchAlgorithmException e) { + LOG.error("Got error while unpacking Postgres", e); throw new ExceptionInInitializerError(e); } catch (final InterruptedException ie) { Thread.currentThread().interrupt(); diff --git a/src/main/java/io/zonky/test/db/postgres/embedded/FlywayPreparer.java b/src/main/java/io/zonky/test/db/postgres/embedded/FlywayPreparer.java index d36afcac..f7fecf4d 100644 --- a/src/main/java/io/zonky/test/db/postgres/embedded/FlywayPreparer.java +++ b/src/main/java/io/zonky/test/db/postgres/embedded/FlywayPreparer.java @@ -13,34 +13,53 @@ */ package io.zonky.test.db.postgres.embedded; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - -import javax.sql.DataSource; - import org.apache.commons.lang3.reflect.MethodUtils; import org.flywaydb.core.Flyway; import org.flywaydb.core.api.configuration.FluentConfiguration; -// TODO: Detect missing migration files. -// cf. https://github.com/flyway/flyway/issues/1496 -// There is also a related @Ignored test in otj-sql. +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; public final class FlywayPreparer implements DatabasePreparer { private final FluentConfiguration configuration; private final List locations; + private final Map properties; + /** + * Creates a new instance of the preparer with the specified locations of migrations. + */ public static FlywayPreparer forClasspathLocation(String... locations) { FluentConfiguration config = Flyway.configure().locations(locations); - return new FlywayPreparer(config, Arrays.asList(locations)); + return new FlywayPreparer(config, Arrays.asList(locations), null); } - private FlywayPreparer(FluentConfiguration configuration, List locations) { + /** + * Creates a new instance of the preparer with the specified configuration properties. + * + *

Example of use: + *

 {@code
+     *     FlywayPreparer preparer = FlywayPreparer.fromConfiguration(Map.of(
+     *             "flyway.locations", "db/migration",
+     *             "flyway.postgresql.transactional.lock", "false"));
+     * }
+ * + * A list of all available configuration properties can be found here. + */ + public static FlywayPreparer fromConfiguration(Map configuration) { + FluentConfiguration config = Flyway.configure().configuration(configuration); + return new FlywayPreparer(config, null, new HashMap<>(configuration)); + } + + private FlywayPreparer(FluentConfiguration configuration, List locations, Map properties) { this.configuration = configuration; this.locations = locations; + this.properties = properties; } @Override @@ -55,15 +74,15 @@ public void prepare(DataSource ds) throws SQLException { } @Override - public boolean equals(Object obj) { - if (! (obj instanceof FlywayPreparer)) { - return false; - } - return Objects.equals(locations, ((FlywayPreparer) obj).locations); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + FlywayPreparer that = (FlywayPreparer) o; + return Objects.equals(locations, that.locations) && Objects.equals(properties, that.properties); } @Override public int hashCode() { - return Objects.hashCode(locations); + return Objects.hash(locations, properties); } } diff --git a/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java b/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java index 7d0c2de3..d611681e 100644 --- a/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java +++ b/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java @@ -19,8 +19,11 @@ import liquibase.database.jvm.JdbcConnection; import liquibase.exception.LiquibaseException; import liquibase.resource.ClassLoaderResourceAccessor; +import liquibase.resource.FileSystemResourceAccessor; +import liquibase.resource.ResourceAccessor; import javax.sql.DataSource; +import java.io.File; import java.sql.Connection; import java.sql.SQLException; import java.util.Objects; @@ -30,45 +33,60 @@ public final class LiquibasePreparer implements DatabasePreparer { private final String location; + private final ResourceAccessor accessor; private final Contexts contexts; public static LiquibasePreparer forClasspathLocation(String location) { - return new LiquibasePreparer(location, new Contexts()); + return forClasspathLocation(location, null); } + public static LiquibasePreparer forClasspathLocation(String location, Contexts contexts) { - return new LiquibasePreparer(location, contexts); + return new LiquibasePreparer(location, new ClassLoaderResourceAccessor(), contexts); + } + + public static LiquibasePreparer forFile(File file) { + return forFile(file, null); + } + + public static LiquibasePreparer forFile(File file, Contexts contexts) { + if (file == null) + throw new IllegalArgumentException("Missing file"); + File dir = file.getParentFile(); + if (dir == null) + throw new IllegalArgumentException("Cannot get parent dir from file"); + + return new LiquibasePreparer(file.getName(), new FileSystemResourceAccessor(dir), contexts); } - private LiquibasePreparer(String location, Contexts contexts) { + private LiquibasePreparer(String location, ResourceAccessor accessor, Contexts contexts) { this.location = location; - this.contexts = contexts; + this.accessor = accessor; + this.contexts = contexts != null ? contexts : new Contexts(); } @Override public void prepare(DataSource ds) throws SQLException { - Connection connection = null; - try { - connection = ds.getConnection(); + try (Connection connection = ds.getConnection()) { Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); - Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database); + Liquibase liquibase = new Liquibase(location, accessor, database); liquibase.update(contexts); } catch (LiquibaseException e) { throw new SQLException(e); - } finally { - if (connection != null) { - connection.rollback(); - connection.close(); - } } } @Override - public boolean equals(Object obj) { - return obj instanceof LiquibasePreparer && Objects.equals(location, ((LiquibasePreparer) obj).location); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LiquibasePreparer that = (LiquibasePreparer) o; + return Objects.equals(location, that.location) + && Objects.equals(accessor, that.accessor) + && Objects.equals(contexts.getContexts(), that.contexts.getContexts()); } @Override public int hashCode() { - return Objects.hashCode(location); + return Objects.hash(location, accessor, contexts.getContexts()); } } diff --git a/src/main/resources/sh/detect_linux_distribution.sh b/src/main/resources/sh/detect_linux_distribution.sh index 2ef4f015..8304880a 100644 --- a/src/main/resources/sh/detect_linux_distribution.sh +++ b/src/main/resources/sh/detect_linux_distribution.sh @@ -12,7 +12,7 @@ detect_linux_distribution() { else DISTRO='' fi - echo $DISTRO + echo "$DISTRO" } -echo $(detect_linux_distribution) \ No newline at end of file +detect_linux_distribution diff --git a/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java index ff765eef..cebb9376 100644 --- a/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java +++ b/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java @@ -18,6 +18,7 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; +import java.util.Collections; import io.zonky.test.db.postgres.embedded.FlywayPreparer; import org.junit.Rule; @@ -25,7 +26,8 @@ public class FlywayPreparerTest { @Rule - public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.forClasspathLocation("db/testing")); + public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.fromConfiguration( + Collections.singletonMap("flyway.locations", "db/testing"))); @Test public void testTablesMade() throws Exception { diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java similarity index 96% rename from src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java rename to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java index d9b9908f..647a514d 100644 --- a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java +++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java @@ -24,7 +24,7 @@ import static org.junit.Assert.assertEquals; -public class LiquibasePreparerContextTest { +public class LiquibasePreparerClasspathContextTest { @Rule public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test"))); diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java similarity index 96% rename from src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java rename to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java index 4c646ca5..07156b36 100644 --- a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java +++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java @@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals; -public class LiquibasePreparerTest { +public class LiquibasePreparerClasspathTest { @Rule public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml")); diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java new file mode 100644 index 00000000..f1b38523 --- /dev/null +++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java @@ -0,0 +1,42 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.zonky.test.db.postgres.junit; + +import io.zonky.test.db.postgres.embedded.LiquibasePreparer; +import liquibase.Contexts; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.junit.Assert.assertEquals; + +public class LiquibasePreparerFileContextTest { + + @Rule + public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test"))); + + @Test + public void testEmptyTables() throws Exception { + try (Connection c = db.getTestDatabase().getConnection(); + Statement s = c.createStatement()) { + ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo"); + rs.next(); + assertEquals(0, rs.getInt(1)); + } + } +} diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java new file mode 100644 index 00000000..233bda84 --- /dev/null +++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.zonky.test.db.postgres.junit; + +import io.zonky.test.db.postgres.embedded.LiquibasePreparer; +import org.junit.Rule; +import org.junit.Test; + +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.junit.Assert.assertEquals; + +public class LiquibasePreparerFileTest { + + @Rule + public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml"))); + + @Test + public void testTablesMade() throws Exception { + try (Connection c = db.getTestDatabase().getConnection(); + Statement s = c.createStatement()) { + ResultSet rs = s.executeQuery("SELECT * FROM foo"); + rs.next(); + assertEquals("bar", rs.getString(1)); + } + } +} diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java similarity index 96% rename from src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java rename to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java index bd229daa..436b64e8 100644 --- a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java +++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java @@ -24,7 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class LiquibasePreparerContextTest { +public class LiquibasePreparerClasspathContextTest { @RegisterExtension public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test"))); diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java similarity index 96% rename from src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java rename to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java index 8653d488..1319ee81 100644 --- a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java +++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java @@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class LiquibasePreparerTest { +public class LiquibasePreparerClasspathTest { @RegisterExtension public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml")); diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java new file mode 100644 index 00000000..837792bd --- /dev/null +++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java @@ -0,0 +1,42 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.zonky.test.db.postgres.junit5; + +import io.zonky.test.db.postgres.embedded.LiquibasePreparer; +import liquibase.Contexts; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LiquibasePreparerFileContextTest { + + @RegisterExtension + public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test"))); + + @Test + public void testEmptyTables() throws Exception { + try (Connection c = db.getTestDatabase().getConnection(); + Statement s = c.createStatement()) { + ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo"); + rs.next(); + assertEquals(0, rs.getInt(1)); + } + } +} diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java new file mode 100644 index 00000000..8c0c5378 --- /dev/null +++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.zonky.test.db.postgres.junit5; + +import io.zonky.test.db.postgres.embedded.LiquibasePreparer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LiquibasePreparerFileTest { + + @RegisterExtension + public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml"))); + + @Test + public void testTablesMade() throws Exception { + try (Connection c = db.getTestDatabase().getConnection(); + Statement s = c.createStatement()) { + ResultSet rs = s.executeQuery("SELECT * FROM foo"); + rs.next(); + assertEquals("bar", rs.getString(1)); + } + } +} diff --git a/src/test/resources/liqui/master-test.xml b/src/test/resources/liqui/master-test.xml index 94fe4963..3670a29b 100644 --- a/src/test/resources/liqui/master-test.xml +++ b/src/test/resources/liqui/master-test.xml @@ -17,7 +17,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> - + Delete from `foo` table @@ -29,4 +29,4 @@ INSERT INTO foo VALUES('bar'); - \ No newline at end of file +