Class EmbeddedPostgres.Builder
java.lang.Object
de.softwareforge.testing.postgres.embedded.EmbeddedPostgres.Builder
- Enclosing class:
- EmbeddedPostgres
Creates a new
EmbeddedPostgres
instance and starts it.-
Method Summary
Modifier and TypeMethodDescriptionaddConnectionProperty
(String key, String value) Adds a connection property.addInitDbConfiguration
(String key, String value) Adds a configuration parameters for theinitdb
command.addLocaleConfiguration
(String key, String value) Deprecated.addServerConfiguration
(String key, String value) Adds a server configuration parameter.build()
Creates and boots a newEmbeddedPostgres
instance.setDataDirectory
(File dataDirectory) Explicitly set the location of the data directory.setDataDirectory
(String dataDirectory) Explicitly set the location of the data directory.setDataDirectory
(Path dataDirectory) Explicitly set the location of the data directory.setErrorRedirector
(ProcessBuilder.Redirect errRedirector) Set aProcessBuilder.Redirect
instance to receive stderr output from the spawned processes.setInstallationBaseDirectory
(File installationBaseDirectory) Sets the directory where the PostgreSQL distribution is unpacked.setNativeBinaryManager
(NativeBinaryManager nativeBinaryManager) Sets theNativeBinaryManager
that provides the location of the postgres installation.setOutputRedirector
(ProcessBuilder.Redirect outRedirector) Set aProcessBuilder.Redirect
instance to receive stdout output from the spawned processes.setPort
(int port) Explicitly set the TCP port for the PostgreSQL server.setRemoveDataOnShutdown
(boolean removeDataOnShutdown) Whether to remove the data directory on server shutdown.setServerStartupWait
(Duration serverStartupWait) Sets the time that the builder will wait for the PostgreSQL server instance to start.setServerVersion
(String serverVersion) Set the version of the PostgreSQL server.useLocalPostgresInstallation
(File directory) Use a locally installed PostgreSQL server for tests.Apply a set of defaults to the database server: timezone: UTC synchronous_commit: off max_connections: 300
-
Method Details
-
withDefaults
Apply a set of defaults to the database server:- timezone: UTC
- synchronous_commit: off
- max_connections: 300
- Returns:
- The builder itself.
-
setServerStartupWait
Sets the time that the builder will wait for the PostgreSQL server instance to start. Default is 10 seconds.- Parameters:
serverStartupWait
- Startup wait time. Must not be null or negative.- Returns:
- The builder itself.
-
setRemoveDataOnShutdown
Whether to remove the data directory on server shutdown. If true, the contents of the data directory are deleted when theEmbeddedPostgres
instance is closed. Default is true.- Parameters:
removeDataOnShutdown
- True removes the contents of the data directory on shutdown.- Returns:
- The builder itself.
-
setDataDirectory
Explicitly set the location of the data directory. Default is using a managed directory.- Parameters:
dataDirectory
- The directory to use. Must not be null. If it exists, the current user must be able to access the directory for reading and writing. If the directory does not exist then the current user must be able to create it for reading and writing.- Returns:
- The builder itself.
-
setDataDirectory
Explicitly set the location of the data directory. Default is using a managed directory.- Parameters:
dataDirectory
- The directory to use. Must not be null. If it exists, the current user must be able to access the directory for reading and writing. If the directory does not exist then the current user must be able to create it for reading and writing.- Returns:
- The builder itself.
-
setDataDirectory
Explicitly set the location of the data directory. Default is using a managed directory.- Parameters:
dataDirectory
- The directory to use. Must not be null. If it exists, the current user must be able to access the directory for reading and writing. If the directory does not exist then the current user must be able to create it for reading and writing.- Returns:
- The builder itself.
-
addServerConfiguration
@NonNull public EmbeddedPostgres.Builder addServerConfiguration(@NonNull String key, @NonNull String value) Adds a server configuration parameter. All parameters are passed to the PostgreSQL server a startup using thepostgres
command.Values and their function are specific to the PostgreSQL version selected.
See the PostgreSQL runtime configuration for more information.
- Parameters:
key
- Configuration parameter name. Must not be null.value
- Configuration parameter value. Must not be null.- Returns:
- The builder itself.
-
addLocaleConfiguration
@Deprecated @NonNull public EmbeddedPostgres.Builder addLocaleConfiguration(@NonNull String key, @NonNull String value) Deprecated. -
addInitDbConfiguration
@NonNull public EmbeddedPostgres.Builder addInitDbConfiguration(@NonNull String key, @NonNull String value) Adds a configuration parameters for theinitdb
command. Theinitdb
command is used to create the PostgreSQL server.Each value is added as a command line parameter to the command.
See the PostgreSQL initdb documentation for an overview of possible values.
- Parameters:
key
- initdb parameter name. Must not be null.value
- initdb parameter value. Must not be null. When the empty string is used as the value, the resulting command line parameter will not have a equal sign and a value assigned.- Returns:
- The builder itself.
-
addConnectionProperty
@NonNull public EmbeddedPostgres.Builder addConnectionProperty(@NonNull String key, @NonNull String value) Adds a connection property. These properties are set on every connection handed out by the data source. See the PostgreSQL JDBC driver documentation for possible values.- Parameters:
key
- connection property name. Must not be null.value
- connection property value. Must not be null.- Returns:
- The builder itself.
-
setInstallationBaseDirectory
@NonNull public EmbeddedPostgres.Builder setInstallationBaseDirectory(@NonNull File installationBaseDirectory) Sets the directory where the PostgreSQL distribution is unpacked. Setting the installation base directory resets theNativeBinaryManager
used to locate the postgres installation back to the default (which is to download the zonky.io Postgres archive and unpack it in the installation directory. The default is using a managed directory.- Parameters:
installationBaseDirectory
- The directory to unpack the postgres distribution. The current user must be able to create and write this directory. Must not be null.- Returns:
- The builder itself.
-
setPort
Explicitly set the TCP port for the PostgreSQL server. If the port is not available, starting the server will fail. Default is to find and use an available TCP port.- Parameters:
port
- The port to use. Must be > 1023 and < 65536.- Returns:
- The builder itself.
-
setServerVersion
Set the version of the PostgreSQL server. This value is passed to the default binary manager which will try to resolve this version from existing Maven artifacts. The value is ignored ifsetNativeBinaryManager(NativeBinaryManager)
is called.Not every PostgreSQL version is supported by pg-embedded. Some older versions lack the necessary options for the command line parameters and will fail at startup. Currently, every version 10 or newer should be working.
- Parameters:
serverVersion
- A partial or full version. Valid values are e.g. "12" or "11.3".
-
setErrorRedirector
@NonNull public EmbeddedPostgres.Builder setErrorRedirector(@NonNull ProcessBuilder.Redirect errRedirector) Set aProcessBuilder.Redirect
instance to receive stderr output from the spawned processes.- Parameters:
errRedirector
- aProcessBuilder.Redirect
instance. Must not be null.- Returns:
- The builder itself.
-
setOutputRedirector
@NonNull public EmbeddedPostgres.Builder setOutputRedirector(@NonNull ProcessBuilder.Redirect outRedirector) Set aProcessBuilder.Redirect
instance to receive stdout output from the spawned processes.- Parameters:
outRedirector
- aProcessBuilder.Redirect
instance. Must not be null.- Returns:
- The builder itself.
-
setNativeBinaryManager
@NonNull public EmbeddedPostgres.Builder setNativeBinaryManager(@NonNull NativeBinaryManager nativeBinaryManager) Sets theNativeBinaryManager
that provides the location of the postgres installation. Explicitly setting a binary manager overrides the installation base directory location set withsetInstallationBaseDirectory(File)
as this is only used by the default binary manager. CallingsetInstallationBaseDirectory(File)
after this method undoes setting the binary manager.- Parameters:
nativeBinaryManager
- ANativeBinaryManager
implementation. Must not be null.- Returns:
- The builder itself.
-
useLocalPostgresInstallation
Use a locally installed PostgreSQL server for tests. The tests will still spin up a new instance and locate the data in the data directory but it will use the locally installed binaries for starting and stopping. Calling this method sets a binary manager, so it overridessetNativeBinaryManager(NativeBinaryManager)
. Calling this method makes the builder ignore thesetInstallationBaseDirectory(File)
setting.- Parameters:
directory
- A local directory that contains a standard PostgreSQL installation. The directory must exist and read and executable.- Returns:
- The builder itself.
-
build
Creates and boots a newEmbeddedPostgres
instance.- Returns:
- A
EmbeddedPostgres
instance representing a started PostgreSQL server. - Throws:
IOException
- If the server could not be installed or started.
-
addInitDbConfiguration(String, String)
.