├── .github └── workflows │ ├── release.yml │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── accept-third-party-license.sh ├── build.gradle ├── embedded-database-spring-test-autoconfigure └── src │ └── main │ └── resources │ └── META-INF │ └── spring.factories ├── embedded-database-spring-test └── src │ ├── main │ ├── java │ │ └── io │ │ │ └── zonky │ │ │ └── test │ │ │ └── db │ │ │ ├── AutoConfigureEmbeddedDatabase.java │ │ │ ├── AutoConfigureEmbeddedDatabases.java │ │ │ ├── EmbeddedDatabaseContextCustomizerFactory.java │ │ │ ├── EmbeddedDatabaseTestExecutionListener.java │ │ │ ├── config │ │ │ ├── ConditionalOnClass.java │ │ │ ├── ConditionalOnMissingBean.java │ │ │ ├── DatabaseProviderFactory.java │ │ │ ├── EmbeddedDatabaseAutoConfiguration.java │ │ │ ├── EmbeddedDatabaseCondition.java │ │ │ ├── MissingDatabaseProviderException.java │ │ │ ├── MissingProviderDependencyException.java │ │ │ ├── OnBeanCondition.java │ │ │ ├── OnClassCondition.java │ │ │ └── Provider.java │ │ │ ├── context │ │ │ ├── DatabaseContext.java │ │ │ ├── DatabaseTargetSource.java │ │ │ ├── DefaultDatabaseContext.java │ │ │ └── EmbeddedDatabaseFactoryBean.java │ │ │ ├── event │ │ │ ├── EventPublishingTestExecutionListener.java │ │ │ ├── TestExecutionFinishedEvent.java │ │ │ └── TestExecutionStartedEvent.java │ │ │ ├── flyway │ │ │ ├── FlywayClassUtils.java │ │ │ ├── FlywayDatabaseExtension.java │ │ │ ├── FlywayDescriptor.java │ │ │ ├── FlywayPropertiesPostProcessor.java │ │ │ ├── FlywayVersion.java │ │ │ ├── FlywayWrapper.java │ │ │ ├── OptimizedFlywayTestExecutionListener.java │ │ │ └── preparer │ │ │ │ ├── BaselineFlywayDatabasePreparer.java │ │ │ │ ├── CleanFlywayDatabasePreparer.java │ │ │ │ ├── FlywayDatabasePreparer.java │ │ │ │ └── MigrateFlywayDatabasePreparer.java │ │ │ ├── liquibase │ │ │ ├── LiquibaseDatabaseExtension.java │ │ │ ├── LiquibaseDatabasePreparer.java │ │ │ ├── LiquibaseDescriptor.java │ │ │ └── LiquibasePropertiesPostProcessor.java │ │ │ ├── logging │ │ │ └── EmbeddedDatabaseReporter.java │ │ │ ├── preparer │ │ │ ├── CompositeDatabasePreparer.java │ │ │ ├── DatabasePreparer.java │ │ │ ├── RecordingDataSource.java │ │ │ ├── RecordingMethodInterceptor.java │ │ │ └── ReplayableDatabasePreparer.java │ │ │ ├── provider │ │ │ ├── DatabaseProvider.java │ │ │ ├── DatabaseRequest.java │ │ │ ├── DatabaseTemplate.java │ │ │ ├── EmbeddedDatabase.java │ │ │ ├── ProviderException.java │ │ │ ├── TemplatableDatabaseProvider.java │ │ │ ├── common │ │ │ │ ├── OptimizingDatabaseProvider.java │ │ │ │ ├── PrefetchingDatabaseProvider.java │ │ │ │ └── TemplatingDatabaseProvider.java │ │ │ ├── derby │ │ │ │ ├── DerbyDatabaseProvider.java │ │ │ │ └── DerbyEmbeddedDatabase.java │ │ │ ├── h2 │ │ │ │ ├── H2DatabaseProvider.java │ │ │ │ └── H2EmbeddedDatabase.java │ │ │ ├── hsqldb │ │ │ │ ├── HSQLDatabaseProvider.java │ │ │ │ └── HSQLEmbeddedDatabase.java │ │ │ ├── mariadb │ │ │ │ ├── DockerMariaDBDatabaseProvider.java │ │ │ │ ├── MariaDBContainerCustomizer.java │ │ │ │ └── MariaDBEmbeddedDatabase.java │ │ │ ├── mssql │ │ │ │ ├── DockerMSSQLDatabaseProvider.java │ │ │ │ ├── MSSQLServerContainerCustomizer.java │ │ │ │ └── MsSQLEmbeddedDatabase.java │ │ │ ├── mysql │ │ │ │ ├── DockerMySQLDatabaseProvider.java │ │ │ │ ├── MySQLContainerCustomizer.java │ │ │ │ └── MySQLEmbeddedDatabase.java │ │ │ ├── postgres │ │ │ │ ├── DockerPostgresDatabaseProvider.java │ │ │ │ ├── OpenTablePostgresDatabaseProvider.java │ │ │ │ ├── PostgreSQLContainerCustomizer.java │ │ │ │ ├── PostgresEmbeddedDatabase.java │ │ │ │ ├── YandexPostgresDatabaseProvider.java │ │ │ │ └── ZonkyPostgresDatabaseProvider.java │ │ │ └── support │ │ │ │ ├── AbstractEmbeddedDatabase.java │ │ │ │ ├── BlockingDatabaseWrapper.java │ │ │ │ └── SimpleDatabaseTemplate.java │ │ │ ├── support │ │ │ ├── DatabaseDefinition.java │ │ │ ├── DatabaseProviders.java │ │ │ ├── DefaultProviderResolver.java │ │ │ ├── ProviderDescriptor.java │ │ │ └── ProviderResolver.java │ │ │ └── util │ │ │ ├── AnnotationUtils.java │ │ │ ├── AopProxyUtils.java │ │ │ ├── PropertyUtils.java │ │ │ ├── RandomStringUtils.java │ │ │ ├── ReflectionUtils.java │ │ │ └── StringUtils.java │ └── resources │ │ └── META-INF │ │ └── spring-configuration-metadata.json │ └── test │ ├── java │ └── io │ │ └── zonky │ │ └── test │ │ ├── category │ │ ├── DerbyTestSuite.java │ │ ├── FlywayTestSuite.java │ │ ├── H2TestSuite.java │ │ ├── HSQLTestSuite.java │ │ ├── LiquibaseTestSuite.java │ │ ├── MSSQLTestSuite.java │ │ ├── MariaDBTestSuite.java │ │ ├── MySQLTestSuite.java │ │ ├── PostgresTestSuite.java │ │ └── SpringTestSuite.java │ │ ├── db │ │ ├── AsyncFlywayInitializationIntegrationTest.java │ │ ├── ConnectionLeakIntegrationTest.java │ │ ├── DatabaseIsolationIntegrationTest.java │ │ ├── DatabaseRefreshIntegrationTest.java │ │ ├── DatabaseRefreshPropertyIntegrationTest.java │ │ ├── DatabaseReplacePropertyIntegrationTest.java │ │ ├── EarlyInitializationIntegrationTest.java │ │ ├── FlywayMethodRefreshIntegrationTest.java │ │ ├── FlywayMigrationIntegrationTest.java │ │ ├── FlywayRefreshIntegrationTest.java │ │ ├── FlywayTransactionalIntegrationTest.java │ │ ├── LiquibaseRefreshIntegrationTest.java │ │ ├── MultipleDataSourcesIntegrationTest.java │ │ ├── MultipleDatabasesIntegrationTest.java │ │ ├── MultipleFlywayBeansClassLevelIntegrationTest.java │ │ ├── MultipleFlywayBeansContextInitializationIntegrationTest.java │ │ ├── MultipleFlywayBeansMethodLevelIntegrationTest.java │ │ ├── ReplacingDefaultDataSourceIntegrationTest.java │ │ ├── SpringBootCustomDatabaseIntegrationTest.java │ │ ├── SpringBootFlywayIntegrationTest.java │ │ ├── SpringBootFlywayPropertiesIntegrationTest.java │ │ ├── SpringBootLiquibaseIntegrationTest.java │ │ ├── SpringBootLiquibasePropertiesIntegrationTest.java │ │ ├── SpringProxiedBeanMethodsIntegrationTest.java │ │ ├── SpringSqlAnnotationIntegrationTest.java │ │ ├── SpringSqlAnnotationRefreshIntegrationTest.java │ │ ├── config │ │ │ ├── ConfigurationPropertiesIntegrationTest.java │ │ │ ├── DatabaseProvidersIntegrationTest.java │ │ │ ├── ProviderTypePriorityIntegrationTest.java │ │ │ └── ProviderTypePropertyIntegrationTest.java │ │ ├── context │ │ │ ├── DefaultDatabaseContextTest.java │ │ │ └── DefaultProviderResolverTest.java │ │ ├── event │ │ │ └── EventPublishingTestExecutionListenerTest.java │ │ ├── flyway │ │ │ ├── FlywayDatabaseExtensionTest.java │ │ │ └── FlywayDescriptorTest.java │ │ ├── liquibase │ │ │ ├── LiquibaseDatabaseExtensionTest.java │ │ │ └── LiquibaseDatabasePreparerTest.java │ │ ├── preparer │ │ │ └── RecordingDataSourceTest.java │ │ ├── provider │ │ │ ├── DefaultProviderIntegrationTest.java │ │ │ ├── DerbyProviderIntegrationTest.java │ │ │ ├── DockerMSSQLProviderIntegrationTest.java │ │ │ ├── DockerMSSQLProviderWithConfigurationIntegrationTest.java │ │ │ ├── DockerMariaDBProviderIntegrationTest.java │ │ │ ├── DockerMariaDBProviderWithConfigurationIntegrationTest.java │ │ │ ├── DockerMySQLProviderIntegrationTest.java │ │ │ ├── DockerMySQLProviderWithConfigurationIntegrationTest.java │ │ │ ├── DockerPostgresProviderIntegrationTest.java │ │ │ ├── DockerPostgresProviderWithConfigurationIntegrationTest.java │ │ │ ├── DockerPostgresProviderWithPostgisImageIntegrationTest.java │ │ │ ├── H2ProviderIntegrationTest.java │ │ │ ├── HSQLProviderIntegrationTest.java │ │ │ ├── OpenTableProviderIntegrationTest.java │ │ │ ├── OpenTableProviderWithConfigurationIntegrationTest.java │ │ │ ├── OptimizingDatabaseProviderTest.java │ │ │ ├── PrefetchingDatabaseProviderTest.java │ │ │ ├── TemplatingDatabaseProviderTest.java │ │ │ ├── YandexProviderIntegrationTest.java │ │ │ ├── YandexProviderWithConfigurationIntegrationTest.java │ │ │ ├── ZonkyProviderIntegrationTest.java │ │ │ ├── ZonkyProviderWithConfigurationIntegrationTest.java │ │ │ ├── derby │ │ │ │ └── DerbyDatabaseProviderTest.java │ │ │ ├── h2 │ │ │ │ └── H2DatabaseProviderTest.java │ │ │ ├── hsqldb │ │ │ │ └── HSQLDatabaseProviderTest.java │ │ │ ├── mariadb │ │ │ │ └── DockerMariaDBDatabaseProviderTest.java │ │ │ ├── mssql │ │ │ │ └── DockerMSSQLDatabaseProviderTest.java │ │ │ ├── mysql │ │ │ │ └── DockerMySQLDatabaseProviderTest.java │ │ │ └── postgres │ │ │ │ ├── DockerPostgresDatabaseProviderTest.java │ │ │ │ ├── OpenTablePostgresDatabaseProviderTest.java │ │ │ │ ├── YandexPostgresDatabaseProviderTest.java │ │ │ │ └── ZonkyPostgresDatabaseProviderTest.java │ │ └── support │ │ │ └── TestDatabasePreparer.java │ │ └── support │ │ ├── ConditionalTestRule.java │ │ ├── MockitoAssertions.java │ │ ├── SpyPostProcessor.java │ │ ├── TestAssumptions.java │ │ └── TestSocketUtils.java │ └── resources │ ├── db │ ├── changelog │ │ └── db.changelog-master.yaml │ ├── create_address_table.sql │ ├── migration │ │ ├── R__people_view.sql │ │ ├── V0001_1__create_person_table.sql │ │ ├── V0002_1__rename_surname_column.sql │ │ ├── afterMigrate.sql │ │ └── beforeMigrate.sql │ ├── next_migration │ │ └── V0001_1__create_person_table.sql │ └── test_migration │ │ ├── appendable │ │ └── V1000_1__create_test_data.sql │ │ ├── dependent │ │ ├── V0001_2__add_full_name_column.sql │ │ └── V0999_1__create_test_data.sql │ │ ├── separated │ │ └── V1000_1__create_test_person_table.sql │ │ └── slow │ │ └── V1000_1__sleep.sql │ └── logback-test.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── zonky.jpg /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | milestone: 4 | types: [closed] 5 | jobs: 6 | build: 7 | name: Release ${{ github.event.milestone.title }} 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v3 12 | with: 13 | ref: master 14 | fetch-depth: 0 15 | - name: Set up Git 16 | run: | 17 | git config user.name "Zonky Bot" 18 | git config user.email "bot@zonky.com" 19 | - name: Set up JDK 20 | uses: actions/setup-java@v3 21 | with: 22 | distribution: 'zulu' 23 | java-version: 8 24 | - name: Install language packs 25 | run: | 26 | sudo apt-get update 27 | sudo apt-get install language-pack-cs 28 | - name: Accept third party licence 29 | env: 30 | ACCEPT_LICENCE: ${{ secrets.ACCEPT_LICENCE }} 31 | if: env.ACCEPT_LICENCE == 'true' 32 | run: ./accept-third-party-license.sh 33 | - name: Rename cloned directory 34 | run: | 35 | mv /home/runner/work/embedded-database-spring-test/embedded-database-spring-test /home/runner/work/embedded-database-spring-test/embedded-database \ 36 | && mkdir /home/runner/work/embedded-database-spring-test/embedded-database-spring-test 37 | - name: Release with Gradle 38 | env: 39 | RELEASE_VERSION: ${{ github.event.milestone.title }} 40 | MAVEN_USER: ${{ secrets.MAVEN_USER }} 41 | MAVEN_PASS: ${{ secrets.MAVEN_PASS }} 42 | ORG_GRADLE_PROJECT_signingKeyId: ${{ secrets.SIGNING_KEY_ID }} 43 | ORG_GRADLE_PROJECT_signingKey: ${{ secrets.SIGNING_CERT }} 44 | ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.SIGNING_PASS }} 45 | run: | 46 | cd /home/runner/work/embedded-database-spring-test/embedded-database && \ 47 | ./gradlew release --info --full-stacktrace \ 48 | -Prelease.useAutomaticVersion=true \ 49 | -Prelease.releaseVersion=$RELEASE_VERSION \ 50 | -Possrh.username=$MAVEN_USER \ 51 | -Possrh.password=$MAVEN_PASS -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Checks 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | name: Java ${{ matrix.java }} 6 | runs-on: ubuntu-latest 7 | strategy: 8 | fail-fast: false 9 | matrix: 10 | java: [8, 11, 17] 11 | steps: 12 | - name: Checkout project 13 | uses: actions/checkout@v3 14 | - name: Set up JDK 15 | uses: actions/setup-java@v3 16 | with: 17 | distribution: 'zulu' 18 | java-version: ${{ matrix.java }} 19 | cache: 'gradle' 20 | - name: Install language packs 21 | run: | 22 | sudo apt-get update 23 | sudo apt-get install language-pack-cs 24 | - name: Accept third party licence 25 | env: 26 | ACCEPT_LICENCE: ${{ secrets.ACCEPT_LICENCE }} 27 | if: env.ACCEPT_LICENCE == 'true' 28 | run: ./accept-third-party-license.sh 29 | - name: Build with Gradle 30 | run: ./gradlew build 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Class files # 2 | target/ 3 | *.class 4 | 5 | # Gradle files # 6 | .gradle/ 7 | build/ 8 | 9 | # Package files # 10 | *.jar 11 | *.war 12 | *.ear 13 | 14 | # Eclipse specific files # 15 | .classpath 16 | .project 17 | .settings/ 18 | 19 | # Idea specific files # 20 | .idea/ 21 | *.iml 22 | *.iws 23 | 24 | # Virtual machine crash logs 25 | hs_err_pid* 26 | 27 | # Gradle wrapper files 28 | !gradle/wrapper/gradle-wrapper.jar 29 | 30 | # Prevent license accepting file to get accidentially commited to git 31 | container-license-acceptance.txt -------------------------------------------------------------------------------- /accept-third-party-license.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | { 4 | echo "mcr.microsoft.com/mssql/server:2022-latest" 5 | echo "mcr.microsoft.com/mssql/server:2019-latest" 6 | echo "mcr.microsoft.com/mssql/server:2017-latest" 7 | echo "mcr.microsoft.com/mssql/server:2017-CU20" 8 | } > embedded-database-spring-test/src/test/resources/container-license-acceptance.txt -------------------------------------------------------------------------------- /embedded-database-spring-test-autoconfigure/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # ContextCustomizerFactories for the Spring TestContext Framework 2 | org.springframework.test.context.ContextCustomizerFactory = \ 3 | io.zonky.test.db.EmbeddedDatabaseContextCustomizerFactory 4 | 5 | # TestExecutionListeners for the Spring TestContext Framework 6 | org.springframework.test.context.TestExecutionListener = \ 7 | io.zonky.test.db.EmbeddedDatabaseTestExecutionListener,\ 8 | io.zonky.test.db.event.EventPublishingTestExecutionListener,\ 9 | io.zonky.test.db.flyway.OptimizedFlywayTestExecutionListener -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/AutoConfigureEmbeddedDatabases.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Inherited; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Container annotation that aggregates several {@link AutoConfigureEmbeddedDatabase} annotations. 28 | *

