Skip to content

Commit

Permalink
Use testcontainers-java to test
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Dec 12, 2023
1 parent 6a50788 commit 46b0cdc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 64 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<hamcrest.version>2.2</hamcrest.version>
<mockito.version>4.11.0</mockito.version>
<awaitility.version>4.2.0</awaitility.version>
<testcontainers.version>1.17.6</testcontainers.version>
<testcontainers.version>1.19.3</testcontainers.version>
<commons-csv.version>1.9.0</commons-csv.version>

<graalvm.version>21.2.0</graalvm.version>
Expand Down
5 changes: 5 additions & 0 deletions test/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,17 @@
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.test.natived.jdbc.commons.AbstractShardingCommonTest;
import org.apache.shardingsphere.test.natived.jdbc.commons.FileTestUtils;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.util.Properties;

public class PostgresTest {

private static final String USERNAME = "root";

private static final String PASSWORD = "123456";

private static final String DATABASE = "test";

private static final String JDBC_URL = "jdbc:postgresql://localhost:49965/" + DATABASE;

private static Process process;

private AbstractShardingCommonTest abstractShardingCommonTest;

private static Connection openConnection() throws SQLException {
Properties props = new Properties();
props.setProperty("user", USERNAME);
props.setProperty("password", PASSWORD);
return DriverManager.getConnection(JDBC_URL, props);
}

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
@BeforeAll
static void beforeAll() throws IOException {
System.out.println("Starting PostgreSQL ...");
process = new ProcessBuilder(
"docker", "run", "--rm", "-p", "49965:5432", "-e", "POSTGRES_DB=" + DATABASE, "-e", "POSTGRES_USER=" + USERNAME,
"-e", "POSTGRES_PASSWORD=" + PASSWORD, "postgres:16.1-bookworm")
.redirectOutput(new File("target/test-classes/postgres-stdout.txt"))
.redirectError(new File("target/test-classes/postgres-stderr.txt"))
.start();
Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().until(() -> {
openConnection().close();
return true;
});
try (Connection connection = openConnection()) {
connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_0;");
connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_1;");
connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_2;");
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println("PostgreSQL started");
}

@AfterAll
static void tearDown() {
if (process != null && process.isAlive()) {
System.out.println("Shutting down PostgreSQL");
process.destroy();
}
}

@Test
@EnabledInNativeImage
void assertShardingInLocalTransactions() throws SQLException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ mode:
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_0
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://localhost:49965/demo_ds_0
username: root
password: 123456
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_1
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://localhost:49965/demo_ds_1
username: root
password: 123456
ds_2:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_2
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://localhost:49965/demo_ds_2
username: root
password: 123456

Expand Down

0 comments on commit 46b0cdc

Please sign in to comment.