Skip to content

Commit

Permalink
#630 switch to otj-pg-embedded: at the unit test level
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Apr 27, 2019
1 parent 43a315d commit 8444513
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
14 changes: 8 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import java.time.format.DateTimeFormatter
buildscript {

dependencies {
classpath 'ru.yandex.qatools.embed:postgresql-embedded:2.9'
//classpath 'ru.yandex.qatools.embed:postgresql-embedded:2.9'
classpath 'com.opentable.components:otj-pg-embedded:0.13.1'
classpath 'org.postgresql:postgresql:42.2.5'
classpath "org.springframework:spring-jdbc:$springVersion"
}
Expand Down Expand Up @@ -141,7 +142,8 @@ dependencies {
compile 'org.imgscalr:imgscalr-lib:4.2'
compile 'org.aspectj:aspectjweaver:1.9.2'

testCompile "ru.yandex.qatools.embed:postgresql-embedded:2.9" //we leave it to the v2.9 for now, since some user had issues on Windows
//testCompile "ru.yandex.qatools.embed:postgresql-embedded:2.9" //we leave it to the v2.9 for now, since some user had issues on Windows
testCompile 'com.opentable.components:otj-pg-embedded:0.13.1'

compileOnly "javax.servlet:javax.servlet-api:4.0.1"
testCompile "javax.servlet:javax.servlet-api:4.0.1"
Expand Down Expand Up @@ -308,11 +310,11 @@ task clever(type: Copy) {
dependsOn build
}

import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres
import ru.yandex.qatools.embed.postgresql.distribution.Version.Main
/*import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres
import ru.yandex.qatools.embed.postgresql.distribution.Version.Main*/
import org.postgresql.ds.PGSimpleDataSource

task startEmbeddedPgSQL {
/*task startEmbeddedPgSQL {
doLast {
final pgsqlPath = Paths.get(".", "alfio-itest")
Files.createDirectories(pgsqlPath)
Expand Down Expand Up @@ -350,7 +352,7 @@ task stopEmbeddedPgSQL {
Runtime.runtime.exec("kill -9 " + pid)
System.out.println("Killed pgsql with pid " + pid)
}
}
}*/

release {
buildTasks = ['distribution']
Expand Down
31 changes: 14 additions & 17 deletions src/test/java/alfio/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import alfio.manager.FileDownloadManager;
import alfio.test.util.IntegrationTestUtil;
import alfio.util.BaseIntegrationTest;
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ByteArrayResource;
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;


import javax.annotation.PreDestroy;
import javax.sql.DataSource;
Expand All @@ -41,17 +41,21 @@
import java.time.ZonedDateTime;
import java.util.Properties;

import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.PRODUCTION;


@Configuration
public class TestConfiguration {

private EmbeddedPostgres postgres;

private final String POSTGRES_USERNAME = "postgres";
private final String POSTGRES_PASSWORD = "postgres";
private final String POSTGRES_DB = "postgres";

@Bean
@Profile("!travis")
public PlatformProvider getCloudProvider(EmbeddedPostgres postgres) {
IntegrationTestUtil.generateDBConfig(postgres.getConnectionUrl().orElseThrow(IllegalArgumentException::new), EmbeddedPostgres.DEFAULT_USER, EmbeddedPostgres.DEFAULT_PASSWORD)
IntegrationTestUtil.generateDBConfig(postgres.getJdbcUrl(POSTGRES_USERNAME, POSTGRES_DB), POSTGRES_USERNAME, POSTGRES_PASSWORD)
.forEach(System::setProperty);
return PlatformProvider.DEFAULT;
}
Expand All @@ -60,9 +64,9 @@ public PlatformProvider getCloudProvider(EmbeddedPostgres postgres) {
@Profile("!travis")
public DataSource getDataSource(EmbeddedPostgres postgres) {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(postgres.getConnectionUrl().orElseThrow(IllegalArgumentException::new));
dataSource.setUsername(EmbeddedPostgres.DEFAULT_USER);
dataSource.setPassword(EmbeddedPostgres.DEFAULT_PASSWORD);
dataSource.setJdbcUrl(postgres.getJdbcUrl(POSTGRES_USERNAME, POSTGRES_DB));
dataSource.setUsername(POSTGRES_USERNAME);
dataSource.setPassword(POSTGRES_PASSWORD);
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setMaximumPoolSize(5);
return dataSource;
Expand All @@ -74,21 +78,14 @@ public EmbeddedPostgres postgres() throws IOException {
Path pgsqlPath = Paths.get(".", "alfio-itest");
Files.createDirectories(pgsqlPath);
Path tmpDataDir = Files.createTempDirectory(pgsqlPath, "alfio-data");
postgres = new EmbeddedPostgres(PRODUCTION, tmpDataDir.normalize().toAbsolutePath().toString());
postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
FileUtils.deleteDirectory(tmpDataDir.normalize().toAbsolutePath().toFile());
} catch (IOException e) {
}
}));
postgres = EmbeddedPostgres.builder().setDataDirectory(tmpDataDir).start();
return postgres;
}

@PreDestroy
public void shutdown() {
public void shutdown() throws IOException {
if (postgres != null) {
postgres.stop();
postgres.close();
}
}

Expand Down

0 comments on commit 8444513

Please sign in to comment.