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 0a9af24 commit 2aa3c58
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 80 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
1 change: 1 addition & 0 deletions test/native/native-image-filter/extra-filter.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
{"excludeClasses": "org.locationtech.jts.geom.**"},
{"excludeClasses": "org.postgresql.**"},
{"excludeClasses": "org.slf4j.event.**"},
{"excludeClasses": "org.testcontainers.jdbc.**"},

{"excludeClasses": "org.apache.shardingsphere.test.natived.**"}
],
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
@@ -1,13 +1,13 @@
[
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.algorithm.sharding.classbased.ClassBasedShardingAlgorithmFactory"},
"name":"org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.rule.EncryptRule"},
"name":"org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.TestQueryAssistedShardingEncryptAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
}
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.algorithm.sharding.classbased.ClassBasedShardingAlgorithmFactory"},
"name":"org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.rule.EncryptRule"},
"name":"org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.TestQueryAssistedShardingEncryptAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
}
]

Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ mode:
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_0
username: root
password: 123456
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_0?TC_DAEMON=true
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_1
username: root
password: 123456
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_1?TC_DAEMON=true
ds_2:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.postgresql.Driver
jdbcUrl: jdbc:postgresql://localhost:49965/demo_ds_2
username: root
password: 123456
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_2?TC_DAEMON=true

rules:
- !SHARDING
Expand Down

0 comments on commit 2aa3c58

Please sign in to comment.