Skip to content

Commit aa44f72

Browse files
Added an option to specify a directory for storing downloaded PostgreSQL distributions [#1]
1 parent 17aee9f commit aa44f72

File tree

10 files changed

+68
-48
lines changed

10 files changed

+68
-48
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ These are all possible configuration options and its default values:
3939

4040
```groovy
4141
postgresEmbedded {
42-
version = "V9_6_2"
42+
version = "V9_6_3"
4343
host = "localhost"
4444
port = 0 // zero value means a random port
4545
dbName = "embedded"
4646
username = "username"
4747
password = "password"
48+
artifactStorePath = "~/.embedpostgresql" // where PostgreSQL distributions are stored after downloading, inside home directory by default
4849
stopWhenBuildFinished = true
4950
timeoutMillisBeforeStop = 0
5051
}
5152
```
5253

53-
You can specify any supported version from [postgresql-embedded](https://github.com/yandex-qatools/postgresql-embedded/blob/postgresql-embedded-1.23/src/main/java/ru/yandex/qatools/embed/postgresql/distribution/Version.java), and a custom version of PostgreSQL as well.
54+
You can specify any supported version from [postgresql-embedded](https://github.com/yandex-qatools/postgresql-embedded/blob/postgresql-embedded-2.4/src/main/java/ru/yandex/qatools/embed/postgresql/distribution/Version.java), and a custom version of PostgreSQL as well.

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ apply plugin: 'maven'
77

88
group = 'com.github.honourednihilist'
99

10-
sourceCompatibility = 1.8
11-
targetCompatibility = 1.8
10+
sourceCompatibility = JavaVersion.VERSION_1_8
11+
targetCompatibility = JavaVersion.VERSION_1_8
1212

1313
compileJava {
1414
options.encoding = 'UTF-8'
@@ -19,8 +19,8 @@ compileTestJava {
1919
}
2020

2121
dependencies {
22-
compileOnly(group: 'org.projectlombok', name: 'lombok', version: '1.16.16')
23-
compile(group: 'ru.yandex.qatools.embed', name: 'postgresql-embedded', version: '1.23')
22+
compileOnly(group: 'org.projectlombok', name: 'lombok', version: '1.16.18')
23+
compile(group: 'ru.yandex.qatools.embed', name: 'postgresql-embedded', version: '2.4')
2424

2525
testCompile(group: 'junit', name: 'junit', version: '4.12')
2626
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.1.1-SNAPSHOT
1+
version=0.2.0

gradle/wrapper/gradle-wrapper.jar

-75 Bytes
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

gradlew

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
3333
# Use the maximum available, or set MAX_FD != -1 to use that value.
3434
MAX_FD="maximum"
3535

36-
warn ( ) {
36+
warn () {
3737
echo "$*"
3838
}
3939

40-
die ( ) {
40+
die () {
4141
echo
4242
echo "$*"
4343
echo
@@ -155,7 +155,7 @@ if $cygwin ; then
155155
fi
156156

157157
# Escape application args
158-
save ( ) {
158+
save () {
159159
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160160
echo " "
161161
}

src/main/java/com/github/honourednihilist/gradle/postgresql/embedded/EmbeddedPostgres.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
package com.github.honourednihilist.gradle.postgresql.embedded;
22

3+
import de.flapdoodle.embed.process.config.IRuntimeConfig;
4+
import de.flapdoodle.embed.process.config.io.ProcessOutput;
5+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
36
import de.flapdoodle.embed.process.distribution.IVersion;
7+
import de.flapdoodle.embed.process.io.Slf4jLevel;
8+
import de.flapdoodle.embed.process.io.Slf4jStreamProcessor;
9+
import de.flapdoodle.embed.process.io.directories.FixedPath;
410
import de.flapdoodle.embed.process.runtime.Network;
11+
import de.flapdoodle.embed.process.store.ArtifactStoreBuilder;
12+
import de.flapdoodle.embed.process.store.PostgresArtifactStoreBuilder;
513

614
import java.io.IOException;
15+
import java.util.HashSet;
716

17+
import ru.yandex.qatools.embed.postgresql.Command;
18+
import ru.yandex.qatools.embed.postgresql.PostgresExecutable;
819
import ru.yandex.qatools.embed.postgresql.PostgresProcess;
920
import ru.yandex.qatools.embed.postgresql.PostgresStarter;
1021
import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig;
1122
import ru.yandex.qatools.embed.postgresql.config.PostgresConfig;
23+
import ru.yandex.qatools.embed.postgresql.config.PostgresDownloadConfigBuilder;
24+
import ru.yandex.qatools.embed.postgresql.config.RuntimeConfigBuilder;
25+
import ru.yandex.qatools.embed.postgresql.ext.LogWatchStreamProcessor;
26+
27+
import static java.util.Collections.singletonList;
28+
import static org.slf4j.LoggerFactory.getLogger;
1229

1330
public class EmbeddedPostgres {
1431

@@ -18,30 +35,55 @@ public class EmbeddedPostgres {
1835
private final String dbName;
1936
private final String username;
2037
private final String password;
38+
private final String artifactStorePath;
2139

2240
private PostgresProcess process;
2341
private String jdbcUrl;
2442

25-
public EmbeddedPostgres(IVersion version, String host, int port, String dbName, String username, String password) {
43+
public EmbeddedPostgres(IVersion version, String host, int port, String dbName, String username, String password, String artifactStorePath) {
2644
this.version = version;
2745
this.host = host;
2846
this.port = port;
2947
this.dbName = dbName;
3048
this.username = username;
3149
this.password = password;
50+
this.artifactStorePath = artifactStorePath;
3251
}
3352

3453
public synchronized void start() throws IOException {
3554
if (process != null) {
3655
throw new IllegalStateException();
3756
}
3857

58+
Command command = Command.Postgres;
59+
60+
IDownloadConfig downloadConfig = new PostgresDownloadConfigBuilder()
61+
.defaultsForCommand(command)
62+
.artifactStorePath(new FixedPath(artifactStorePath))
63+
.build();
64+
65+
ArtifactStoreBuilder artifactStoreBuilder = new PostgresArtifactStoreBuilder()
66+
.defaults(command)
67+
.download(downloadConfig);
68+
69+
LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor("started",
70+
new HashSet<>(singletonList("failed")),
71+
new Slf4jStreamProcessor(getLogger("postgres"), Slf4jLevel.TRACE));
72+
73+
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
74+
.defaults(command)
75+
.processOutput(new ProcessOutput(logWatch, logWatch, logWatch))
76+
.artifactStore(artifactStoreBuilder)
77+
.build();
78+
79+
PostgresStarter<PostgresExecutable, PostgresProcess> starter = new PostgresStarter<>(PostgresExecutable.class, runtimeConfig);
80+
3981
PostgresConfig config = new PostgresConfig(version,
4082
new AbstractPostgresConfig.Net(host, port == 0 ? Network.getFreeServerPort() : port),
4183
new AbstractPostgresConfig.Storage(dbName),
4284
new AbstractPostgresConfig.Timeout(),
4385
new AbstractPostgresConfig.Credentials(username, password));
44-
process = PostgresStarter.getDefaultInstance().prepare(config).start();
86+
process = starter.prepare(config).start();
4587
jdbcUrl = "jdbc:postgresql://" + config.net().host() + ":" + config.net().port() + "/" + config.storage().dbName();
4688
}
4789

src/main/java/com/github/honourednihilist/gradle/postgresql/embedded/GradlePostgresqlEmbeddedPlugin.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
import java.util.Map;
1010

1111
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.PRODUCTION;
12-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_3;
13-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_4;
1412
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_5;
1513
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;
16-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_3_15;
17-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_4_10;
18-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_5_5;
19-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_6_2;
14+
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_6_3;
2015

2116
public class GradlePostgresqlEmbeddedPlugin implements Plugin<Project> {
2217

@@ -32,7 +27,8 @@ public void apply(Project project) {
3227
extension.getPort(),
3328
extension.getDbName(),
3429
extension.getUsername(),
35-
extension.getPassword());
30+
extension.getPassword(),
31+
extension.getArtifactStorePath());
3632
startTask.setPostgres(postgres);
3733

3834
if (extension.isStopWhenBuildFinished()) {
@@ -46,16 +42,8 @@ IVersion parseVersion(String version) {
4642
versions.put(PRODUCTION.name(), PRODUCTION);
4743
versions.put(V9_6.name(), V9_6);
4844
versions.put(V9_5.name(), V9_5);
49-
versions.put(V9_4.name(), V9_4);
50-
versions.put(V9_3.name(), V9_3);
51-
versions.put(V9_6_2.name(), V9_6_2);
52-
versions.put(V9_5_5.name(), V9_5_5);
53-
versions.put(V9_4_10.name(), V9_4_10);
54-
versions.put(V9_3_15.name(), V9_3_15);
55-
versions.put(V9_6_2.asInDownloadPath(), V9_6_2);
56-
versions.put(V9_5_5.asInDownloadPath(), V9_5_5);
57-
versions.put(V9_4_10.asInDownloadPath(), V9_4_10);
58-
versions.put(V9_3_15.asInDownloadPath(), V9_3_15);
45+
versions.put(V9_6_3.name(), V9_6_3);
46+
versions.put(V9_6_3.asInDownloadPath(), V9_6_3);
5947
return versions.getOrDefault(version, () -> version);
6048
}
6149
}

src/main/java/com/github/honourednihilist/gradle/postgresql/embedded/PostgresqlEmbeddedExtension.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.honourednihilist.gradle.postgresql.embedded;
22

3+
import java.io.File;
4+
35
import lombok.Data;
46

57
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.PRODUCTION;
@@ -15,6 +17,7 @@ public class PostgresqlEmbeddedExtension {
1517
private String dbName = "embedded";
1618
private String username = "username";
1719
private String password = "password";
20+
private String artifactStorePath = System.getProperty("user.home") + File.separatorChar + ".embedpostgresql";
1821

1922
private boolean stopWhenBuildFinished = true;
2023
private int timeoutMillisBeforeStop = 0;

src/test/java/com/github/honourednihilist/gradle/postgresql/embedded/GradlePostgresqlEmbeddedPluginTest.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
import static org.hamcrest.CoreMatchers.is;
66
import static org.junit.Assert.assertThat;
77
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.PRODUCTION;
8-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_3;
9-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_4;
108
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_5;
119
import static ru.yandex.qatools.embed.postgresql.distribution.Version.Main.V9_6;
12-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_3_15;
13-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_4_10;
14-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_5_5;
15-
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_6_2;
10+
import static ru.yandex.qatools.embed.postgresql.distribution.Version.V9_6_3;
1611

1712
public class GradlePostgresqlEmbeddedPluginTest {
1813

@@ -23,18 +18,9 @@ public void testParseVersion() {
2318
assertThat(plugin.parseVersion(PRODUCTION.name()), is(PRODUCTION));
2419
assertThat(plugin.parseVersion(V9_6.name()), is(V9_6));
2520
assertThat(plugin.parseVersion(V9_5.name()), is(V9_5));
26-
assertThat(plugin.parseVersion(V9_4.name()), is(V9_4));
27-
assertThat(plugin.parseVersion(V9_3.name()), is(V9_3));
28-
29-
assertThat(plugin.parseVersion(V9_6_2.name()), is(V9_6_2));
30-
assertThat(plugin.parseVersion(V9_5_5.name()), is(V9_5_5));
31-
assertThat(plugin.parseVersion(V9_4_10.name()), is(V9_4_10));
32-
assertThat(plugin.parseVersion(V9_3_15.name()), is(V9_3_15));
33-
34-
assertThat(plugin.parseVersion(V9_6_2.asInDownloadPath()), is(V9_6_2));
35-
assertThat(plugin.parseVersion(V9_5_5.asInDownloadPath()), is(V9_5_5));
36-
assertThat(plugin.parseVersion(V9_4_10.asInDownloadPath()), is(V9_4_10));
37-
assertThat(plugin.parseVersion(V9_3_15.asInDownloadPath()), is(V9_3_15));
21+
22+
assertThat(plugin.parseVersion(V9_6_3.name()), is(V9_6_3));
23+
assertThat(plugin.parseVersion(V9_6_3.asInDownloadPath()), is(V9_6_3));
3824

3925
assertThat(plugin.parseVersion("any version").asInDownloadPath(), is("any version"));
4026
}

0 commit comments

Comments
 (0)