|
| 1 | +/* |
| 2 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.community.sql; |
| 17 | + |
| 18 | +import org.testcontainers.containers.*; |
| 19 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 20 | +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; |
| 21 | +import org.testcontainers.utility.DockerImageName; |
| 22 | + |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import javax.sql.DataSource; |
| 26 | +import com.zaxxer.hikari.HikariConfig; |
| 27 | +import com.zaxxer.hikari.HikariDataSource; |
| 28 | + |
| 29 | +public final class SharedSqlContainers { |
| 30 | + |
| 31 | + private static final ConcurrentHashMap<String, JdbcDatabaseContainer<?>> CONTAINERS = new ConcurrentHashMap<>(); |
| 32 | + |
| 33 | + private SharedSqlContainers() { } |
| 34 | + |
| 35 | + public static JdbcDatabaseContainer<?> getContainer(String dialectName) { |
| 36 | + boolean isCi = System.getenv("CI") != null || System.getenv("GITHUB_ACTIONS") != null; |
| 37 | + return CONTAINERS.computeIfAbsent(dialectName, key -> createContainerInternal(key, isCi)); |
| 38 | + } |
| 39 | + |
| 40 | + private static JdbcDatabaseContainer<?> createContainerInternal(String dialectName, boolean isCi) { |
| 41 | + switch (dialectName) { |
| 42 | + case "mysql": { |
| 43 | + MySQLContainer<?> c = new MySQLContainer<>("mysql:8.0") |
| 44 | + .withDatabaseName("testdb") |
| 45 | + .withUsername("testuser") |
| 46 | + .withPassword("testpass"); |
| 47 | + if (!isCi) c.withReuse(true); |
| 48 | + return c; |
| 49 | + } |
| 50 | + case "sqlserver": { |
| 51 | + MSSQLServerContainer<?> c = new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04") |
| 52 | + .acceptLicense() |
| 53 | + .withPassword("TestPass123!"); |
| 54 | + if (!isCi) c.withReuse(true); |
| 55 | + return c; |
| 56 | + } |
| 57 | + case "oracle": { |
| 58 | + OracleContainer c = new OracleContainer( |
| 59 | + DockerImageName.parse("gvenzl/oracle-free:23-slim-faststart") |
| 60 | + .asCompatibleSubstituteFor("gvenzl/oracle-xe")) { |
| 61 | + @Override |
| 62 | + public String getDatabaseName() { |
| 63 | + return "FREEPDB1"; |
| 64 | + } |
| 65 | + } |
| 66 | + .withPassword("oracle123") |
| 67 | + .withSharedMemorySize(1073741824L) |
| 68 | + .withStartupTimeout(Duration.ofMinutes(20)) |
| 69 | + .waitingFor(new WaitAllStrategy() |
| 70 | + .withStrategy(Wait.forListeningPort()) |
| 71 | + .withStrategy(Wait.forLogMessage(".*DATABASE IS READY TO USE.*\\n", 1)) |
| 72 | + ) |
| 73 | + .withEnv("ORACLE_CHARACTERSET", "AL32UTF8"); |
| 74 | + if (!isCi) c.withReuse(true); |
| 75 | + return c; |
| 76 | + } |
| 77 | + case "postgresql": { |
| 78 | + PostgreSQLContainer<?> c = new PostgreSQLContainer<>(DockerImageName.parse("postgres:15")) |
| 79 | + .withDatabaseName("testdb") |
| 80 | + .withUsername("test") |
| 81 | + .withPassword("test"); |
| 82 | + if (!isCi) c.withReuse(true); |
| 83 | + return c; |
| 84 | + } |
| 85 | + case "mariadb": { |
| 86 | + MariaDBContainer<?> c = new MariaDBContainer<>("mariadb:11.3.2") |
| 87 | + .withDatabaseName("testdb") |
| 88 | + .withUsername("testuser") |
| 89 | + .withPassword("testpass"); |
| 90 | + if (!isCi) c.withReuse(true); |
| 91 | + return c; |
| 92 | + } |
| 93 | + default: |
| 94 | + throw new IllegalArgumentException("Unsupported dialect: " + dialectName); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static void stopAll() { |
| 99 | + CONTAINERS.values().forEach(JdbcDatabaseContainer::stop); |
| 100 | + CONTAINERS.clear(); |
| 101 | + } |
| 102 | + |
| 103 | + public static DataSource createDataSource(JdbcDatabaseContainer<?> container) { |
| 104 | + HikariConfig config = new HikariConfig(); |
| 105 | + config.setJdbcUrl(container.getJdbcUrl()); |
| 106 | + config.setUsername(container.getUsername()); |
| 107 | + config.setPassword(container.getPassword()); |
| 108 | + config.setDriverClassName(container.getDriverClassName()); |
| 109 | + return new HikariDataSource(config); |
| 110 | + } |
| 111 | +} |
0 commit comments