Added database connection, added test and start with Testcontainers.

This commit is contained in:
maciejrusek
2026-04-21 21:48:49 +02:00
parent cb7ec452eb
commit c1af31f41d
7 changed files with 135 additions and 3 deletions

3
.env.example Normal file
View File

@@ -0,0 +1,3 @@
POSTGRES_PASSWORD=""
POSTGRES_USER=""
POSTGRES_DB=""

3
.gitignore vendored
View File

@@ -36,4 +36,5 @@ build/
.vscode/
### Mac OS ###
.DS_Store
.DS_Store
.env

View File

@@ -15,5 +15,5 @@
## TODO:
- [ ] Fabryka połączenia do bazy danych
- [ ] Testy bazy
- [x] Fabryka połączenia do bazy danych
- [x] Testy bazy

18
docker-compose.yaml Normal file
View File

@@ -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

40
pom.xml
View File

@@ -14,4 +14,44 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.10</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -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();
}
}

View File

@@ -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());
}
}
}