|
48 | 48 | import java.util.Map; |
49 | 49 | import java.util.Map.Entry; |
50 | 50 | import java.util.Objects; |
| 51 | +import java.util.Optional; |
51 | 52 | import java.util.UUID; |
52 | 53 | import java.util.concurrent.Phaser; |
53 | 54 | import java.util.concurrent.TimeUnit; |
@@ -111,19 +112,22 @@ public class EmbeddedPostgres implements Closeable |
111 | 112 | Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig, |
112 | 113 | PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, ProcessBuilder.Redirect outputRedirector) throws IOException |
113 | 114 | { |
114 | | - this(parentDirectory, dataDirectory, cleanDataDirectory, postgresConfig, localeConfig, port, connectConfig, pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT); |
| 115 | + this(parentDirectory, dataDirectory, cleanDataDirectory, postgresConfig, localeConfig, port, connectConfig, |
| 116 | + pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT, null); |
115 | 117 | } |
116 | 118 |
|
117 | 119 | EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory, |
118 | 120 | Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig, |
119 | | - PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, ProcessBuilder.Redirect outputRedirector, Duration pgStartupWait) throws IOException |
| 121 | + PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, |
| 122 | + ProcessBuilder.Redirect outputRedirector, Duration pgStartupWait, |
| 123 | + File overrideWorkingDirectory) throws IOException |
120 | 124 | { |
121 | 125 | this.cleanDataDirectory = cleanDataDirectory; |
122 | 126 | this.postgresConfig = new HashMap<>(postgresConfig); |
123 | 127 | this.localeConfig = new HashMap<>(localeConfig); |
124 | 128 | this.connectConfig = new HashMap<>(connectConfig); |
125 | 129 | this.port = port; |
126 | | - this.pgDir = prepareBinaries(pgBinaryResolver); |
| 130 | + this.pgDir = prepareBinaries(pgBinaryResolver, overrideWorkingDirectory); |
127 | 131 | this.errorRedirector = errorRedirector; |
128 | 132 | this.outputRedirector = outputRedirector; |
129 | 133 | this.pgStartupWait = pgStartupWait; |
@@ -475,6 +479,7 @@ public static EmbeddedPostgres.Builder builder() |
475 | 479 | public static class Builder |
476 | 480 | { |
477 | 481 | private final File parentDirectory = getWorkingDirectory(); |
| 482 | + private File overrideWorkingDirectory; |
478 | 483 | private File builderDataDirectory; |
479 | 484 | private final Map<String, String> config = new HashMap<>(); |
480 | 485 | private final Map<String, String> localeConfig = new HashMap<>(); |
@@ -535,6 +540,11 @@ public Builder setConnectConfig(String key, String value) { |
535 | 540 | return this; |
536 | 541 | } |
537 | 542 |
|
| 543 | + public Builder setOverrideWorkingDirectory(File workingDirectory) { |
| 544 | + overrideWorkingDirectory = workingDirectory; |
| 545 | + return this; |
| 546 | + } |
| 547 | + |
538 | 548 | public Builder setPort(int port) { |
539 | 549 | builderPort = port; |
540 | 550 | return this; |
@@ -564,7 +574,9 @@ public EmbeddedPostgres start() throws IOException { |
564 | 574 | if (builderDataDirectory == null) { |
565 | 575 | builderDataDirectory = Files.createTempDirectory("epg").toFile(); |
566 | 576 | } |
567 | | - return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config, localeConfig, builderPort, connectConfig, pgBinaryResolver, errRedirector, outRedirector, pgStartupWait); |
| 577 | + return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config, |
| 578 | + localeConfig, builderPort, connectConfig, pgBinaryResolver, errRedirector, outRedirector, |
| 579 | + pgStartupWait, overrideWorkingDirectory); |
568 | 580 | } |
569 | 581 |
|
570 | 582 | @Override |
@@ -730,7 +742,7 @@ private void closeChannel(Channel channel) { |
730 | 742 | } |
731 | 743 | } |
732 | 744 |
|
733 | | - private static File prepareBinaries(PgBinaryResolver pgBinaryResolver) |
| 745 | + private static File prepareBinaries(PgBinaryResolver pgBinaryResolver, File overrideWorkingDirectory) |
734 | 746 | { |
735 | 747 | PREPARE_BINARIES_LOCK.lock(); |
736 | 748 | try { |
@@ -760,7 +772,8 @@ private static File prepareBinaries(PgBinaryResolver pgBinaryResolver) |
760 | 772 | pgArchiveData.close(); |
761 | 773 |
|
762 | 774 | String pgDigest = Hex.encodeHexString(pgArchiveData.getMessageDigest().digest()); |
763 | | - pgDir = new File(getWorkingDirectory(), String.format("PG-%s", pgDigest)); |
| 775 | + File workingDirectory = Optional.ofNullable(overrideWorkingDirectory).orElse(getWorkingDirectory()); |
| 776 | + pgDir = new File(workingDirectory, String.format("PG-%s", pgDigest)); |
764 | 777 |
|
765 | 778 | mkdirs(pgDir); |
766 | 779 | final File unpackLockFile = new File(pgDir, LOCK_FILE_NAME); |
|
0 commit comments