From ee7ef496b84ed30ae959530e7ca7ea7ce781934f Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Thu, 26 Apr 2018 05:23:10 +0200 Subject: [PATCH] Make EmbeddedPostgres implements AutoCloseable (#131) This allows to use it in a try-with-resources statement, in order to have it automatically closed/stopped. --- README.md | 4 ++++ .../yandex/qatools/embed/postgresql/EmbeddedPostgres.java | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3da0f48..3586387 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ conn.close(); postgres.stop(); ``` +Note that EmbeddedPostgres implements [java.lang.AutoCloseable](https://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html), +which means that you can use it with a [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) +statement (in Java >= 7) to have it automatically stopped. + ### How to avoid archive extraction on every run You can specify the cached artifact store to avoid archives downloading and extraction (in case if a directory remains on every run). diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java b/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java index cee57ad..6c36efa 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java @@ -26,7 +26,7 @@ /** * Helper class simplifying the start up configuration for embedded postgres */ -public class EmbeddedPostgres { +public class EmbeddedPostgres implements AutoCloseable { public static final String DEFAULT_USER = "postgres";//NOSONAR public static final String DEFAULT_PASSWORD = "postgres";//NOSONAR public static final String DEFAULT_DB_NAME = "postgres";//NOSONAR @@ -215,4 +215,9 @@ private String formatConnUrl(PostgresConfig config) { public void stop() { getProcess().orElseThrow(() -> new IllegalStateException("Cannot stop not started instance!")).stop(); } + + @Override + public void close() { + this.stop(); + } }