From c1af31f41dee7872eab009bdd739ad5dc819477b Mon Sep 17 00:00:00 2001 From: maciejrusek Date: Tue, 21 Apr 2026 21:48:49 +0200 Subject: [PATCH] Added database connection, added test and start with Testcontainers. --- .env.example | 3 ++ .gitignore | 3 +- README.md | 4 +-- docker-compose.yaml | 18 ++++++++++ pom.xml | 40 ++++++++++++++++++++++ src/main/java/util/DatabaseConnection.java | 33 ++++++++++++++++++ src/test/java/DatabaseConnectionTest.java | 37 ++++++++++++++++++++ 7 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 .env.example create mode 100644 docker-compose.yaml create mode 100644 src/main/java/util/DatabaseConnection.java create mode 100644 src/test/java/DatabaseConnectionTest.java diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f732a35 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +POSTGRES_PASSWORD="" +POSTGRES_USER="" +POSTGRES_DB="" diff --git a/.gitignore b/.gitignore index 480bdf5..4235d41 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,5 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store +.env \ No newline at end of file diff --git a/README.md b/README.md index b69166d..00c31fd 100644 --- a/README.md +++ b/README.md @@ -15,5 +15,5 @@ ## TODO: -- [ ] Fabryka połączenia do bazy danych -- [ ] Testy bazy \ No newline at end of file +- [x] Fabryka połączenia do bazy danych +- [x] Testy bazy \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2925206 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,18 @@ +services: + + db: + image: postgres:18.3 + restart: always + shm_size: 128mb + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_DB: ${POSTGRES_DB} + ports: + - 12900:5432 + + adminer: + image: adminer + restart: always + ports: + - 12901:8080 diff --git a/pom.xml b/pom.xml index c8e50cc..b38bf60 100644 --- a/pom.xml +++ b/pom.xml @@ -14,4 +14,44 @@ UTF-8 + + + com.zaxxer + HikariCP + 7.0.2 + + + + org.postgresql + postgresql + 42.7.10 + + + + io.github.cdimascio + dotenv-java + 3.2.0 + + + + junit + junit + 4.13.2 + test + + + + org.testcontainers + postgresql + 1.19.8 + test + + + org.junit.jupiter + junit-jupiter + 5.9.3 + test + + + \ No newline at end of file diff --git a/src/main/java/util/DatabaseConnection.java b/src/main/java/util/DatabaseConnection.java new file mode 100644 index 0000000..7676037 --- /dev/null +++ b/src/main/java/util/DatabaseConnection.java @@ -0,0 +1,33 @@ +package util; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import io.github.cdimascio.dotenv.Dotenv; + +import java.sql.Connection; +import java.sql.SQLException; + +public class DatabaseConnection { + + private static Dotenv dotenv = Dotenv.load(); + + private static HikariConfig config = new HikariConfig(); + private static HikariDataSource ds; + + static { + config.setJdbcUrl(dotenv.get("JDBC")); + config.setUsername(dotenv.get("POSTGRES_USER")); + config.setPassword(dotenv.get("POSTGRES_PASSWORD")); + config.setDriverClassName("org.postgresql.Driver"); + config.setMaximumPoolSize(10); + config.setMinimumIdle(2); + ds = new HikariDataSource(config); + } + + private DatabaseConnection() {}; + + public static Connection getConnection() throws SQLException { + return ds.getConnection(); + } + +} diff --git a/src/test/java/DatabaseConnectionTest.java b/src/test/java/DatabaseConnectionTest.java new file mode 100644 index 0000000..87a7661 --- /dev/null +++ b/src/test/java/DatabaseConnectionTest.java @@ -0,0 +1,37 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import static org.junit.Assert.assertFalse; + +import java.sql.Connection; +import java.sql.DriverManager; + +import org.testcontainers.containers.PostgreSQLContainer; + +public class DatabaseConnectionTest { + + static PostgreSQLContainer postgres = new PostgreSQLContainer<>( + "postgres:16-alpine" + ); + + @BeforeAll + static void beforeAll() { + postgres.start(); + } + + @AfterAll + static void afterAll() { + postgres.stop(); + } + + @Test + public void shouldConnect() throws Exception { + try (Connection conn = DriverManager.getConnection( + postgres.getJdbcUrl(), + postgres.getUsername(), + postgres.getPassword() + )) { + assertFalse(conn.isClosed()); + } + } +}