29 | * Can be used natively, declaring several nested {@link AutoConfigureEmbeddedDatabase} annotations. 30 | * Can also be used in conjunction with Java 8's support for repeatable annotations, 31 | * where {@link AutoConfigureEmbeddedDatabase} can simply be declared several times on the same 32 | * {@linkplain ElementType#TYPE type}, implicitly generating this container annotation. 33 | *

34 | * This annotation may be used as a meta-annotation to create custom composed annotations. 35 | * 36 | * @see AutoConfigureEmbeddedDatabase 37 | */ 38 | @Documented 39 | @Inherited 40 | @Target(ElementType.TYPE) 41 | @Retention(RetentionPolicy.RUNTIME) 42 | public @interface AutoConfigureEmbeddedDatabases { 43 | 44 | AutoConfigureEmbeddedDatabase[] value(); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/ConditionalOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.context.annotation.Conditional; 20 | 21 | import java.lang.annotation.Documented; 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Target({ ElementType.TYPE, ElementType.METHOD }) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Documented 30 | @Conditional(OnClassCondition.class) 31 | @interface ConditionalOnClass { 32 | 33 | String[] name() default {}; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/ConditionalOnMissingBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.context.annotation.Conditional; 20 | 21 | import java.lang.annotation.Documented; 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Target({ ElementType.TYPE, ElementType.METHOD }) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Documented 30 | @Conditional(OnBeanCondition.class) 31 | @interface ConditionalOnMissingBean { 32 | 33 | String[] name() default {}; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/EmbeddedDatabaseCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 20 | import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; 21 | 22 | public class EmbeddedDatabaseCondition extends NoneNestedConditions { 23 | 24 | EmbeddedDatabaseCondition() { 25 | super(ConfigurationPhase.PARSE_CONFIGURATION); 26 | } 27 | 28 | @ConditionalOnProperty(prefix = "zonky.test.database", name = "replace", havingValue = "none") 29 | static class EmbeddedDatabaseDisabled { 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/MissingDatabaseProviderException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import io.zonky.test.db.support.ProviderDescriptor; 20 | 21 | import java.util.List; 22 | 23 | public class MissingDatabaseProviderException extends RuntimeException { 24 | 25 | public MissingDatabaseProviderException(ProviderDescriptor descriptor, List availableProviders) { 26 | super(String.format("Missing database provider for: %s, available providers: %s", descriptor, availableProviders)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/MissingProviderDependencyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | public class MissingProviderDependencyException extends RuntimeException { 20 | 21 | public MissingProviderDependencyException(String message) { 22 | super(message); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/OnBeanCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 20 | import org.springframework.context.annotation.ConditionContext; 21 | import org.springframework.context.annotation.ConfigurationCondition; 22 | import org.springframework.core.Ordered; 23 | import org.springframework.core.annotation.Order; 24 | import org.springframework.core.type.AnnotatedTypeMetadata; 25 | import org.springframework.util.MultiValueMap; 26 | 27 | import java.util.Arrays; 28 | import java.util.List; 29 | import java.util.stream.Collectors; 30 | 31 | @Order(Ordered.LOWEST_PRECEDENCE) 32 | class OnBeanCondition implements ConfigurationCondition { 33 | 34 | @Override 35 | public ConfigurationPhase getConfigurationPhase() { 36 | return ConfigurationPhase.REGISTER_BEAN; 37 | } 38 | 39 | @Override 40 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 41 | ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); 42 | List candidates = getCandidates(metadata, ConditionalOnMissingBean.class); 43 | 44 | for (String beanName : candidates) { 45 | if (beanFactory.containsBean(beanName)) { 46 | return false; 47 | } 48 | } 49 | 50 | return true; 51 | } 52 | 53 | private List getCandidates(AnnotatedTypeMetadata metadata, Class annotationType) { 54 | MultiValueMap attributes = metadata.getAllAnnotationAttributes(annotationType.getName(), true); 55 | if (attributes == null || attributes.get("name") == null) { 56 | throw new IllegalStateException("@ConditionalOnMissingBean did not specify a bean name"); 57 | } 58 | return attributes.get("name").stream().flatMap(o -> Arrays.stream((String[]) o)).collect(Collectors.toList()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/OnClassCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.context.annotation.Condition; 20 | import org.springframework.context.annotation.ConditionContext; 21 | import org.springframework.core.Ordered; 22 | import org.springframework.core.annotation.Order; 23 | import org.springframework.core.type.AnnotatedTypeMetadata; 24 | import org.springframework.util.ClassUtils; 25 | import org.springframework.util.MultiValueMap; 26 | 27 | import java.util.Arrays; 28 | import java.util.List; 29 | import java.util.stream.Collectors; 30 | 31 | @Order(Ordered.HIGHEST_PRECEDENCE) 32 | class OnClassCondition implements Condition { 33 | 34 | @Override 35 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 36 | ClassLoader classLoader = context.getClassLoader(); 37 | List candidates = getCandidates(metadata, ConditionalOnClass.class); 38 | 39 | for (String className : candidates) { 40 | if (!ClassUtils.isPresent(className, classLoader)) { 41 | return false; 42 | } 43 | } 44 | 45 | return true; 46 | } 47 | 48 | private List getCandidates(AnnotatedTypeMetadata metadata, Class annotationType) { 49 | MultiValueMap attributes = metadata.getAllAnnotationAttributes(annotationType.getName(), true); 50 | if (attributes == null || attributes.get("name") == null) { 51 | throw new IllegalStateException("@ConditionalOnClass did not specify a class name"); 52 | } 53 | return attributes.get("name").stream().flatMap(o -> Arrays.stream((String[]) o)).collect(Collectors.toList()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/config/Provider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import org.springframework.beans.factory.annotation.Qualifier; 20 | import org.springframework.context.annotation.Lazy; 21 | 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.ElementType; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.RetentionPolicy; 26 | import java.lang.annotation.Target; 27 | 28 | @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE }) 29 | @Retention(RetentionPolicy.RUNTIME) 30 | @Documented 31 | @Lazy 32 | @Qualifier 33 | public @interface Provider { 34 | 35 | String type(); 36 | 37 | String database(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DatabaseContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.context; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | import io.zonky.test.db.provider.EmbeddedDatabase; 21 | 22 | import java.util.List; 23 | 24 | public interface DatabaseContext { 25 | 26 | List getCorePreparers(); 27 | 28 | List getTestPreparers(); 29 | 30 | EmbeddedDatabase getDatabase(); 31 | 32 | ContextState getState(); 33 | 34 | void apply(DatabasePreparer preparer); 35 | 36 | void reset(); 37 | 38 | enum ContextState { 39 | 40 | INITIALIZING, 41 | FRESH, 42 | AHEAD, 43 | DIRTY 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DatabaseTargetSource.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.context; 2 | 3 | import io.zonky.test.db.context.DatabaseContext; 4 | import org.springframework.aop.TargetSource; 5 | 6 | import javax.sql.DataSource; 7 | 8 | public class DatabaseTargetSource implements TargetSource { 9 | 10 | private final DatabaseContext context; 11 | 12 | public DatabaseTargetSource(DatabaseContext context) { 13 | this.context = context; 14 | } 15 | 16 | public DatabaseContext getContext() { 17 | return context; 18 | } 19 | 20 | @Override 21 | public Class getTargetClass() { 22 | return DataSource.class; 23 | } 24 | 25 | @Override 26 | public boolean isStatic() { 27 | return false; 28 | } 29 | 30 | @Override 31 | public Object getTarget() { 32 | return context.getDatabase(); 33 | } 34 | 35 | @Override 36 | public void releaseTarget(Object target) { 37 | // nothing to do 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/context/EmbeddedDatabaseFactoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.context; 18 | 19 | import org.springframework.aop.framework.ProxyFactory; 20 | import org.springframework.beans.factory.FactoryBean; 21 | import org.springframework.beans.factory.ObjectFactory; 22 | import org.springframework.core.Ordered; 23 | 24 | import javax.sql.DataSource; 25 | 26 | /** 27 | * Implementation of the {@link org.springframework.beans.factory.FactoryBean} interface 28 | * that provides fully cacheable instances of the embedded postgres database. 29 | */ 30 | public class EmbeddedDatabaseFactoryBean implements FactoryBean, Ordered { 31 | 32 | private final ObjectFactory databaseContextProvider; 33 | 34 | private DataSource dataSource; 35 | 36 | public EmbeddedDatabaseFactoryBean(ObjectFactory databaseContextProvider) { 37 | this.databaseContextProvider = databaseContextProvider; 38 | } 39 | 40 | @Override 41 | public int getOrder() { 42 | return Ordered.HIGHEST_PRECEDENCE; 43 | } 44 | 45 | @Override 46 | public boolean isSingleton() { 47 | return true; 48 | } 49 | 50 | @Override 51 | public Class getObjectType() { 52 | return DataSource.class; 53 | } 54 | 55 | @Override 56 | public synchronized DataSource getObject() { 57 | if (dataSource == null) { 58 | DatabaseContext databaseContext = databaseContextProvider.getObject(); 59 | dataSource = ProxyFactory.getProxy(DataSource.class, new DatabaseTargetSource(databaseContext)); 60 | } 61 | return dataSource; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/event/EventPublishingTestExecutionListener.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.event; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.test.context.TestContext; 7 | import org.springframework.test.context.TestExecutionListener; 8 | import org.springframework.test.context.support.AbstractTestExecutionListener; 9 | import org.springframework.util.ClassUtils; 10 | 11 | public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener { 12 | 13 | private static final Logger logger = LoggerFactory.getLogger(EventPublishingTestExecutionListener.class); 14 | 15 | private static final boolean TEST_EXECUTION_METHODS_SUPPORTED = ClassUtils.getMethodIfAvailable( 16 | TestExecutionListener.class, "beforeTestExecution", null) != null; 17 | 18 | @Override 19 | public void beforeTestMethod(TestContext testContext) { 20 | if (!TEST_EXECUTION_METHODS_SUPPORTED) { 21 | beforeTestExecution(testContext); 22 | } 23 | } 24 | 25 | @Override 26 | public void afterTestMethod(TestContext testContext) { 27 | if (!TEST_EXECUTION_METHODS_SUPPORTED) { 28 | afterTestExecution(testContext); 29 | } 30 | } 31 | 32 | @Override 33 | public void beforeTestExecution(TestContext testContext) { 34 | ApplicationContext applicationContext = testContext.getApplicationContext(); 35 | applicationContext.publishEvent(new TestExecutionStartedEvent(applicationContext, testContext.getTestMethod())); 36 | logger.trace("Test execution started - '{}#{}'", testContext.getTestClass().getSimpleName(), testContext.getTestMethod().getName()); 37 | } 38 | 39 | @Override 40 | public void afterTestExecution(TestContext testContext) { 41 | ApplicationContext applicationContext = testContext.getApplicationContext(); 42 | applicationContext.publishEvent(new TestExecutionFinishedEvent(applicationContext, testContext.getTestMethod())); 43 | logger.trace("Test execution finished - '{}#{}'", testContext.getTestClass().getSimpleName(), testContext.getTestMethod().getName()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/event/TestExecutionFinishedEvent.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.event; 2 | 3 | import org.springframework.context.ApplicationEvent; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | public class TestExecutionFinishedEvent extends ApplicationEvent { 8 | 9 | private final Method testMethod; 10 | 11 | public TestExecutionFinishedEvent(Object source, Method testMethod) { 12 | super(source); 13 | this.testMethod = testMethod; 14 | } 15 | 16 | public Method getTestMethod() { 17 | return testMethod; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/event/TestExecutionStartedEvent.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.event; 2 | 3 | import org.springframework.context.ApplicationEvent; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | public class TestExecutionStartedEvent extends ApplicationEvent { 8 | 9 | private final Method testMethod; 10 | 11 | public TestExecutionStartedEvent(Object source, Method testMethod) { 12 | super(source); 13 | this.testMethod = testMethod; 14 | } 15 | 16 | public Method getTestMethod() { 17 | return testMethod; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayClassUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.flyway; 18 | 19 | import org.flywaydb.core.Flyway; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.core.io.ClassPathResource; 22 | import org.springframework.util.ClassUtils; 23 | import org.springframework.util.StreamUtils; 24 | 25 | import static java.nio.charset.StandardCharsets.UTF_8; 26 | 27 | public class FlywayClassUtils { 28 | 29 | private static final FlywayVersion flywayVersion = loadFlywayVersion(); 30 | 31 | private FlywayClassUtils() {} 32 | 33 | private static FlywayVersion loadFlywayVersion() { 34 | try { 35 | ClassPathResource versionResource = new ClassPathResource("org/flywaydb/core/internal/version.txt", FlywayClassUtils.class.getClassLoader()); 36 | if (versionResource.exists()) { 37 | String version = StreamUtils.copyToString(versionResource.getInputStream(), UTF_8); 38 | return FlywayVersion.parseVersion(version); 39 | } else if (ClassUtils.hasMethod(Flyway.class, "isPlaceholderReplacement")) { 40 | return FlywayVersion.parseVersion("3.2"); 41 | } else if (ClassUtils.hasMethod(Flyway.class, "getBaselineVersion")) { 42 | return FlywayVersion.parseVersion("3.1"); 43 | } else { 44 | return FlywayVersion.parseVersion("3.0"); 45 | } 46 | } catch (Exception e) { 47 | LoggerFactory.getLogger(FlywayClassUtils.class).error("Unexpected error when resolving flyway version", e); 48 | return FlywayVersion.parseVersion("0"); 49 | } 50 | } 51 | 52 | public static FlywayVersion getFlywayVersion() { 53 | return flywayVersion; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayPropertiesPostProcessor.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.flyway; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | import org.springframework.boot.autoconfigure.flyway.FlywayProperties; 6 | import org.springframework.core.Ordered; 7 | 8 | public class FlywayPropertiesPostProcessor implements BeanPostProcessor, Ordered { 9 | 10 | @Override 11 | public int getOrder() { 12 | return Ordered.LOWEST_PRECEDENCE - 1; 13 | } 14 | 15 | @Override 16 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 17 | return bean; 18 | } 19 | 20 | @Override 21 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 22 | if (bean instanceof FlywayProperties) { 23 | FlywayProperties properties = (FlywayProperties) bean; 24 | properties.setUrl(null); 25 | properties.setUser(null); 26 | properties.setPassword(null); 27 | } 28 | return bean; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.flyway; 18 | 19 | import java.util.Arrays; 20 | import java.util.stream.Collectors; 21 | 22 | public class FlywayVersion implements Comparable { 23 | 24 | private final int[] parts; 25 | 26 | public static FlywayVersion parseVersion(String version) { 27 | String[] split = version.split("\\."); 28 | int[] parts = Arrays.stream(split).mapToInt(Integer::parseInt).toArray(); 29 | return new FlywayVersion(parts); 30 | } 31 | 32 | private FlywayVersion(int[] parts) { 33 | this.parts = parts; 34 | } 35 | 36 | public boolean isGreaterThan(String version) { 37 | return compareTo(parseVersion(version)) > 0; 38 | } 39 | 40 | public boolean isGreaterThanOrEqualTo(String version) { 41 | return compareTo(parseVersion(version)) >= 0; 42 | } 43 | 44 | public boolean isLessThan(String version) { 45 | return compareTo(parseVersion(version)) < 0; 46 | } 47 | 48 | public boolean isLessThanOrEqualTo(String version) { 49 | return compareTo(parseVersion(version)) <= 0; 50 | } 51 | 52 | @Override 53 | public int compareTo(FlywayVersion other) { 54 | for (int i = 0; i < this.parts.length || i < other.parts.length; i++) { 55 | int thisPart = i < this.parts.length ? this.parts[i] : 0; 56 | int otherPart = i < other.parts.length ? other.parts[i] : 0; 57 | 58 | if (thisPart > otherPart) { 59 | return 1; 60 | } else if (thisPart < otherPart) { 61 | return -1; 62 | } 63 | } 64 | return 0; 65 | } 66 | 67 | @Override 68 | public boolean equals(Object o) { 69 | if (this == o) return true; 70 | if (o == null || getClass() != o.getClass()) return false; 71 | FlywayVersion that = (FlywayVersion) o; 72 | return Arrays.equals(parts, that.parts); 73 | } 74 | 75 | @Override 76 | public int hashCode() { 77 | return Arrays.hashCode(parts); 78 | } 79 | 80 | @Override 81 | public String toString() { 82 | return Arrays.stream(parts) 83 | .mapToObj(String::valueOf) 84 | .collect(Collectors.joining(".")); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/preparer/BaselineFlywayDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.flyway.preparer; 18 | 19 | import io.zonky.test.db.flyway.FlywayDescriptor; 20 | import io.zonky.test.db.flyway.FlywayWrapper; 21 | 22 | public class BaselineFlywayDatabasePreparer extends FlywayDatabasePreparer { 23 | 24 | public BaselineFlywayDatabasePreparer(FlywayDescriptor descriptor) { 25 | super(descriptor); 26 | } 27 | 28 | @Override 29 | public long estimatedDuration() { 30 | return 100; 31 | } 32 | 33 | @Override 34 | protected Object doOperation(FlywayWrapper wrapper) { 35 | return wrapper.baseline(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/preparer/CleanFlywayDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.flyway.preparer; 18 | 19 | import io.zonky.test.db.flyway.FlywayDescriptor; 20 | import io.zonky.test.db.flyway.FlywayWrapper; 21 | 22 | public class CleanFlywayDatabasePreparer extends FlywayDatabasePreparer { 23 | 24 | public CleanFlywayDatabasePreparer(FlywayDescriptor descriptor) { 25 | super(descriptor); 26 | } 27 | 28 | @Override 29 | public long estimatedDuration() { 30 | return 100; 31 | } 32 | 33 | @Override 34 | protected Object doOperation(FlywayWrapper wrapper) { 35 | wrapper.setCleanDisabled(false); 36 | return wrapper.clean(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/preparer/FlywayDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.flyway.preparer; 18 | 19 | import com.google.common.base.MoreObjects; 20 | import com.google.common.base.Stopwatch; 21 | import io.zonky.test.db.flyway.FlywayDescriptor; 22 | import io.zonky.test.db.flyway.FlywayWrapper; 23 | import io.zonky.test.db.preparer.DatabasePreparer; 24 | import org.slf4j.Logger; 25 | import org.slf4j.LoggerFactory; 26 | import org.springframework.util.concurrent.ListenableFuture; 27 | import org.springframework.util.concurrent.SettableListenableFuture; 28 | 29 | import javax.sql.DataSource; 30 | import java.util.Objects; 31 | 32 | public abstract class FlywayDatabasePreparer implements DatabasePreparer { 33 | 34 | protected final Logger logger = LoggerFactory.getLogger(getClass()); 35 | 36 | protected final SettableListenableFuture result = new SettableListenableFuture<>(); 37 | protected final FlywayDescriptor descriptor; 38 | 39 | public FlywayDatabasePreparer(FlywayDescriptor descriptor) { 40 | this.descriptor = descriptor; 41 | } 42 | 43 | public FlywayDescriptor getDescriptor() { 44 | return descriptor; 45 | } 46 | 47 | public ListenableFuture getResult() { 48 | return result; 49 | } 50 | 51 | protected abstract Object doOperation(FlywayWrapper wrapper); 52 | 53 | @Override 54 | public void prepare(DataSource dataSource) { 55 | Stopwatch stopwatch = Stopwatch.createStarted(); 56 | 57 | FlywayWrapper wrapper = FlywayWrapper.newInstance(); 58 | descriptor.applyTo(wrapper); 59 | wrapper.setDataSource(dataSource); 60 | 61 | try { 62 | result.set(doOperation(wrapper)); 63 | logger.trace("Database has been successfully prepared in {}", stopwatch); 64 | } catch (RuntimeException e) { 65 | result.setException(e); 66 | throw e; 67 | } 68 | } 69 | 70 | @Override 71 | public boolean equals(Object o) { 72 | if (this == o) return true; 73 | if (o == null || getClass() != o.getClass()) return false; 74 | FlywayDatabasePreparer that = (FlywayDatabasePreparer) o; 75 | return Objects.equals(descriptor, that.descriptor); 76 | } 77 | 78 | @Override 79 | public int hashCode() { 80 | return Objects.hash(descriptor); 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | return MoreObjects.toStringHelper(this) 86 | .add("schemas", descriptor.getSchemas()) 87 | .add("estimatedDuration", estimatedDuration()) 88 | .toString(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/liquibase/LiquibasePropertiesPostProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.liquibase; 18 | 19 | import org.springframework.beans.BeansException; 20 | import org.springframework.beans.factory.config.BeanPostProcessor; 21 | import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties; 22 | import org.springframework.core.Ordered; 23 | 24 | public class LiquibasePropertiesPostProcessor implements BeanPostProcessor, Ordered { 25 | 26 | @Override 27 | public int getOrder() { 28 | return Ordered.LOWEST_PRECEDENCE - 1; 29 | } 30 | 31 | @Override 32 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 33 | return bean; 34 | } 35 | 36 | @Override 37 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 38 | if (bean instanceof LiquibaseProperties) { 39 | LiquibaseProperties properties = (LiquibaseProperties) bean; 40 | properties.setUrl(null); 41 | properties.setUser(null); 42 | properties.setPassword(null); 43 | } 44 | return bean; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/logging/EmbeddedDatabaseReporter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.logging; 18 | 19 | import io.zonky.test.db.provider.EmbeddedDatabase; 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | 23 | import java.lang.reflect.AnnotatedElement; 24 | import java.lang.reflect.Method; 25 | 26 | public class EmbeddedDatabaseReporter { 27 | 28 | private static final Logger logger = LoggerFactory.getLogger(EmbeddedDatabaseReporter.class); 29 | 30 | public static void reportDataSource(String beanName, EmbeddedDatabase database, AnnotatedElement element) { 31 | logger.info("JDBC URL to connect to '{}': url='{}', scope='{}'", beanName, database.getJdbcUrl(), getElementName(element)); 32 | } 33 | 34 | private static String getElementName(AnnotatedElement element) { 35 | if (element instanceof Class) { 36 | return ((Class) element).getSimpleName(); 37 | } else if (element instanceof Method) { 38 | Method method = (Method) element; 39 | return getElementName(method.getDeclaringClass()) + "#" + method.getName(); 40 | } else { 41 | return element.toString(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/preparer/CompositeDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.preparer; 18 | 19 | import com.google.common.base.MoreObjects; 20 | import com.google.common.collect.ImmutableList; 21 | 22 | import javax.sql.DataSource; 23 | import java.sql.SQLException; 24 | import java.util.List; 25 | import java.util.Objects; 26 | 27 | public class CompositeDatabasePreparer implements DatabasePreparer { 28 | 29 | private final List preparers; 30 | 31 | public CompositeDatabasePreparer(List preparers) { 32 | this.preparers = ImmutableList.copyOf(preparers); 33 | } 34 | 35 | public List getPreparers() { 36 | return preparers; 37 | } 38 | 39 | @Override 40 | public long estimatedDuration() { 41 | return preparers.stream() 42 | .mapToLong(DatabasePreparer::estimatedDuration) 43 | .sum(); 44 | } 45 | 46 | @Override 47 | public void prepare(DataSource dataSource) throws SQLException { 48 | for (DatabasePreparer preparer : preparers) { 49 | preparer.prepare(dataSource); 50 | } 51 | } 52 | 53 | @Override 54 | public boolean equals(Object o) { 55 | if (this == o) return true; 56 | if (o == null || getClass() != o.getClass()) return false; 57 | CompositeDatabasePreparer that = (CompositeDatabasePreparer) o; 58 | return Objects.equals(preparers, that.preparers); 59 | } 60 | 61 | @Override 62 | public int hashCode() { 63 | return Objects.hash(preparers); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return MoreObjects.toStringHelper(this) 69 | .add("preparers", preparers) 70 | .toString(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/preparer/DatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.preparer; 18 | 19 | import javax.sql.DataSource; 20 | import java.sql.SQLException; 21 | 22 | public interface DatabasePreparer { 23 | 24 | long estimatedDuration(); 25 | 26 | void prepare(DataSource dataSource) throws SQLException; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/preparer/RecordingDataSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.preparer; 18 | 19 | import org.springframework.aop.framework.ProxyFactory; 20 | 21 | import javax.sql.DataSource; 22 | import java.lang.reflect.Modifier; 23 | 24 | public interface RecordingDataSource extends DataSource { 25 | 26 | ReplayableDatabasePreparer getPreparer(); 27 | 28 | static RecordingDataSource wrap(DataSource dataSource) { 29 | ProxyFactory proxyFactory = new ProxyFactory(dataSource); 30 | proxyFactory.addAdvice(new RecordingMethodInterceptor()); 31 | proxyFactory.addInterface(RecordingDataSource.class); 32 | 33 | if (!Modifier.isFinal(dataSource.getClass().getModifiers())) { 34 | proxyFactory.setProxyTargetClass(true); 35 | } 36 | 37 | return (RecordingDataSource) proxyFactory.getProxy(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/preparer/ReplayableDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.preparer; 18 | 19 | public interface ReplayableDatabasePreparer extends DatabasePreparer { 20 | 21 | boolean hasRecords(); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/DatabaseProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | 21 | public interface DatabaseProvider { 22 | 23 | EmbeddedDatabase createDatabase(DatabasePreparer preparer) throws ProviderException; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/DatabaseRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | 21 | public class DatabaseRequest { 22 | 23 | private final DatabasePreparer preparer; 24 | private final DatabaseTemplate template; 25 | 26 | public static DatabaseRequest of(DatabasePreparer preparer) { 27 | return new DatabaseRequest(preparer, null); 28 | } 29 | 30 | public static DatabaseRequest of(DatabasePreparer preparer, DatabaseTemplate template) { 31 | return new DatabaseRequest(preparer, template); 32 | } 33 | 34 | private DatabaseRequest(DatabasePreparer preparer, DatabaseTemplate template) { 35 | this.template = template; 36 | this.preparer = preparer; 37 | } 38 | 39 | public DatabasePreparer getPreparer() { 40 | return preparer; 41 | } 42 | 43 | public DatabaseTemplate getTemplate() { 44 | return template; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/DatabaseTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import java.io.Closeable; 20 | 21 | public interface DatabaseTemplate extends Closeable { 22 | 23 | String getTemplateName(); 24 | 25 | void close(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import javax.sql.DataSource; 20 | import java.io.Closeable; 21 | 22 | public interface EmbeddedDatabase extends DataSource, Closeable { 23 | 24 | String getJdbcUrl(); 25 | 26 | void close(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/ProviderException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import org.springframework.core.NestedRuntimeException; 20 | 21 | public class ProviderException extends NestedRuntimeException { 22 | 23 | /** 24 | * Construct a new provider exception. 25 | * 26 | * @param message the exception message 27 | */ 28 | public ProviderException(String message) { 29 | super(message); 30 | } 31 | 32 | /** 33 | * Construct a new provider exception. 34 | * 35 | * @param message the exception message 36 | * @param cause the cause 37 | */ 38 | public ProviderException(String message, Throwable cause) { 39 | super(message, cause); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/TemplatableDatabaseProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | 21 | public interface TemplatableDatabaseProvider extends DatabaseProvider { 22 | 23 | DatabaseTemplate createTemplate(DatabaseRequest request) throws ProviderException; 24 | 25 | EmbeddedDatabase createDatabase(DatabaseRequest request) throws ProviderException; 26 | 27 | default EmbeddedDatabase createDatabase(DatabasePreparer preparer) throws ProviderException { 28 | return createDatabase(DatabaseRequest.of(preparer)); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/derby/DerbyDatabaseProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.derby; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | import io.zonky.test.db.provider.DatabaseProvider; 21 | import io.zonky.test.db.provider.EmbeddedDatabase; 22 | import io.zonky.test.db.provider.ProviderException; 23 | import org.apache.derby.jdbc.EmbeddedDriver ; 24 | import org.springframework.jdbc.datasource.SimpleDriverDataSource; 25 | 26 | import java.sql.SQLException; 27 | import java.util.Objects; 28 | import java.util.Properties; 29 | import java.util.UUID; 30 | import java.util.concurrent.CompletableFuture; 31 | 32 | public class DerbyDatabaseProvider implements DatabaseProvider { 33 | 34 | private static final String URL_TEMPLATE = "jdbc:derby:memory:%s;%s"; 35 | 36 | @Override 37 | public EmbeddedDatabase createDatabase(DatabasePreparer preparer) throws ProviderException { 38 | SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); 39 | String databaseName = UUID.randomUUID().toString(); 40 | 41 | dataSource.setDriverClass(EmbeddedDriver.class); 42 | dataSource.setUrl(String.format(URL_TEMPLATE, databaseName, "create=true")); 43 | dataSource.setUsername("sa"); 44 | dataSource.setPassword(""); 45 | 46 | DerbyEmbeddedDatabase database = new DerbyEmbeddedDatabase(dataSource, databaseName, 47 | () -> shutdownDatabase(databaseName)); 48 | try { 49 | if (preparer != null) { 50 | preparer.prepare(database); 51 | } 52 | } catch (SQLException e) { 53 | throw new ProviderException("Unexpected error when creating a database", e); 54 | } 55 | return database; 56 | } 57 | 58 | @Override 59 | public boolean equals(Object o) { 60 | if (this == o) return true; 61 | return o != null && getClass() == o.getClass(); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(DerbyDatabaseProvider.class); 67 | } 68 | 69 | private static void shutdownDatabase(String dbName) { 70 | CompletableFuture.runAsync(() -> { 71 | try { 72 | new EmbeddedDriver().connect(String.format(URL_TEMPLATE, dbName, "drop=true"), new Properties()); 73 | } catch (SQLException e) { 74 | // nothing to do 75 | } 76 | }); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/derby/DerbyEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.derby; 18 | 19 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 20 | 21 | import javax.sql.DataSource; 22 | 23 | public class DerbyEmbeddedDatabase extends AbstractEmbeddedDatabase { 24 | 25 | private final DataSource dataSource; 26 | private final String dbName; 27 | 28 | public DerbyEmbeddedDatabase(DataSource dataSource, String dbName, Runnable closeCallback) { 29 | super(closeCallback); 30 | this.dataSource = dataSource; 31 | this.dbName = dbName; 32 | } 33 | 34 | @Override 35 | protected DataSource getDataSource() { 36 | return dataSource; 37 | } 38 | 39 | @Override 40 | public String getJdbcUrl() { 41 | return String.format("jdbc:derby:memory:%s;user=sa", dbName); 42 | } 43 | 44 | public String getDatabaseName() { 45 | return dbName; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/h2/H2EmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.h2; 18 | 19 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 20 | import org.h2.tools.Server; 21 | 22 | import javax.sql.DataSource; 23 | 24 | public class H2EmbeddedDatabase extends AbstractEmbeddedDatabase { 25 | 26 | private final Server server; 27 | private final DataSource dataSource; 28 | private final String dbName; 29 | 30 | public H2EmbeddedDatabase(Server server, DataSource dataSource, String dbName, Runnable closeCallback) { 31 | super(closeCallback); 32 | this.server = server; 33 | this.dataSource = dataSource; 34 | this.dbName = dbName; 35 | } 36 | 37 | @Override 38 | protected DataSource getDataSource() { 39 | return dataSource; 40 | } 41 | 42 | @Override 43 | public String getJdbcUrl() { 44 | if (server != null) { 45 | return String.format("jdbc:h2:tcp://localhost:%s/mem:%s;USER=sa", server.getPort(), dbName); 46 | } else { 47 | return String.format("jdbc:h2:mem:%s;USER=sa", dbName); 48 | } 49 | } 50 | 51 | public String getDatabaseName() { 52 | return dbName; 53 | } 54 | 55 | public int getPortNumber() { 56 | if (server != null) { 57 | return server.getPort(); 58 | } else { 59 | return 0; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/hsqldb/HSQLDatabaseProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.hsqldb; 18 | 19 | import io.zonky.test.db.preparer.DatabasePreparer; 20 | import io.zonky.test.db.provider.DatabaseProvider; 21 | import io.zonky.test.db.provider.EmbeddedDatabase; 22 | import io.zonky.test.db.provider.ProviderException; 23 | import org.springframework.jdbc.datasource.SimpleDriverDataSource; 24 | 25 | import javax.sql.DataSource; 26 | import java.sql.Connection; 27 | import java.sql.SQLException; 28 | import java.sql.Statement; 29 | import java.util.Objects; 30 | import java.util.UUID; 31 | import java.util.concurrent.CompletableFuture; 32 | 33 | public class HSQLDatabaseProvider implements DatabaseProvider { 34 | 35 | @Override 36 | public EmbeddedDatabase createDatabase(DatabasePreparer preparer) throws ProviderException { 37 | SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); 38 | String databaseName = UUID.randomUUID().toString(); 39 | 40 | dataSource.setDriverClass(org.hsqldb.jdbcDriver.class); 41 | dataSource.setUrl(String.format("jdbc:hsqldb:mem:%s", databaseName)); 42 | dataSource.setUsername("sa"); 43 | dataSource.setPassword(""); 44 | 45 | HSQLEmbeddedDatabase database = new HSQLEmbeddedDatabase(dataSource, databaseName, 46 | () -> shutdownDatabase(dataSource, databaseName)); 47 | try { 48 | if (preparer != null) { 49 | preparer.prepare(database); 50 | } 51 | } catch (SQLException e) { 52 | throw new ProviderException("Unexpected error when creating a database", e); 53 | } 54 | return database; 55 | } 56 | 57 | @Override 58 | public boolean equals(Object o) { 59 | if (this == o) return true; 60 | return o != null && getClass() == o.getClass(); 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | return Objects.hash(HSQLDatabaseProvider.class); 66 | } 67 | 68 | private static void shutdownDatabase(DataSource dataSource, String dbName) { 69 | CompletableFuture.runAsync(() -> { 70 | try { 71 | executeStatement(dataSource, "SHUTDOWN"); 72 | } catch (SQLException e) { 73 | // nothing to do 74 | } 75 | }); 76 | } 77 | 78 | private static void executeStatement(DataSource dataSource, String ddlStatement) throws SQLException { 79 | try (Connection connection = dataSource.getConnection(); Statement stmt = connection.createStatement()) { 80 | stmt.execute(ddlStatement); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/hsqldb/HSQLEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.hsqldb; 18 | 19 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 20 | 21 | import javax.sql.DataSource; 22 | 23 | public class HSQLEmbeddedDatabase extends AbstractEmbeddedDatabase { 24 | 25 | private final DataSource dataSource; 26 | private final String dbName; 27 | 28 | public HSQLEmbeddedDatabase(DataSource dataSource, String dbName, Runnable closeCallback) { 29 | super(closeCallback); 30 | this.dataSource = dataSource; 31 | this.dbName = dbName; 32 | } 33 | 34 | @Override 35 | protected DataSource getDataSource() { 36 | return dataSource; 37 | } 38 | 39 | @Override 40 | public String getJdbcUrl() { 41 | return String.format("jdbc:hsqldb:mem:%s;user=sa", dbName); 42 | } 43 | 44 | public String getDatabaseName() { 45 | return dbName; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mariadb/MariaDBContainerCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.mariadb; 18 | 19 | import org.testcontainers.containers.MariaDBContainer; 20 | 21 | /** 22 | * Callback interface that can be implemented by beans wishing to customize 23 | * the mariadb container before it is used by a {@code DockerMariaDBDatabaseProvider}. 24 | */ 25 | @FunctionalInterface 26 | public interface MariaDBContainerCustomizer { 27 | 28 | /** 29 | * Customize the given {@link MariaDBContainer}. 30 | * @param container the container to customize 31 | */ 32 | void customize(MariaDBContainer container); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mssql/MSSQLServerContainerCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.mssql; 18 | 19 | import org.testcontainers.containers.MSSQLServerContainer; 20 | 21 | /** 22 | * Callback interface that can be implemented by beans wishing to customize 23 | * the mssql server container before it is used by a {@code DockerMSSQLDatabaseProvider}. 24 | */ 25 | @FunctionalInterface 26 | public interface MSSQLServerContainerCustomizer { 27 | 28 | /** 29 | * Customize the given {@link MSSQLServerContainer}. 30 | * @param container the container to customize 31 | */ 32 | void customize(MSSQLServerContainer container); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mssql/MsSQLEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.mssql; 18 | 19 | import com.microsoft.sqlserver.jdbc.SQLServerDataSource; 20 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 21 | import org.springframework.util.StringUtils; 22 | 23 | import javax.sql.DataSource; 24 | 25 | import static io.zonky.test.db.util.ReflectionUtils.invokeMethod; 26 | 27 | public class MsSQLEmbeddedDatabase extends AbstractEmbeddedDatabase { 28 | 29 | private final SQLServerDataSource dataSource; 30 | 31 | public MsSQLEmbeddedDatabase(SQLServerDataSource dataSource, Runnable closeCallback) { 32 | super(closeCallback); 33 | this.dataSource = dataSource; 34 | } 35 | 36 | @Override 37 | protected DataSource getDataSource() { 38 | return dataSource; 39 | } 40 | 41 | @Override 42 | public String getJdbcUrl() { 43 | String url = String.format("jdbc:sqlserver://%s:%s;databaseName=%s;user=%s", 44 | dataSource.getServerName(), dataSource.getPortNumber(), dataSource.getDatabaseName(), dataSource.getUser()); 45 | if (StringUtils.hasText(getPassword())) { 46 | url += String.format(";password=%s", getPassword()); 47 | } 48 | return url; 49 | } 50 | 51 | private String getPassword() { 52 | return invokeMethod(dataSource, "getPassword"); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mysql/MySQLContainerCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.mysql; 18 | 19 | import org.testcontainers.containers.MySQLContainer; 20 | 21 | /** 22 | * Callback interface that can be implemented by beans wishing to customize 23 | * the mysql container before it is used by a {@code DockerMySQLDatabaseProvider}. 24 | */ 25 | @FunctionalInterface 26 | public interface MySQLContainerCustomizer { 27 | 28 | /** 29 | * Customize the given {@link MySQLContainer}. 30 | * @param container the container to customize 31 | */ 32 | void customize(MySQLContainer container); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mysql/MySQLEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.mysql; 18 | 19 | import com.mysql.cj.jdbc.MysqlDataSource; 20 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 21 | import org.springframework.util.StringUtils; 22 | 23 | import javax.sql.DataSource; 24 | 25 | public class MySQLEmbeddedDatabase extends AbstractEmbeddedDatabase { 26 | 27 | private final MysqlDataSource dataSource; 28 | 29 | public MySQLEmbeddedDatabase(MysqlDataSource dataSource, Runnable closeCallback) { 30 | super(closeCallback); 31 | this.dataSource = dataSource; 32 | } 33 | 34 | @Override 35 | protected DataSource getDataSource() { 36 | return dataSource; 37 | } 38 | 39 | @Override 40 | public String getJdbcUrl() { 41 | String url = dataSource.getUrl() + String.format("?user=%s", dataSource.getUser()); 42 | if (StringUtils.hasText(dataSource.getPassword())) { 43 | url += String.format("&password=%s", dataSource.getPassword()); 44 | } 45 | return url; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgreSQLContainerCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.postgres; 18 | 19 | import org.testcontainers.containers.PostgreSQLContainer; 20 | 21 | /** 22 | * Callback interface that can be implemented by beans wishing to customize 23 | * the postgres container before it is used by a {@code DockerPostgresDatabaseProvider}. 24 | */ 25 | @FunctionalInterface 26 | public interface PostgreSQLContainerCustomizer { 27 | 28 | /** 29 | * Customize the given {@link PostgreSQLContainer}. 30 | * @param container the container to customize 31 | */ 32 | void customize(PostgreSQLContainer container); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.postgres; 18 | 19 | import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase; 20 | import org.postgresql.ds.PGSimpleDataSource; 21 | import org.springframework.util.StringUtils; 22 | 23 | import javax.sql.DataSource; 24 | 25 | public class PostgresEmbeddedDatabase extends AbstractEmbeddedDatabase { 26 | 27 | private final PGSimpleDataSource dataSource; 28 | 29 | public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) { 30 | super(closeCallback); 31 | this.dataSource = dataSource; 32 | } 33 | 34 | @Override 35 | protected DataSource getDataSource() { 36 | return dataSource; 37 | } 38 | 39 | @Override 40 | public String getJdbcUrl() { 41 | String url = dataSource.getUrl() + String.format("?user=%s", dataSource.getUser()); 42 | if (StringUtils.hasText(dataSource.getPassword())) { 43 | url += String.format("&password=%s", dataSource.getPassword()); 44 | } 45 | return url; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.provider.support; 2 | 3 | import io.zonky.test.db.provider.EmbeddedDatabase; 4 | 5 | import javax.sql.DataSource; 6 | import java.io.PrintWriter; 7 | import java.sql.Connection; 8 | import java.sql.SQLException; 9 | import java.sql.SQLFeatureNotSupportedException; 10 | import java.util.logging.Logger; 11 | 12 | public abstract class AbstractEmbeddedDatabase implements EmbeddedDatabase { 13 | 14 | private final Runnable closeCallback; 15 | 16 | protected AbstractEmbeddedDatabase(Runnable closeCallback) { 17 | this.closeCallback = closeCallback; 18 | } 19 | 20 | protected abstract DataSource getDataSource(); 21 | 22 | @Override 23 | public Connection getConnection() throws SQLException { 24 | return getDataSource().getConnection(); 25 | } 26 | 27 | @Override 28 | public Connection getConnection(String username, String password) throws SQLException { 29 | return getDataSource().getConnection(username, password); 30 | } 31 | 32 | @Override 33 | public PrintWriter getLogWriter() throws SQLException { 34 | return getDataSource().getLogWriter(); 35 | } 36 | 37 | @Override 38 | public void setLogWriter(PrintWriter out) throws SQLException { 39 | getDataSource().setLogWriter(out); 40 | } 41 | 42 | @Override 43 | public int getLoginTimeout() throws SQLException { 44 | return getDataSource().getLoginTimeout(); 45 | } 46 | 47 | @Override 48 | public void setLoginTimeout(int seconds) throws SQLException { 49 | getDataSource().setLoginTimeout(seconds); 50 | } 51 | 52 | @Override 53 | public T unwrap(Class iface) throws SQLException { 54 | if (iface.isAssignableFrom(getClass())) { 55 | return iface.cast(this); 56 | } 57 | if (iface.isAssignableFrom(getDataSource().getClass())) { 58 | return iface.cast(getDataSource()); 59 | } 60 | return getDataSource().unwrap(iface); 61 | } 62 | 63 | @Override 64 | public boolean isWrapperFor(Class iface) throws SQLException { 65 | if (iface.isAssignableFrom(getClass())) { 66 | return true; 67 | } 68 | if (iface.isAssignableFrom(getDataSource().getClass())) { 69 | return true; 70 | } 71 | return getDataSource().isWrapperFor(iface); 72 | } 73 | 74 | @Override 75 | public Logger getParentLogger() throws SQLFeatureNotSupportedException { 76 | return getDataSource().getParentLogger(); 77 | } 78 | 79 | @Override 80 | public synchronized void close() { 81 | closeCallback.run(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/SimpleDatabaseTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider.support; 18 | 19 | import io.zonky.test.db.provider.DatabaseTemplate; 20 | 21 | public class SimpleDatabaseTemplate implements DatabaseTemplate { 22 | 23 | private final String templateName; 24 | private final Runnable closeCallback; 25 | 26 | public SimpleDatabaseTemplate(String templateName, Runnable closeCallback) { 27 | this.templateName = templateName; 28 | this.closeCallback = closeCallback; 29 | } 30 | 31 | @Override 32 | public String getTemplateName() { 33 | return templateName; 34 | } 35 | 36 | @Override 37 | public synchronized void close() { 38 | closeCallback.run(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/support/DatabaseDefinition.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.support; 2 | 3 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 4 | 5 | import java.util.Objects; 6 | 7 | public class DatabaseDefinition { 8 | 9 | private final String beanName; 10 | private final AutoConfigureEmbeddedDatabase.DatabaseType databaseType; 11 | private final AutoConfigureEmbeddedDatabase.DatabaseProvider providerType; 12 | 13 | public DatabaseDefinition(String beanName, AutoConfigureEmbeddedDatabase.DatabaseType databaseType, AutoConfigureEmbeddedDatabase.DatabaseProvider providerType) { 14 | this.beanName = beanName; 15 | this.databaseType = databaseType; 16 | this.providerType = providerType; 17 | } 18 | 19 | public String getBeanName() { 20 | return beanName; 21 | } 22 | 23 | public AutoConfigureEmbeddedDatabase.DatabaseType getDatabaseType() { 24 | return databaseType; 25 | } 26 | 27 | public AutoConfigureEmbeddedDatabase.DatabaseProvider getProviderType() { 28 | return providerType; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object o) { 33 | if (this == o) return true; 34 | if (o == null || getClass() != o.getClass()) return false; 35 | DatabaseDefinition that = (DatabaseDefinition) o; 36 | return Objects.equals(beanName, that.beanName) && 37 | databaseType == that.databaseType && 38 | providerType == that.providerType; 39 | } 40 | 41 | @Override 42 | public int hashCode() { 43 | return Objects.hash(beanName, databaseType, providerType); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/support/ProviderDescriptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.support; 18 | 19 | import com.google.common.base.MoreObjects; 20 | import org.springframework.util.Assert; 21 | 22 | import java.util.Objects; 23 | 24 | public final class ProviderDescriptor { 25 | 26 | private final String providerName; 27 | private final String databaseName; 28 | 29 | public static ProviderDescriptor of(String providerName, String databaseName) { 30 | return new ProviderDescriptor(providerName, databaseName); 31 | } 32 | 33 | private ProviderDescriptor(String providerName, String databaseName) { 34 | Assert.notNull(databaseName, "Database name must not be null"); 35 | Assert.notNull(providerName, "Provider name must not be null"); 36 | this.databaseName = databaseName.toLowerCase(); 37 | this.providerName = providerName.toLowerCase(); 38 | } 39 | 40 | public String getProviderName() { 41 | return providerName; 42 | } 43 | 44 | public String getDatabaseName() { 45 | return databaseName; 46 | } 47 | 48 | @Override 49 | public boolean equals(Object o) { 50 | if (this == o) return true; 51 | if (o == null || getClass() != o.getClass()) return false; 52 | ProviderDescriptor that = (ProviderDescriptor) o; 53 | return Objects.equals(providerName, that.providerName) && 54 | Objects.equals(databaseName, that.databaseName); 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return Objects.hash(providerName, databaseName); 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return MoreObjects.toStringHelper(this) 65 | .add("providerName", providerName) 66 | .add("databaseName", databaseName) 67 | .toString(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/support/ProviderResolver.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.support; 2 | 3 | public interface ProviderResolver { 4 | 5 | ProviderDescriptor getDescriptor(DatabaseDefinition definition); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/util/AnnotationUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.util; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import org.springframework.core.annotation.AnnotatedElementUtils; 21 | import org.springframework.util.ClassUtils; 22 | 23 | import java.util.LinkedHashSet; 24 | import java.util.Set; 25 | import java.util.concurrent.ConcurrentHashMap; 26 | import java.util.function.Function; 27 | import java.util.function.Predicate; 28 | 29 | import static io.zonky.test.db.util.ReflectionUtils.invokeStaticMethod; 30 | import static java.util.stream.Collectors.toCollection; 31 | 32 | public class AnnotationUtils { 33 | 34 | private static final Class testContextAnnotationUtilsClass; 35 | 36 | static { 37 | Class targetClass; 38 | try { 39 | ClassLoader classLoader = AnnotationUtils.class.getClassLoader(); 40 | targetClass = ClassUtils.forName("org.springframework.test.context.TestContextAnnotationUtils", classLoader); 41 | } catch (ClassNotFoundException e) { 42 | targetClass = null; 43 | } 44 | testContextAnnotationUtilsClass = targetClass; 45 | } 46 | 47 | private AnnotationUtils() {} 48 | 49 | public static Set getDatabaseAnnotations(Class annotatedElement) { 50 | Set annotations; 51 | 52 | if (testContextAnnotationUtilsClass != null) { 53 | annotations = invokeStaticMethod(testContextAnnotationUtilsClass, "getMergedRepeatableAnnotations", annotatedElement, AutoConfigureEmbeddedDatabase.class); 54 | } else { 55 | annotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, AutoConfigureEmbeddedDatabase.class); 56 | } 57 | 58 | return annotations.stream() 59 | .filter(distinctByKey(AutoConfigureEmbeddedDatabase::beanName)) 60 | .collect(toCollection(LinkedHashSet::new)); 61 | } 62 | 63 | private static Predicate distinctByKey(Function keyExtractor) { 64 | Set seen = ConcurrentHashMap.newKeySet(); 65 | return t -> seen.add(keyExtractor.apply(t)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/util/AopProxyUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.util; 18 | 19 | import io.zonky.test.db.context.DatabaseTargetSource; 20 | import io.zonky.test.db.context.DatabaseContext; 21 | import org.springframework.aop.TargetSource; 22 | import org.springframework.aop.framework.Advised; 23 | 24 | import javax.sql.DataSource; 25 | 26 | public class AopProxyUtils { 27 | 28 | private AopProxyUtils() {} 29 | 30 | public static DatabaseContext getDatabaseContext(DataSource dataSource) { 31 | if (dataSource instanceof Advised) { 32 | TargetSource targetSource = ((Advised) dataSource).getTargetSource(); 33 | if (targetSource instanceof DatabaseTargetSource) { 34 | return ((DatabaseTargetSource) targetSource).getContext(); 35 | } 36 | } 37 | return null; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/util/PropertyUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.util; 18 | 19 | import org.springframework.core.env.ConfigurableEnvironment; 20 | import org.springframework.core.env.EnumerablePropertySource; 21 | import org.springframework.core.env.Environment; 22 | import org.springframework.core.env.PropertySource; 23 | 24 | import java.util.EnumSet; 25 | import java.util.HashMap; 26 | import java.util.Locale; 27 | import java.util.Map; 28 | 29 | public class PropertyUtils { 30 | 31 | private PropertyUtils() {} 32 | 33 | public static Map extractAll(Environment environment, String prefix) { 34 | prefix += "."; 35 | Map properties = new HashMap<>(); 36 | if (environment instanceof ConfigurableEnvironment) { 37 | for (PropertySource propertySource : ((ConfigurableEnvironment) environment).getPropertySources()) { 38 | if (propertySource instanceof EnumerablePropertySource) { 39 | for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) { 40 | if (key.startsWith(prefix)) { 41 | properties.put(key.substring(prefix.length()), String.valueOf(propertySource.getProperty(key))); 42 | } 43 | } 44 | } 45 | } 46 | } 47 | return properties; 48 | } 49 | 50 | public static > E getEnumProperty(Environment environment, String key, Class enumType) { 51 | return getEnumProperty(environment, key, enumType, null); 52 | } 53 | 54 | public static > E getEnumProperty(Environment environment, String key, Class enumType, E defaultValue) { 55 | String enumName = environment.getProperty(key, String.class); 56 | if (enumName == null) { 57 | return defaultValue; 58 | } 59 | String normalizedEnumName = enumName.trim().replaceAll("-", "_").toUpperCase(Locale.ENGLISH); 60 | for (E candidate : EnumSet.allOf(enumType)) { 61 | if (candidate.name().equals(normalizedEnumName)) { 62 | return candidate; 63 | } 64 | } 65 | throw new IllegalArgumentException("No enum constant " + enumType.getCanonicalName() + "." + enumName); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/util/RandomStringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.util; 18 | 19 | import java.util.concurrent.ThreadLocalRandom; 20 | 21 | public class RandomStringUtils { 22 | 23 | private RandomStringUtils() {} 24 | 25 | public static String randomAlphabetic(int length) { 26 | int leftLimit = 97; // letter 'a' 27 | int rightLimit = 122; // letter 'z' 28 | ThreadLocalRandom random = ThreadLocalRandom.current(); 29 | return random.ints(leftLimit, rightLimit + 1) 30 | .limit(length) 31 | .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) 32 | .toString(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/main/java/io/zonky/test/db/util/StringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.util; 18 | 19 | import org.springframework.util.ObjectUtils; 20 | 21 | public class StringUtils { 22 | 23 | private StringUtils() {} 24 | 25 | public static boolean startsWithAny(String input, String... searchStrings) { 26 | if (org.springframework.util.StringUtils.isEmpty(input) || ObjectUtils.isEmpty(searchStrings)) { 27 | return false; 28 | } 29 | for (String searchString : searchStrings) { 30 | if (input.startsWith(searchString)) { 31 | return true; 32 | } 33 | } 34 | return false; 35 | } 36 | 37 | public static boolean endsWithAny(String input, String... searchStrings) { 38 | if (org.springframework.util.StringUtils.isEmpty(input) || ObjectUtils.isEmpty(searchStrings)) { 39 | return false; 40 | } 41 | for (String searchString : searchStrings) { 42 | if (input.endsWith(searchString)) { 43 | return true; 44 | } 45 | } 46 | return false; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/DerbyTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface DerbyTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/FlywayTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface FlywayTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/H2TestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface H2TestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/HSQLTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface HSQLTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/LiquibaseTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface LiquibaseTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/MSSQLTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface MSSQLTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/MariaDBTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface MariaDBTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/MySQLTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface MySQLTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/PostgresTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface PostgresTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/category/SpringTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.category; 18 | 19 | public interface SpringTestSuite { 20 | } 21 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/ConnectionLeakIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import io.zonky.test.category.FlywayTestSuite; 5 | import io.zonky.test.db.flyway.FlywayWrapper; 6 | import org.flywaydb.core.Flyway; 7 | import org.flywaydb.test.annotation.FlywayTest; 8 | import org.junit.Test; 9 | import org.junit.experimental.categories.Category; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.jdbc.core.JdbcTemplate; 15 | import org.springframework.test.annotation.Repeat; 16 | import org.springframework.test.annotation.Timed; 17 | import org.springframework.test.context.ContextConfiguration; 18 | import org.springframework.test.context.TestPropertySource; 19 | import org.springframework.test.context.junit4.SpringRunner; 20 | 21 | import javax.sql.DataSource; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | 28 | @RunWith(SpringRunner.class) 29 | @Category(FlywayTestSuite.class) 30 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 31 | @TestPropertySource(properties = "zonky.test.database.postgres.server.properties.max_connections=15") 32 | @ContextConfiguration 33 | public class ConnectionLeakIntegrationTest { 34 | 35 | private static final String SQL_SELECT_PERSONS = "select * from test.person"; 36 | 37 | @Configuration 38 | static class Config { 39 | 40 | @Bean(initMethod = "migrate") 41 | public Flyway flyway(DataSource dataSource) { 42 | FlywayWrapper wrapper = FlywayWrapper.newInstance(); 43 | wrapper.setDataSource(dataSource); 44 | wrapper.setSchemas(ImmutableList.of("test")); 45 | return wrapper.getFlyway(); 46 | } 47 | 48 | @Bean 49 | public JdbcTemplate jdbcTemplate(DataSource dataSource) { 50 | return new JdbcTemplate(dataSource); 51 | } 52 | } 53 | 54 | @Autowired 55 | private JdbcTemplate jdbcTemplate; 56 | 57 | @Test 58 | @Repeat(50) 59 | @Timed(millis = 30000) 60 | @FlywayTest(locationsForMigrate = "db/test_migration/appendable") 61 | public void testEmbeddedDatabase() { 62 | List> persons = jdbcTemplate.queryForList(SQL_SELECT_PERSONS); 63 | assertThat(persons).isNotNull().hasSize(2); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/DatabaseReplacePropertyIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db; 2 | 3 | import io.zonky.test.db.context.DatabaseContext; 4 | import io.zonky.test.db.provider.DatabaseProvider; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.TestPropertySource; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | 13 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 14 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.RefreshMode.AFTER_EACH_TEST_METHOD; 15 | import static org.assertj.core.api.Assertions.assertThat; 16 | 17 | @RunWith(SpringRunner.class) 18 | @TestPropertySource(properties = "zonky.test.database.replace=none") 19 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, refresh = AFTER_EACH_TEST_METHOD) 20 | @ContextConfiguration 21 | public class DatabaseReplacePropertyIntegrationTest { 22 | 23 | @Autowired 24 | private ApplicationContext applicationContext; 25 | @Autowired 26 | private DatabaseProvider dockerPostgresDatabaseProvider; 27 | 28 | @Test 29 | public void noDatabaseContextShouldExist() { 30 | assertThat(dockerPostgresDatabaseProvider).isNotNull(); 31 | assertThat(applicationContext.getBeanNamesForType(DatabaseContext.class)).isEmpty(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/EarlyInitializationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import io.zonky.test.category.FlywayTestSuite; 5 | import io.zonky.test.db.flyway.FlywayWrapper; 6 | import org.flywaydb.core.Flyway; 7 | import org.junit.Test; 8 | import org.junit.experimental.categories.Category; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 12 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.core.Ordered; 16 | import org.springframework.test.context.ContextConfiguration; 17 | import org.springframework.test.context.junit4.SpringRunner; 18 | 19 | import javax.sql.DataSource; 20 | 21 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 22 | import static org.assertj.core.api.Assertions.assertThat; 23 | 24 | @RunWith(SpringRunner.class) 25 | @Category(FlywayTestSuite.class) 26 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 27 | @ContextConfiguration 28 | public class EarlyInitializationIntegrationTest { 29 | 30 | @Configuration 31 | static class Config { 32 | 33 | @Bean 34 | public BeanFactoryPostProcessor beanFactoryPostProcessor() { 35 | return new TestBeanFactoryPostProcessor(); 36 | } 37 | 38 | @Bean(initMethod = "migrate") 39 | public Flyway flyway(DataSource dataSource) { 40 | FlywayWrapper wrapper = FlywayWrapper.newInstance(); 41 | wrapper.setDataSource(dataSource); 42 | wrapper.setSchemas(ImmutableList.of("test")); 43 | return wrapper.getFlyway(); 44 | } 45 | } 46 | 47 | @Autowired 48 | private DataSource dataSource; 49 | 50 | @Test 51 | public void testDataSource() { 52 | assertThat(dataSource).isNotNull(); 53 | } 54 | 55 | private static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { 56 | 57 | @Override 58 | public int getOrder() { 59 | return Ordered.HIGHEST_PRECEDENCE; 60 | } 61 | 62 | @Override 63 | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 64 | beanFactory.getBeanNamesForType(DataSource.class, true, true); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/MultipleDataSourcesIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.postgresql.ds.common.BaseDataSource; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | import java.sql.SQLException; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 32 | import static io.zonky.test.support.MockitoAssertions.mockWithName; 33 | import static org.assertj.core.api.Assertions.assertThat; 34 | import static org.mockito.Mockito.mock; 35 | 36 | @RunWith(SpringRunner.class) 37 | @AutoConfigureEmbeddedDatabase(beanName = "dataSource2", type = POSTGRES) 38 | @ContextConfiguration 39 | public class MultipleDataSourcesIntegrationTest { 40 | 41 | @Configuration 42 | static class Config { 43 | 44 | @Bean 45 | public DataSource dataSource1() { 46 | return mock(DataSource.class, "mockDataSource1"); 47 | } 48 | 49 | @Bean 50 | public DataSource dataSource2() { 51 | return mock(DataSource.class, "mockDataSource2"); 52 | } 53 | 54 | @Bean 55 | public DataSource dataSource3() { 56 | return mock(DataSource.class, "mockDataSource3"); 57 | } 58 | } 59 | 60 | @Autowired 61 | private DataSource dataSource1; 62 | @Autowired 63 | private DataSource dataSource2; 64 | @Autowired 65 | private DataSource dataSource3; 66 | 67 | @Test 68 | public void dataSource1ShouldBeMock() { 69 | assertThat(dataSource1).is(mockWithName("mockDataSource1")); 70 | } 71 | 72 | @Test 73 | public void dataSource2ShouldBePostgresDataSource() throws SQLException { 74 | assertThat(dataSource2).isNot(mockWithName("mockDataSource2")); 75 | assertThat(dataSource2.unwrap(BaseDataSource.class)).isNotNull(); 76 | } 77 | 78 | @Test 79 | public void dataSource3ShouldBeMock() { 80 | assertThat(dataSource3).is(mockWithName("mockDataSource3")); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/ReplacingDefaultDataSourceIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.postgresql.ds.PGSimpleDataSource; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.context.annotation.Primary; 26 | import org.springframework.test.context.ContextConfiguration; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | import static org.mockito.Mockito.mock; 34 | 35 | @RunWith(SpringRunner.class) 36 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 37 | @ContextConfiguration 38 | public class ReplacingDefaultDataSourceIntegrationTest { 39 | 40 | @Configuration 41 | static class Config { 42 | 43 | @Bean 44 | @Primary 45 | public DataSource dataSource() { 46 | return mock(DataSource.class, "mockDataSource"); 47 | } 48 | } 49 | 50 | @Autowired 51 | private DataSource dataSource; 52 | 53 | @Test 54 | public void primaryDataSourceShouldBeReplaced() throws Exception { 55 | assertThat(dataSource.unwrap(PGSimpleDataSource.class)).isExactlyInstanceOf(PGSimpleDataSource.class); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/SpringBootFlywayPropertiesIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import io.zonky.test.category.FlywayTestSuite; 20 | import io.zonky.test.db.flyway.FlywayWrapper; 21 | import io.zonky.test.support.ConditionalTestRule; 22 | import io.zonky.test.support.TestAssumptions; 23 | import org.flywaydb.core.Flyway; 24 | import org.junit.ClassRule; 25 | import org.junit.Test; 26 | import org.junit.experimental.categories.Category; 27 | import org.junit.runner.RunWith; 28 | import org.springframework.beans.factory.annotation.Autowired; 29 | import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.test.context.TestPropertySource; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import javax.sql.DataSource; 35 | 36 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 37 | import static org.assertj.core.api.Assertions.assertThat; 38 | 39 | @RunWith(SpringRunner.class) 40 | @Category(FlywayTestSuite.class) 41 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 42 | @TestPropertySource(properties = { 43 | "liquibase.enabled=false", 44 | "spring.liquibase.enabled=false", 45 | 46 | "flyway.url=jdbc:postgresql://localhost:5432/test", 47 | "flyway.user=flyway", 48 | "flyway.password=password", 49 | "flyway.schemas=test", 50 | 51 | "spring.flyway.url=jdbc:postgresql://localhost:5432/test", 52 | "spring.flyway.user=flyway", 53 | "spring.flyway.password=password", 54 | "spring.flyway.schemas=test" 55 | }) 56 | @JdbcTest 57 | public class SpringBootFlywayPropertiesIntegrationTest { 58 | 59 | @ClassRule 60 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeSpringBootSupportsJdbcTestAnnotation); 61 | 62 | @Configuration 63 | static class Config {} 64 | 65 | @Autowired 66 | private Flyway flyway; 67 | 68 | @Autowired 69 | private DataSource dataSource; 70 | 71 | @Test 72 | public void test() { 73 | FlywayWrapper wrapper = FlywayWrapper.forBean(flyway); 74 | assertThat(wrapper.getDataSource()).isSameAs(dataSource); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/SpringBootLiquibaseIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import io.zonky.test.category.LiquibaseTestSuite; 20 | import io.zonky.test.support.ConditionalTestRule; 21 | import io.zonky.test.support.TestAssumptions; 22 | import org.junit.ClassRule; 23 | import org.junit.Test; 24 | import org.junit.experimental.categories.Category; 25 | import org.junit.runner.RunWith; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.jdbc.core.JdbcTemplate; 31 | import org.springframework.test.context.TestPropertySource; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import javax.sql.DataSource; 35 | import java.util.List; 36 | import java.util.Map; 37 | 38 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 39 | import static org.assertj.core.api.Assertions.assertThat; 40 | import static org.assertj.core.api.Assertions.entry; 41 | 42 | @RunWith(SpringRunner.class) 43 | @Category(LiquibaseTestSuite.class) 44 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 45 | @TestPropertySource(properties = { 46 | "flyway.enabled=false", 47 | "spring.flyway.enabled=false", 48 | }) 49 | @JdbcTest 50 | public class SpringBootLiquibaseIntegrationTest { 51 | 52 | @ClassRule 53 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeSpringBootSupportsJdbcTestAnnotation); 54 | 55 | private static final String SQL_SELECT_PERSONS = "select * from test.person"; 56 | 57 | @Configuration 58 | static class Config { 59 | 60 | @Bean 61 | public JdbcTemplate jdbcTemplate(DataSource dataSource) { 62 | return new JdbcTemplate(dataSource); 63 | } 64 | } 65 | 66 | @Autowired 67 | private JdbcTemplate jdbcTemplate; 68 | 69 | @Test 70 | public void testJdbcTemplate() { 71 | assertThat(jdbcTemplate).isNotNull(); 72 | 73 | List> persons = jdbcTemplate.queryForList(SQL_SELECT_PERSONS); 74 | assertThat(persons).isNotNull().hasSize(1); 75 | 76 | Map person = persons.get(0); 77 | assertThat(person).containsExactly( 78 | entry("id", 1L), 79 | entry("first_name", "Dave"), 80 | entry("last_name", "Syer")); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/SpringBootLiquibasePropertiesIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import io.zonky.test.category.LiquibaseTestSuite; 20 | import io.zonky.test.support.ConditionalTestRule; 21 | import io.zonky.test.support.TestAssumptions; 22 | import liquibase.integration.spring.SpringLiquibase; 23 | import org.junit.ClassRule; 24 | import org.junit.Test; 25 | import org.junit.experimental.categories.Category; 26 | import org.junit.runner.RunWith; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.test.context.TestPropertySource; 31 | import org.springframework.test.context.junit4.SpringRunner; 32 | 33 | import javax.sql.DataSource; 34 | 35 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 36 | import static org.assertj.core.api.Assertions.assertThat; 37 | 38 | @RunWith(SpringRunner.class) 39 | @Category(LiquibaseTestSuite.class) 40 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 41 | @TestPropertySource(properties = { 42 | "flyway.enabled=false", 43 | "spring.flyway.enabled=false", 44 | 45 | "liquibase.url=jdbc:postgresql://localhost:5432/test", 46 | "liquibase.user=flyway", 47 | "liquibase.password=password", 48 | 49 | "spring.liquibase.url=jdbc:postgresql://localhost:5432/test", 50 | "spring.liquibase.user=flyway", 51 | "spring.liquibase.password=password", 52 | }) 53 | @JdbcTest 54 | public class SpringBootLiquibasePropertiesIntegrationTest { 55 | 56 | @ClassRule 57 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeSpringBootSupportsJdbcTestAnnotation); 58 | 59 | @Configuration 60 | static class Config {} 61 | 62 | @Autowired 63 | private SpringLiquibase liquibase; 64 | 65 | @Autowired 66 | private DataSource dataSource; 67 | 68 | @Test 69 | public void test() { 70 | assertThat(liquibase.getDataSource()).isSameAs(dataSource); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/SpringProxiedBeanMethodsIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db; 18 | 19 | import io.zonky.test.category.SpringTestSuite; 20 | import io.zonky.test.support.ConditionalTestRule; 21 | import io.zonky.test.support.TestAssumptions; 22 | import org.junit.ClassRule; 23 | import org.junit.Test; 24 | import org.junit.experimental.categories.Category; 25 | import org.junit.runner.RunWith; 26 | import org.mockito.Mockito; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.jdbc.core.JdbcTemplate; 31 | import org.springframework.test.context.junit4.SpringRunner; 32 | 33 | import javax.sql.DataSource; 34 | 35 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 36 | import static io.zonky.test.support.MockitoAssertions.mock; 37 | import static org.assertj.core.api.Assertions.assertThat; 38 | 39 | @RunWith(SpringRunner.class) 40 | @Category(SpringTestSuite.class) 41 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 42 | public class SpringProxiedBeanMethodsIntegrationTest { 43 | 44 | @ClassRule 45 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeSpringSupportsInstanceSupplier); 46 | 47 | @Configuration 48 | static class Config { 49 | 50 | @Bean 51 | public DataSource dataSource() { 52 | return Mockito.mock(DataSource.class); 53 | } 54 | 55 | @Bean 56 | public JdbcTemplate jdbcTemplate() { 57 | return new JdbcTemplate(dataSource()); 58 | } 59 | } 60 | 61 | @Autowired 62 | private DataSource dataSource; 63 | 64 | @Autowired 65 | private JdbcTemplate jdbcTemplate; 66 | 67 | @Test 68 | public void checkInitializedBeans() { 69 | assertThat(dataSource).isNotNull().isNot(mock()); 70 | assertThat(jdbcTemplate).isNotNull(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/config/ConfigurationPropertiesIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.postgresql.ds.PGSimpleDataSource; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.jdbc.core.JdbcTemplate; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.TestPropertySource; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 36 | @TestPropertySource(properties = { 37 | "zonky.test.database.postgres.client.properties.stringtype=unspecified", 38 | "zonky.test.database.postgres.initdb.properties.lc-collate=cs_CZ.UTF-8", 39 | "zonky.test.database.postgres.server.properties.max_connections=100", 40 | "zonky.test.database.postgres.server.properties.shared_buffers=64MB", 41 | }) 42 | @ContextConfiguration 43 | public class ConfigurationPropertiesIntegrationTest { 44 | 45 | @Autowired 46 | private DataSource dataSource; 47 | 48 | @Test 49 | public void testConfigurationProperties() throws Exception { 50 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getProperty("stringtype")).isEqualTo("unspecified"); 51 | 52 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 53 | 54 | String collate = jdbcTemplate.queryForObject("show lc_collate", String.class); 55 | assertThat(collate).isEqualTo("cs_CZ.UTF-8"); 56 | 57 | String maxConnections = jdbcTemplate.queryForObject("show max_connections", String.class); 58 | assertThat(maxConnections).isEqualTo("100"); 59 | 60 | String sharedBuffers = jdbcTemplate.queryForObject("show shared_buffers", String.class); 61 | assertThat(sharedBuffers).isEqualTo("64MB"); 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/config/DatabaseProvidersIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.config; 2 | 3 | import io.zonky.test.category.SpringTestSuite; 4 | import io.zonky.test.db.provider.DatabaseProvider; 5 | import io.zonky.test.db.support.DatabaseProviders; 6 | import io.zonky.test.db.support.ProviderDescriptor; 7 | import org.junit.Test; 8 | import org.junit.experimental.categories.Category; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | import org.springframework.test.context.ContextConfiguration; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | 17 | import static io.zonky.test.support.MockitoAssertions.mockWithName; 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | import static org.assertj.core.api.Assertions.assertThatCode; 20 | import static org.mockito.Mockito.mock; 21 | 22 | @RunWith(SpringRunner.class) 23 | @Category(SpringTestSuite.class) 24 | @ContextConfiguration 25 | public class DatabaseProvidersIntegrationTest { 26 | 27 | @Configuration 28 | @Import(DatabaseProviders.class) 29 | static class Config { 30 | 31 | @Bean 32 | @Provider(type = "provider1", database = "database1") 33 | public DatabaseProvider provider1() { 34 | return mock(DatabaseProvider.class, "mockProvider1"); 35 | } 36 | 37 | @Bean 38 | @Provider(type = "provider1", database = "database2") 39 | public DatabaseProvider provider2() { 40 | return mock(DatabaseProvider.class, "mockProvider2"); 41 | } 42 | 43 | @Bean 44 | @Provider(type = "provider2", database = "database2") 45 | public DatabaseProvider provider3() { 46 | return mock(DatabaseProvider.class, "mockProvider3"); 47 | } 48 | } 49 | 50 | @Autowired 51 | private DatabaseProviders databaseProviders; 52 | 53 | @Test 54 | public void testProviders() { 55 | assertThat(databaseProviders.getProvider(ProviderDescriptor.of("provider1", "database1"))).is(mockWithName("mockProvider1")); 56 | assertThat(databaseProviders.getProvider(ProviderDescriptor.of("provider1", "database2"))).is(mockWithName("mockProvider2")); 57 | assertThat(databaseProviders.getProvider(ProviderDescriptor.of("provider2", "database2"))).is(mockWithName("mockProvider3")); 58 | } 59 | 60 | @Test 61 | public void missingProvider() { 62 | assertThatCode(() -> databaseProviders.getProvider(ProviderDescriptor.of("provider3", "database2"))) 63 | .isExactlyInstanceOf(MissingDatabaseProviderException.class) 64 | .hasMessage("Missing database provider for: ProviderDescriptor{providerName=provider3, databaseName=database2}, available providers: [provider1, provider2]"); 65 | } 66 | } -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/config/ProviderTypePriorityIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.postgresql.ds.PGSimpleDataSource; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.test.context.ContextConfiguration; 25 | import org.springframework.test.context.TestPropertySource; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | 30 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.DOCKER; 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = DOCKER) 36 | @TestPropertySource(properties = "zonky.test.database.provider=zonky") 37 | @ContextConfiguration 38 | public class ProviderTypePriorityIntegrationTest { 39 | 40 | @Autowired 41 | private DataSource dataSource; 42 | 43 | @Test 44 | public void dockerProviderShouldBeUsed() throws Exception { 45 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("docker"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/config/ProviderTypePropertyIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.config; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.postgresql.ds.PGSimpleDataSource; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.test.context.ContextConfiguration; 25 | import org.springframework.test.context.TestPropertySource; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | 30 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 31 | import static org.assertj.core.api.Assertions.assertThat; 32 | 33 | @RunWith(SpringRunner.class) 34 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 35 | @TestPropertySource(properties = "zonky.test.database.provider=docker") 36 | @ContextConfiguration 37 | public class ProviderTypePropertyIntegrationTest { 38 | 39 | @Autowired 40 | private DataSource dataSource; 41 | 42 | @Test 43 | public void dockerProviderShouldBeUsed() throws Exception { 44 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("docker"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/event/EventPublishingTestExecutionListenerTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.event; 2 | 3 | import io.zonky.test.category.SpringTestSuite; 4 | import org.junit.Test; 5 | import org.junit.experimental.categories.Category; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.context.event.EventListener; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.concurrent.atomic.AtomicInteger; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | @RunWith(SpringRunner.class) 21 | @Category(SpringTestSuite.class) 22 | @ContextConfiguration 23 | public class EventPublishingTestExecutionListenerTest { 24 | 25 | @Configuration 26 | static class Config { 27 | 28 | @Bean 29 | public TestExecutionContext testExecutionContext() { 30 | return new TestExecutionContext(); 31 | } 32 | } 33 | 34 | @Autowired 35 | private TestExecutionContext testExecutionContext; 36 | 37 | @Test 38 | public void test1() { 39 | int count = testExecutionContext.executedTestCount.incrementAndGet(); 40 | assertThat(testExecutionContext.executionStartedEvents.size()).isEqualTo(count); 41 | assertThat(testExecutionContext.executionFinishedEvents.size()).isEqualTo(count - 1); 42 | } 43 | 44 | @Test 45 | public void test2() { 46 | int count = testExecutionContext.executedTestCount.incrementAndGet(); 47 | assertThat(testExecutionContext.executionStartedEvents.size()).isEqualTo(count); 48 | assertThat(testExecutionContext.executionFinishedEvents.size()).isEqualTo(count - 1); 49 | } 50 | 51 | @Test 52 | public void test3() { 53 | int count = testExecutionContext.executedTestCount.incrementAndGet(); 54 | assertThat(testExecutionContext.executionStartedEvents.size()).isEqualTo(count); 55 | assertThat(testExecutionContext.executionFinishedEvents.size()).isEqualTo(count - 1); 56 | } 57 | 58 | private static class TestExecutionContext { 59 | 60 | private final AtomicInteger executedTestCount = new AtomicInteger(); 61 | private final List executionStartedEvents = new ArrayList<>(); 62 | private final List executionFinishedEvents = new ArrayList<>(); 63 | 64 | @EventListener 65 | public synchronized void handleTestStarted(TestExecutionStartedEvent event) { 66 | executionStartedEvents.add(event); 67 | } 68 | 69 | @EventListener 70 | public synchronized void handleTestFinished(TestExecutionFinishedEvent event) { 71 | executionFinishedEvents.add(event); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/liquibase/LiquibaseDatabaseExtensionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.liquibase; 18 | 19 | import io.zonky.test.category.LiquibaseTestSuite; 20 | import io.zonky.test.db.context.DatabaseContext; 21 | import io.zonky.test.db.context.DatabaseTargetSource; 22 | import liquibase.exception.LiquibaseException; 23 | import liquibase.integration.spring.SpringLiquibase; 24 | import org.junit.Before; 25 | import org.junit.Test; 26 | import org.junit.experimental.categories.Category; 27 | import org.junit.runner.RunWith; 28 | import org.mockito.Mock; 29 | import org.mockito.runners.MockitoJUnitRunner; 30 | import org.springframework.aop.framework.Advised; 31 | 32 | import javax.sql.DataSource; 33 | 34 | import static org.mockito.Mockito.mock; 35 | import static org.mockito.Mockito.verify; 36 | import static org.mockito.Mockito.when; 37 | import static org.mockito.Mockito.withSettings; 38 | 39 | @RunWith(MockitoJUnitRunner.class) 40 | @Category(LiquibaseTestSuite.class) 41 | public class LiquibaseDatabaseExtensionTest { 42 | 43 | @Mock 44 | private DatabaseContext databaseContext; 45 | 46 | private SpringLiquibase liquibase; 47 | 48 | @Before 49 | public void setUp() { 50 | Advised dataSource = mock(Advised.class, withSettings().extraInterfaces(DataSource.class)); 51 | when(dataSource.getTargetSource()).thenReturn(new DatabaseTargetSource(databaseContext)); 52 | 53 | SpringLiquibase liquibase = new SpringLiquibase(); 54 | liquibase.setDataSource((DataSource) dataSource); 55 | 56 | LiquibaseDatabaseExtension extension = new LiquibaseDatabaseExtension(); 57 | this.liquibase = (SpringLiquibase) extension.postProcessBeforeInitialization(liquibase, "liquibase"); 58 | } 59 | 60 | @Test 61 | public void testMigrate() throws LiquibaseException { 62 | liquibase.afterPropertiesSet(); 63 | 64 | verify(databaseContext).apply(new LiquibaseDatabasePreparer(LiquibaseDescriptor.from(liquibase))); 65 | } 66 | } -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/liquibase/LiquibaseDatabasePreparerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.liquibase; 18 | 19 | import io.zonky.test.category.LiquibaseTestSuite; 20 | import liquibase.integration.spring.SpringLiquibase; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | import org.junit.experimental.categories.Category; 24 | import org.springframework.core.io.DefaultResourceLoader; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | 28 | @Category(LiquibaseTestSuite.class) 29 | public class LiquibaseDatabasePreparerTest { 30 | 31 | private LiquibaseDatabasePreparer preparer; 32 | 33 | @Before 34 | public void setUp() throws Exception { 35 | SpringLiquibase liquibase = new SpringLiquibase(); 36 | liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml"); 37 | liquibase.setResourceLoader(new DefaultResourceLoader()); 38 | LiquibaseDescriptor descriptor = LiquibaseDescriptor.from(liquibase); 39 | 40 | preparer = new LiquibaseDatabasePreparer(descriptor); 41 | } 42 | 43 | @Test 44 | public void estimatedDuration() { 45 | assertThat(preparer.estimatedDuration()).isEqualTo(214); 46 | } 47 | } -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DefaultProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.postgresql.ds.PGSimpleDataSource; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.test.context.ContextConfiguration; 25 | import org.springframework.test.context.junit4.SpringRunner; 26 | 27 | import javax.sql.DataSource; 28 | import java.sql.SQLException; 29 | 30 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 31 | import static org.assertj.core.api.Assertions.assertThat; 32 | 33 | @RunWith(SpringRunner.class) 34 | @AutoConfigureEmbeddedDatabase(type = POSTGRES) 35 | @ContextConfiguration 36 | public class DefaultProviderIntegrationTest { 37 | 38 | @Autowired 39 | private DataSource dataSource; 40 | 41 | @Test 42 | public void testDataSource() throws SQLException { 43 | assertThat(dataSource.unwrap(PGSimpleDataSource.class)).isNotNull(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DerbyProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.DerbyTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | import java.sql.SQLException; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.DERBY; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @Category(DerbyTestSuite.class) 36 | @AutoConfigureEmbeddedDatabase(type = DERBY) 37 | @ContextConfiguration 38 | public class DerbyProviderIntegrationTest { 39 | 40 | @Autowired 41 | private DataSource dataSource; 42 | 43 | @Test 44 | public void testDataSource() throws SQLException { 45 | assertThat(dataSource.unwrap(EmbeddedDatabase.class).getJdbcUrl()).startsWith("jdbc:derby:"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMSSQLProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.provider; 2 | 3 | import io.zonky.test.category.MSSQLTestSuite; 4 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 5 | import io.zonky.test.db.provider.mssql.MsSQLEmbeddedDatabase; 6 | import io.zonky.test.support.ConditionalTestRule; 7 | import io.zonky.test.support.TestAssumptions; 8 | import org.junit.ClassRule; 9 | import org.junit.Test; 10 | import org.junit.experimental.categories.Category; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.test.context.ContextConfiguration; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import javax.sql.DataSource; 17 | import java.sql.SQLException; 18 | 19 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MSSQL; 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @Category(MSSQLTestSuite.class) 24 | @AutoConfigureEmbeddedDatabase(type = MSSQL) 25 | @ContextConfiguration 26 | public class DockerMSSQLProviderIntegrationTest { 27 | 28 | @ClassRule 29 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeLicenceAcceptance); 30 | 31 | @Autowired 32 | private DataSource dataSource; 33 | 34 | @Test 35 | public void testDataSource() throws SQLException { 36 | assertThat(dataSource.unwrap(MsSQLEmbeddedDatabase.class).getJdbcUrl()).contains("password=A_Str0ng_Required_Password"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMSSQLProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.provider; 2 | 3 | import io.zonky.test.category.MSSQLTestSuite; 4 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 5 | import io.zonky.test.db.provider.mssql.MSSQLServerContainerCustomizer; 6 | import io.zonky.test.db.provider.mssql.MsSQLEmbeddedDatabase; 7 | import io.zonky.test.support.ConditionalTestRule; 8 | import io.zonky.test.support.TestAssumptions; 9 | import org.junit.ClassRule; 10 | import org.junit.Test; 11 | import org.junit.experimental.categories.Category; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.jdbc.core.JdbcTemplate; 17 | import org.springframework.test.context.ContextConfiguration; 18 | import org.springframework.test.context.TestPropertySource; 19 | import org.springframework.test.context.junit4.SpringRunner; 20 | 21 | import javax.sql.DataSource; 22 | import java.sql.SQLException; 23 | 24 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MSSQL; 25 | import static org.assertj.core.api.Assertions.assertThat; 26 | 27 | @RunWith(SpringRunner.class) 28 | @Category(MSSQLTestSuite.class) 29 | @AutoConfigureEmbeddedDatabase(type = MSSQL) 30 | @TestPropertySource(properties = { 31 | "zonky.test.database.mssql.docker.image=mcr.microsoft.com/mssql/server:2017-CU20" 32 | }) 33 | @ContextConfiguration 34 | public class DockerMSSQLProviderWithConfigurationIntegrationTest { 35 | 36 | @ClassRule 37 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeLicenceAcceptance); 38 | 39 | @Configuration 40 | static class Config { 41 | 42 | @Bean 43 | public MSSQLServerContainerCustomizer mssqlContainerCustomizer() { 44 | return container -> container.withPassword("docker_Str0ng_Required_Password"); 45 | } 46 | } 47 | 48 | @Autowired 49 | private DataSource dataSource; 50 | 51 | @Test 52 | public void testDataSource() throws SQLException { 53 | assertThat(dataSource.unwrap(MsSQLEmbeddedDatabase.class).getJdbcUrl()).contains("password=docker_Str0ng_Required_Password"); 54 | 55 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 56 | String databaseVersion = jdbcTemplate.queryForObject("select @@version", String.class); 57 | assertThat(databaseVersion).startsWith("Microsoft SQL Server 2017 (RTM-CU20)"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMariaDBProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.MariaDBTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | import java.sql.SQLException; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MARIADB; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @Category(MariaDBTestSuite.class) 36 | @AutoConfigureEmbeddedDatabase(type = MARIADB) 37 | @ContextConfiguration 38 | public class DockerMariaDBProviderIntegrationTest { 39 | 40 | @Autowired 41 | private DataSource dataSource; 42 | 43 | @Test 44 | public void testDataSource() throws SQLException { 45 | assertThat(dataSource.unwrap(EmbeddedDatabase.class).getJdbcUrl()).contains("password=docker"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMariaDBProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.MariaDBTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import io.zonky.test.db.provider.mariadb.MariaDBContainerCustomizer; 22 | import org.junit.Test; 23 | import org.junit.experimental.categories.Category; 24 | import org.junit.runner.RunWith; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.context.annotation.Bean; 27 | import org.springframework.context.annotation.Configuration; 28 | import org.springframework.jdbc.core.JdbcTemplate; 29 | import org.springframework.test.context.ContextConfiguration; 30 | import org.springframework.test.context.TestPropertySource; 31 | import org.springframework.test.context.junit4.SpringRunner; 32 | 33 | import javax.sql.DataSource; 34 | import java.sql.SQLException; 35 | 36 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MARIADB; 37 | import static org.assertj.core.api.Assertions.assertThat; 38 | 39 | @RunWith(SpringRunner.class) 40 | @Category(MariaDBTestSuite.class) 41 | @AutoConfigureEmbeddedDatabase(type = MARIADB) 42 | @ContextConfiguration 43 | @TestPropertySource(properties = { 44 | "zonky.test.database.mariadb.docker.image=mariadb:10.1" 45 | }) 46 | public class DockerMariaDBProviderWithConfigurationIntegrationTest { 47 | 48 | @Configuration 49 | static class Config { 50 | 51 | @Bean 52 | public MariaDBContainerCustomizer mariadbContainerCustomizer() { 53 | return container -> container.withPassword("docker-mariadb"); 54 | } 55 | } 56 | 57 | @Autowired 58 | private DataSource dataSource; 59 | 60 | @Test 61 | public void testDataSource() throws SQLException { 62 | assertThat(dataSource.unwrap(EmbeddedDatabase.class).getJdbcUrl()).contains("password=docker-mariadb"); 63 | 64 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 65 | String databaseVersion = jdbcTemplate.queryForObject("select @@version", String.class); 66 | assertThat(databaseVersion).startsWith("10.1"); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMySQLProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import com.mysql.cj.jdbc.MysqlDataSource; 20 | import io.zonky.test.category.MySQLTestSuite; 21 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 22 | import org.junit.Test; 23 | import org.junit.experimental.categories.Category; 24 | import org.junit.runner.RunWith; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.test.context.ContextConfiguration; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | import java.sql.SQLException; 31 | 32 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MYSQL; 33 | import static org.assertj.core.api.Assertions.assertThat; 34 | 35 | @RunWith(SpringRunner.class) 36 | @Category(MySQLTestSuite.class) 37 | @AutoConfigureEmbeddedDatabase(type = MYSQL) 38 | @ContextConfiguration 39 | public class DockerMySQLProviderIntegrationTest { 40 | 41 | @Autowired 42 | private DataSource dataSource; 43 | 44 | @Test 45 | public void testDataSource() throws SQLException { 46 | assertThat(dataSource.unwrap(MysqlDataSource.class).getPassword()).isEqualTo("docker"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerMySQLProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import com.mysql.cj.jdbc.MysqlDataSource; 20 | import io.zonky.test.category.MySQLTestSuite; 21 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 22 | import io.zonky.test.db.provider.mysql.MySQLContainerCustomizer; 23 | import org.junit.Test; 24 | import org.junit.experimental.categories.Category; 25 | import org.junit.runner.RunWith; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | import org.springframework.jdbc.core.JdbcTemplate; 30 | import org.springframework.test.context.ContextConfiguration; 31 | import org.springframework.test.context.TestPropertySource; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import javax.sql.DataSource; 35 | import java.sql.SQLException; 36 | 37 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.MYSQL; 38 | import static org.assertj.core.api.Assertions.assertThat; 39 | 40 | @RunWith(SpringRunner.class) 41 | @Category(MySQLTestSuite.class) 42 | @AutoConfigureEmbeddedDatabase(type = MYSQL) 43 | @ContextConfiguration 44 | @TestPropertySource(properties = { 45 | "zonky.test.database.mysql.docker.image=mysql:5.6.48" 46 | }) 47 | public class DockerMySQLProviderWithConfigurationIntegrationTest { 48 | 49 | @Configuration 50 | static class Config { 51 | 52 | @Bean 53 | public MySQLContainerCustomizer mySqlContainerCustomizer() { 54 | return container -> container.withPassword("docker-mysql"); 55 | } 56 | } 57 | 58 | @Autowired 59 | private DataSource dataSource; 60 | 61 | @Test 62 | public void testDataSource() throws SQLException { 63 | assertThat(dataSource.unwrap(MysqlDataSource.class).getPassword()).isEqualTo("docker-mysql"); 64 | 65 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 66 | String databaseVersion = jdbcTemplate.queryForObject("select @@version", String.class); 67 | assertThat(databaseVersion).startsWith("5.6.48"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerPostgresProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.postgresql.ds.PGSimpleDataSource; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.test.context.ContextConfiguration; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | import java.sql.SQLException; 31 | 32 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.DOCKER; 33 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 34 | import static org.assertj.core.api.Assertions.assertThat; 35 | 36 | @RunWith(SpringRunner.class) 37 | @Category(PostgresTestSuite.class) 38 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = DOCKER) 39 | @ContextConfiguration 40 | public class DockerPostgresProviderIntegrationTest { 41 | 42 | @Autowired 43 | private DataSource dataSource; 44 | 45 | @Test 46 | public void testDataSource() throws SQLException { 47 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("docker"); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerPostgresProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import io.zonky.test.db.provider.postgres.PostgreSQLContainerCustomizer; 22 | import org.junit.Test; 23 | import org.junit.experimental.categories.Category; 24 | import org.junit.runner.RunWith; 25 | import org.postgresql.ds.PGSimpleDataSource; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | import org.springframework.jdbc.core.JdbcTemplate; 30 | import org.springframework.test.context.ContextConfiguration; 31 | import org.springframework.test.context.TestPropertySource; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import javax.sql.DataSource; 35 | import java.sql.SQLException; 36 | 37 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.DOCKER; 38 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 39 | import static org.assertj.core.api.Assertions.assertThat; 40 | 41 | @RunWith(SpringRunner.class) 42 | @Category(PostgresTestSuite.class) 43 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = DOCKER) 44 | @TestPropertySource(properties = { 45 | "zonky.test.database.postgres.docker.image=postgres:9.6-alpine", 46 | "zonky.test.database.postgres.docker.tmpfs.enabled=true" 47 | }) 48 | @ContextConfiguration 49 | public class DockerPostgresProviderWithConfigurationIntegrationTest { 50 | 51 | @Configuration 52 | static class Config { 53 | 54 | @Bean 55 | public PostgreSQLContainerCustomizer postgresContainerCustomizer() { 56 | return container -> container.withPassword("docker-postgres"); 57 | } 58 | } 59 | 60 | @Autowired 61 | private DataSource dataSource; 62 | 63 | @Test 64 | public void testDataSource() throws SQLException { 65 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("docker-postgres"); 66 | 67 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 68 | String version = jdbcTemplate.queryForObject("show server_version", String.class); 69 | assertThat(version).startsWith("9.6."); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/DockerPostgresProviderWithPostgisImageIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 20 | import io.zonky.test.db.provider.postgres.PostgreSQLContainerCustomizer; 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.postgresql.ds.PGSimpleDataSource; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | import org.springframework.jdbc.core.JdbcTemplate; 28 | import org.springframework.test.context.ContextConfiguration; 29 | import org.springframework.test.context.TestPropertySource; 30 | import org.springframework.test.context.junit4.SpringRunner; 31 | 32 | import javax.sql.DataSource; 33 | import java.sql.SQLException; 34 | 35 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.DOCKER; 36 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 37 | import static org.assertj.core.api.Assertions.assertThat; 38 | 39 | @RunWith(SpringRunner.class) 40 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = DOCKER) 41 | @TestPropertySource(properties = { 42 | "zonky.test.database.postgres.docker.image=postgis/postgis:9.6-3.1-alpine" 43 | }) 44 | @ContextConfiguration 45 | public class DockerPostgresProviderWithPostgisImageIntegrationTest { 46 | 47 | @Configuration 48 | static class Config { 49 | 50 | @Bean 51 | public PostgreSQLContainerCustomizer postgresContainerCustomizer() { 52 | return container -> container.withPassword("docker-postgis"); 53 | } 54 | } 55 | 56 | @Autowired 57 | private DataSource dataSource; 58 | 59 | @Test 60 | public void testDataSource() throws SQLException { 61 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("docker-postgis"); 62 | 63 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 64 | 65 | String postgresVersion = jdbcTemplate.queryForObject("show server_version", String.class); 66 | assertThat(postgresVersion).startsWith("9.6."); 67 | 68 | jdbcTemplate.update("create extension postgis"); 69 | String postgisVersion = jdbcTemplate.queryForObject("select postgis_version()", String.class); 70 | assertThat(postgisVersion).startsWith("3.1"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/H2ProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.H2TestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | import java.sql.SQLException; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.H2; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @Category(H2TestSuite.class) 36 | @AutoConfigureEmbeddedDatabase(type = H2) 37 | @ContextConfiguration 38 | public class H2ProviderIntegrationTest { 39 | 40 | @Autowired 41 | private DataSource dataSource; 42 | 43 | @Test 44 | public void testDataSource() throws SQLException { 45 | assertThat(dataSource.unwrap(EmbeddedDatabase.class).getJdbcUrl()).startsWith("jdbc:h2:"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/HSQLProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.HSQLTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringRunner; 27 | 28 | import javax.sql.DataSource; 29 | import java.sql.SQLException; 30 | 31 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.HSQL; 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | @RunWith(SpringRunner.class) 35 | @Category(HSQLTestSuite.class) 36 | @AutoConfigureEmbeddedDatabase(type = HSQL) 37 | @ContextConfiguration 38 | public class HSQLProviderIntegrationTest { 39 | 40 | @Autowired 41 | private DataSource dataSource; 42 | 43 | @Test 44 | public void testDataSource() throws SQLException { 45 | assertThat(dataSource.unwrap(EmbeddedDatabase.class).getJdbcUrl()).startsWith("jdbc:hsqldb:"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/OpenTableProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.postgresql.ds.PGSimpleDataSource; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.test.context.ContextConfiguration; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | import java.sql.SQLException; 31 | 32 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.OPENTABLE; 33 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 34 | import static org.assertj.core.api.Assertions.assertThat; 35 | 36 | @RunWith(SpringRunner.class) 37 | @Category(PostgresTestSuite.class) 38 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = OPENTABLE) 39 | @ContextConfiguration 40 | public class OpenTableProviderIntegrationTest { 41 | 42 | @Autowired 43 | private DataSource dataSource; 44 | 45 | @Test 46 | public void testDataSource() throws SQLException { 47 | assertThat(dataSource.unwrap(PGSimpleDataSource.class)).isNotNull(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/OpenTableProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import com.opentable.db.postgres.embedded.EmbeddedPostgres; 20 | import io.zonky.test.category.PostgresTestSuite; 21 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 22 | import io.zonky.test.support.TestSocketUtils; 23 | import org.junit.Test; 24 | import org.junit.experimental.categories.Category; 25 | import org.junit.runner.RunWith; 26 | import org.postgresql.ds.PGSimpleDataSource; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.test.context.ContextConfiguration; 31 | import org.springframework.test.context.junit4.SpringRunner; 32 | 33 | import javax.sql.DataSource; 34 | import java.sql.SQLException; 35 | import java.util.function.Consumer; 36 | 37 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.OPENTABLE; 38 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 39 | import static org.assertj.core.api.Assertions.assertThat; 40 | 41 | @RunWith(SpringRunner.class) 42 | @Category(PostgresTestSuite.class) 43 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = OPENTABLE) 44 | @ContextConfiguration 45 | public class OpenTableProviderWithConfigurationIntegrationTest { 46 | 47 | @Configuration 48 | static class Config { 49 | 50 | @Bean 51 | public Integer randomPort() { 52 | return TestSocketUtils.findAvailableTcpPort(); 53 | } 54 | 55 | @Bean 56 | public Consumer embeddedPostgresCustomizer(Integer randomPort) { 57 | return builder -> builder.setPort(randomPort); 58 | } 59 | } 60 | 61 | @Autowired 62 | private Integer randomPort; 63 | 64 | @Autowired 65 | private DataSource dataSource; 66 | 67 | @Test 68 | public void testDataSource() throws SQLException { 69 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPortNumber()).isEqualTo(randomPort); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/YandexProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import io.zonky.test.support.ConditionalTestRule; 22 | import io.zonky.test.support.TestAssumptions; 23 | import org.junit.ClassRule; 24 | import org.junit.Test; 25 | import org.junit.experimental.categories.Category; 26 | import org.junit.runner.RunWith; 27 | import org.postgresql.ds.PGSimpleDataSource; 28 | import org.springframework.beans.factory.annotation.Autowired; 29 | import org.springframework.test.context.ContextConfiguration; 30 | import org.springframework.test.context.junit4.SpringRunner; 31 | 32 | import javax.sql.DataSource; 33 | import java.sql.SQLException; 34 | 35 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.YANDEX; 36 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 37 | import static org.assertj.core.api.Assertions.assertThat; 38 | 39 | @RunWith(SpringRunner.class) 40 | @Category(PostgresTestSuite.class) 41 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = YANDEX) 42 | @ContextConfiguration 43 | public class YandexProviderIntegrationTest { 44 | 45 | @ClassRule 46 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeYandexSupportsCurrentPostgresVersion); 47 | 48 | @Autowired 49 | private DataSource dataSource; 50 | 51 | @Test 52 | public void testDataSource() throws SQLException { 53 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("yandex"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/YandexProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import io.zonky.test.support.ConditionalTestRule; 22 | import io.zonky.test.support.TestAssumptions; 23 | import org.junit.ClassRule; 24 | import org.junit.Test; 25 | import org.junit.experimental.categories.Category; 26 | import org.junit.runner.RunWith; 27 | import org.postgresql.ds.PGSimpleDataSource; 28 | import org.springframework.beans.factory.annotation.Autowired; 29 | import org.springframework.jdbc.core.JdbcTemplate; 30 | import org.springframework.test.context.ContextConfiguration; 31 | import org.springframework.test.context.TestPropertySource; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import javax.sql.DataSource; 35 | import java.sql.SQLException; 36 | 37 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.YANDEX; 38 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 39 | import static org.assertj.core.api.Assertions.assertThat; 40 | 41 | @RunWith(SpringRunner.class) 42 | @Category(PostgresTestSuite.class) 43 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = YANDEX) 44 | @TestPropertySource(properties = { 45 | "zonky.test.database.postgres.yandex-provider.postgres-version=9.6.11-1" 46 | }) 47 | @ContextConfiguration 48 | public class YandexProviderWithConfigurationIntegrationTest { 49 | 50 | @ClassRule 51 | public static ConditionalTestRule conditionalTestRule = new ConditionalTestRule(TestAssumptions::assumeYandexSupportsCurrentPostgresVersion); 52 | 53 | @Autowired 54 | private DataSource dataSource; 55 | 56 | @Test 57 | public void testDataSource() throws SQLException { 58 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPassword()).isEqualTo("yandex"); 59 | 60 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 61 | String version = jdbcTemplate.queryForObject("show server_version", String.class); 62 | assertThat(version).startsWith("9.6."); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/ZonkyProviderIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import org.junit.Test; 22 | import org.junit.experimental.categories.Category; 23 | import org.junit.runner.RunWith; 24 | import org.postgresql.ds.PGSimpleDataSource; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.test.context.ContextConfiguration; 27 | import org.springframework.test.context.junit4.SpringRunner; 28 | 29 | import javax.sql.DataSource; 30 | import java.sql.SQLException; 31 | 32 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY; 33 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 34 | import static org.assertj.core.api.Assertions.assertThat; 35 | 36 | @RunWith(SpringRunner.class) 37 | @Category(PostgresTestSuite.class) 38 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = ZONKY) 39 | @ContextConfiguration 40 | public class ZonkyProviderIntegrationTest { 41 | 42 | @Autowired 43 | private DataSource dataSource; 44 | 45 | @Test 46 | public void testDataSource() throws SQLException { 47 | assertThat(dataSource.unwrap(PGSimpleDataSource.class)).isNotNull(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/ZonkyProviderWithConfigurationIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.db.provider; 18 | 19 | import io.zonky.test.category.PostgresTestSuite; 20 | import io.zonky.test.db.AutoConfigureEmbeddedDatabase; 21 | import io.zonky.test.db.postgres.embedded.EmbeddedPostgres; 22 | import io.zonky.test.support.TestSocketUtils; 23 | import org.junit.Test; 24 | import org.junit.experimental.categories.Category; 25 | import org.junit.runner.RunWith; 26 | import org.postgresql.ds.PGSimpleDataSource; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.test.context.ContextConfiguration; 31 | import org.springframework.test.context.junit4.SpringRunner; 32 | 33 | import javax.sql.DataSource; 34 | import java.sql.SQLException; 35 | import java.util.function.Consumer; 36 | 37 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY; 38 | import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES; 39 | import static org.assertj.core.api.Assertions.assertThat; 40 | 41 | @RunWith(SpringRunner.class) 42 | @Category(PostgresTestSuite.class) 43 | @AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = ZONKY) 44 | @ContextConfiguration 45 | public class ZonkyProviderWithConfigurationIntegrationTest { 46 | 47 | @Configuration 48 | static class Config { 49 | 50 | @Bean 51 | public Integer randomPort() { 52 | return TestSocketUtils.findAvailableTcpPort(); 53 | } 54 | 55 | @Bean 56 | public Consumer embeddedPostgresCustomizer(Integer randomPort) { 57 | return builder -> builder.setPort(randomPort); 58 | } 59 | } 60 | 61 | @Autowired 62 | private Integer randomPort; 63 | 64 | @Autowired 65 | private DataSource dataSource; 66 | 67 | @Test 68 | public void testDataSource() throws SQLException { 69 | assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPortNumber()).isEqualTo(randomPort); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/db/support/TestDatabasePreparer.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.db.support; 2 | 3 | import io.zonky.test.db.preparer.DatabasePreparer; 4 | 5 | import javax.sql.DataSource; 6 | import java.util.Objects; 7 | import java.util.function.Consumer; 8 | 9 | public class TestDatabasePreparer implements DatabasePreparer { 10 | 11 | public static TestDatabasePreparer empty() { 12 | return new TestDatabasePreparer(null, dataSource -> {}); 13 | } 14 | 15 | public static TestDatabasePreparer empty(String identifier) { 16 | return new TestDatabasePreparer(identifier, dataSource -> {}); 17 | } 18 | 19 | public static TestDatabasePreparer of(Consumer action) { 20 | return new TestDatabasePreparer(null, action); 21 | } 22 | 23 | public static TestDatabasePreparer of(String identifier, Consumer action) { 24 | return new TestDatabasePreparer(identifier, action); 25 | } 26 | 27 | public static TestDatabasePreparer of(String identifier, long duration, Consumer action) { 28 | return new TestDatabasePreparer(identifier, action, duration); 29 | } 30 | 31 | private final String identifier; 32 | private final Consumer action; 33 | private final long estimatedDuration; 34 | 35 | private TestDatabasePreparer(String identifier, Consumer action) { 36 | this.identifier = identifier; 37 | this.action = action; 38 | this.estimatedDuration = 0; 39 | } 40 | 41 | private TestDatabasePreparer(String identifier, Consumer action, long estimatedDuration) { 42 | this.identifier = identifier; 43 | this.action = action; 44 | this.estimatedDuration = estimatedDuration; 45 | } 46 | 47 | @Override 48 | public long estimatedDuration() { 49 | return estimatedDuration; 50 | } 51 | 52 | @Override 53 | public void prepare(DataSource dataSource) { 54 | action.accept(dataSource); 55 | } 56 | 57 | @Override 58 | public boolean equals(Object o) { 59 | if (this == o) return true; 60 | if (o == null || getClass() != o.getClass()) return false; 61 | TestDatabasePreparer that = (TestDatabasePreparer) o; 62 | if (identifier == null || that.identifier == null) return false; 63 | return Objects.equals(identifier, that.identifier); 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | return Objects.hash(identifier); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/support/ConditionalTestRule.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.support; 2 | 3 | import org.junit.rules.TestRule; 4 | import org.junit.runner.Description; 5 | import org.junit.runners.model.Statement; 6 | 7 | public class ConditionalTestRule implements TestRule { 8 | 9 | private final Runnable assumption; 10 | 11 | public ConditionalTestRule(Runnable assumption) { 12 | this.assumption = assumption; 13 | } 14 | 15 | @Override 16 | public Statement apply(Statement base, Description description) { 17 | return new Statement() { 18 | @Override 19 | public void evaluate() throws Throwable { 20 | assumption.run(); 21 | base.evaluate(); 22 | } 23 | }; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/support/MockitoAssertions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.support; 18 | 19 | import org.assertj.core.api.Condition; 20 | import org.mockito.internal.util.MockUtil; 21 | 22 | import static io.zonky.test.db.util.ReflectionUtils.invokeConstructor; 23 | import static io.zonky.test.db.util.ReflectionUtils.invokeMethod; 24 | import static io.zonky.test.db.util.ReflectionUtils.invokeStaticMethod; 25 | import static org.assertj.core.api.Assertions.allOf; 26 | 27 | public class MockitoAssertions { 28 | 29 | private MockitoAssertions() {} 30 | 31 | public static Condition mock() { 32 | return new Condition("target object should be a mock") { 33 | @Override 34 | public boolean matches(T object) { 35 | try { 36 | return invokeMethod(invokeConstructor(MockUtil.class), "isMock", object); 37 | } catch (Exception e) { 38 | return invokeStaticMethod(MockUtil.class, "isMock", object); 39 | } 40 | } 41 | }; 42 | } 43 | 44 | public static Condition mockWithName(String name) { 45 | Condition hasName = new Condition("mock should have a specified name") { 46 | @Override 47 | public boolean matches(T object) { 48 | try { 49 | return name.equals(invokeMethod(invokeConstructor(MockUtil.class), "getMockName", object).toString()); 50 | } catch (Exception e) { 51 | return name.equals(invokeStaticMethod(MockUtil.class, "getMockName", object).toString()); 52 | } 53 | } 54 | }; 55 | 56 | return allOf(mock(), hasName); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/support/SpyPostProcessor.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.support; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | 6 | import java.util.function.BiPredicate; 7 | 8 | import static org.mockito.Mockito.CALLS_REAL_METHODS; 9 | import static org.mockito.Mockito.mock; 10 | import static org.mockito.Mockito.withSettings; 11 | 12 | public class SpyPostProcessor implements BeanPostProcessor { 13 | 14 | private final BiPredicate predicate; 15 | 16 | public SpyPostProcessor(BiPredicate predicate) { 17 | this.predicate = predicate; 18 | } 19 | 20 | @Override 21 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 22 | return bean; 23 | } 24 | 25 | @Override 26 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 27 | if (predicate.test(bean, beanName)) { 28 | return mock(bean.getClass(), withSettings() 29 | .defaultAnswer(CALLS_REAL_METHODS) 30 | .spiedInstance(bean)); 31 | } 32 | return bean; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/support/TestAssumptions.java: -------------------------------------------------------------------------------- 1 | package io.zonky.test.support; 2 | 3 | import io.zonky.test.db.flyway.FlywayClassUtils; 4 | import org.springframework.beans.factory.support.AbstractBeanDefinition; 5 | import org.springframework.util.ClassUtils; 6 | 7 | import java.util.function.Supplier; 8 | 9 | import static org.junit.Assume.assumeTrue; 10 | 11 | public class TestAssumptions { 12 | 13 | private TestAssumptions() {} 14 | 15 | public static void assumeFlywaySupportsBaselineOperation() { 16 | assumeFlywayVersion("4"); 17 | } 18 | 19 | public static void assumeFlywaySupportsRepeatableMigrations() { 20 | assumeFlywayVersion("4"); 21 | } 22 | 23 | public static void assumeFlywaySupportsRepeatableAnnotations() { 24 | assumeFlywayVersion("4.2"); 25 | } 26 | 27 | private static void assumeFlywayVersion(String minVersion) { 28 | assumeTrue(FlywayClassUtils.getFlywayVersion().isGreaterThanOrEqualTo(minVersion)); 29 | } 30 | 31 | public static void assumeSpringSupportsInstanceSupplier() { 32 | assumeTrue(ClassUtils.hasMethod(AbstractBeanDefinition.class, "setInstanceSupplier", Supplier.class)); 33 | } 34 | 35 | public static void assumeSpringBootSupportsJdbcTestAnnotation() { 36 | assumeTrue(ClassUtils.isPresent("org.springframework.boot.test.autoconfigure.jdbc.JdbcTest", TestAssumptions.class.getClassLoader())); 37 | } 38 | 39 | public static void assumeLicenceAcceptance() { 40 | assumeTrue(TestAssumptions.class.getResource("/container-license-acceptance.txt") != null); 41 | } 42 | 43 | public static void assumeYandexSupportsCurrentPostgresVersion() { 44 | if (isLinux()) { 45 | String postgresVersion = System.getenv("ZONKY_TEST_DATABASE_POSTGRES_YANDEX-PROVIDER_POSTGRES-VERSION"); 46 | assumeTrue(postgresVersion != null); 47 | String majorVersion = postgresVersion.substring(0, postgresVersion.indexOf(".")); 48 | assumeTrue(Integer.parseInt(majorVersion) < 11); 49 | } 50 | } 51 | 52 | private static boolean isLinux() { 53 | String osName = System.getProperty("os.name"); 54 | if (osName == null) { 55 | return false; 56 | } 57 | return osName.startsWith("Linux") || osName.startsWith("LINUX"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/java/io/zonky/test/support/TestSocketUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.zonky.test.support; 18 | 19 | import org.springframework.util.Assert; 20 | 21 | import javax.net.ServerSocketFactory; 22 | import java.net.InetAddress; 23 | import java.net.ServerSocket; 24 | import java.util.Random; 25 | 26 | public class TestSocketUtils { 27 | 28 | private static final int PORT_RANGE_MIN = 1024; 29 | private static final int PORT_RANGE_MAX = 65535; 30 | private static final int PORT_RANGE_PLUS_ONE = PORT_RANGE_MAX - PORT_RANGE_MIN + 1; 31 | private static final int MAX_ATTEMPTS = 1_000; 32 | 33 | private static final Random random = new Random(System.nanoTime()); 34 | 35 | private TestSocketUtils() {} 36 | 37 | public static int findAvailableTcpPort() { 38 | int candidatePort; 39 | int searchCounter = 0; 40 | do { 41 | Assert.state(++searchCounter <= MAX_ATTEMPTS, () -> String.format( 42 | "Could not find an available TCP port in the range [%d, %d] after %d attempts", 43 | PORT_RANGE_MIN, PORT_RANGE_MAX, MAX_ATTEMPTS)); 44 | candidatePort = PORT_RANGE_MIN + random.nextInt(PORT_RANGE_PLUS_ONE); 45 | } 46 | while (!isPortAvailable(candidatePort)); 47 | 48 | return candidatePort; 49 | } 50 | 51 | private static boolean isPortAvailable(int port) { 52 | try { 53 | ServerSocket serverSocket = ServerSocketFactory.getDefault() 54 | .createServerSocket(port, 1, InetAddress.getByName("localhost")); 55 | serverSocket.close(); 56 | return true; 57 | } 58 | catch (Exception ex) { 59 | return false; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/changelog/db.changelog-master.yaml: -------------------------------------------------------------------------------- 1 | databaseChangeLog: 2 | - changeSet: 3 | id: 1 4 | author: tomix26 5 | changes: 6 | - sql: 7 | sql: create schema test 8 | - changeSet: 9 | id: 2 10 | author: tomix26 11 | changes: 12 | - createTable: 13 | schemaName: test 14 | tableName: person 15 | columns: 16 | - column: 17 | name: id 18 | type: bigint 19 | constraints: 20 | primaryKey: true 21 | nullable: false 22 | - column: 23 | name: first_name 24 | type: varchar(255) 25 | constraints: 26 | nullable: false 27 | - column: 28 | name: surname 29 | type: varchar(255) 30 | constraints: 31 | nullable: false 32 | - changeSet: 33 | id: 3 34 | author: tomix26 35 | changes: 36 | - insert: 37 | schemaName: test 38 | tableName: person 39 | columns: 40 | - column: 41 | name: id 42 | value: 1 43 | - column: 44 | name: first_name 45 | value: Dave 46 | - column: 47 | name: surname 48 | value: Syer 49 | - changeSet: 50 | id: 4 51 | author: tomix26 52 | changes: 53 | - renameColumn: 54 | schemaName: test 55 | tableName: person 56 | oldColumnName: surname 57 | newColumnName: last_name -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/create_address_table.sql: -------------------------------------------------------------------------------- 1 | create schema test; 2 | 3 | create table test.address ( 4 | id bigint primary key not null, 5 | street varchar(255) not null 6 | ); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/migration/R__people_view.sql: -------------------------------------------------------------------------------- 1 | create or replace view test.people as select id, first_name from test.person; -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/migration/V0001_1__create_person_table.sql: -------------------------------------------------------------------------------- 1 | create table test.person ( 2 | id bigint primary key not null, 3 | first_name varchar(255) not null, 4 | surname varchar(255) not null 5 | ); 6 | 7 | insert into test.person (id, first_name, surname) values (1, 'Dave', 'Syer'); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/migration/V0002_1__rename_surname_column.sql: -------------------------------------------------------------------------------- 1 | alter table test.person rename column surname to last_name; -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/migration/afterMigrate.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zonkyio/embedded-database-spring-test/8d24e1414d8c31551f241307f770a6676f20cf59/embedded-database-spring-test/src/test/resources/db/migration/afterMigrate.sql -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/migration/beforeMigrate.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zonkyio/embedded-database-spring-test/8d24e1414d8c31551f241307f770a6676f20cf59/embedded-database-spring-test/src/test/resources/db/migration/beforeMigrate.sql -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/next_migration/V0001_1__create_person_table.sql: -------------------------------------------------------------------------------- 1 | create table next.person ( 2 | id bigint primary key not null, 3 | first_name varchar(255) not null, 4 | surname varchar(255) not null 5 | ); 6 | 7 | insert into next.person (id, first_name, surname) values (1, 'Dave', 'Syer'); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/test_migration/appendable/V1000_1__create_test_data.sql: -------------------------------------------------------------------------------- 1 | insert into test.person (id, first_name, last_name) values (2, 'Tom', 'Hanks'); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/test_migration/dependent/V0001_2__add_full_name_column.sql: -------------------------------------------------------------------------------- 1 | alter table test.person add column full_name varchar(255); 2 | 3 | update test.person set full_name = first_name||' '||surname -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/test_migration/dependent/V0999_1__create_test_data.sql: -------------------------------------------------------------------------------- 1 | insert into test.person (id, first_name, last_name, full_name) values (3, 'Will', 'Smith', 'Will Smith'); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/test_migration/separated/V1000_1__create_test_person_table.sql: -------------------------------------------------------------------------------- 1 | create table test.person ( 2 | id bigint primary key not null, 3 | first_name varchar(255) not null, 4 | last_name varchar(255) not null 5 | ); 6 | 7 | insert into test.person (id, first_name, last_name) values (1, 'Tom', 'Hanks'); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/db/test_migration/slow/V1000_1__sleep.sql: -------------------------------------------------------------------------------- 1 | select pg_sleep(1); -------------------------------------------------------------------------------- /embedded-database-spring-test/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=2.7.0 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zonkyio/embedded-database-spring-test/8d24e1414d8c31551f241307f770a6676f20cf59/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Apr 23 17:20:37 CEST 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'embedded-database-spring-test-parent' 2 | 3 | include 'embedded-database-spring-test' 4 | include 'embedded-database-spring-test-autoconfigure' 5 | -------------------------------------------------------------------------------- /zonky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zonkyio/embedded-database-spring-test/8d24e1414d8c31551f241307f770a6676f20cf59/zonky.jpg --------------------------------------------------------------------------------