-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add testcontainers-java module
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.oceanbase.samples</groupId> | ||
<artifactId>testcontainers-java</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>ob-samples-testcontainers-java</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.oceanbase</groupId> | ||
<artifactId>oceanbase-client</artifactId> | ||
<version>2.4.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>oceanbase</artifactId> | ||
<version>1.19.7</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>2.17.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
mvn verify |
96 changes: 96 additions & 0 deletions
96
java/testcontainers-java/src/test/java/com/oceanbase/samples/OceanBaseCEContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.oceanbase.samples; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
import org.testcontainers.lifecycle.Startables; | ||
import org.testcontainers.oceanbase.OceanBaseCEContainer; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.stream.Stream; | ||
|
||
public class OceanBaseCEContainerTest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OceanBaseCEContainerTest.class); | ||
|
||
public static final OceanBaseCEContainer CONTAINER = | ||
new OceanBaseCEContainer("oceanbase/oceanbase-ce:latest") | ||
.withEnv("MODE", "slim") | ||
.withEnv("FASTBOOT", "true") | ||
.withLogConsumer(new Slf4jLogConsumer(LOG)); | ||
|
||
@BeforeClass | ||
public static void startContainers() { | ||
Startables.deepStart(Stream.of(CONTAINER)).join(); | ||
LOG.info( | ||
"OceanBase docker container started, image: {}, host: {}, sql port: {}, rpc port:{}.", | ||
CONTAINER.getDockerImageName(), | ||
CONTAINER.getHost(), | ||
CONTAINER.getMappedPort(2881), | ||
CONTAINER.getMappedPort(2882)); | ||
} | ||
|
||
@AfterClass | ||
public static void closeContainers() { | ||
CONTAINER.close(); | ||
LOG.info("OceanBase docker container stopped."); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
String database = "testcontainers"; | ||
String table = "person"; | ||
String tableName = String.format("`%s`.`%s`", database, table); | ||
|
||
LOG.info( | ||
"Try to connect to OceanBase docker container with url: {}.", | ||
CONTAINER.getJdbcUrl()); | ||
try (Connection connection = CONTAINER.createConnection("?useSSL=false")) { | ||
LOG.info("Connect to OceanBase docker container successfully."); | ||
|
||
LOG.info("Prepare database and table."); | ||
try (Statement statement = connection.createStatement()) { | ||
statement.execute("CREATE DATABASE IF NOT EXISTS " + database); | ||
statement.execute("USE " + database); | ||
statement.execute( | ||
"CREATE TABLE IF NOT EXISTS " + table + " (name VARCHAR(50), age INT)"); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
LOG.info("Insert data to table {}.", tableName); | ||
try (PreparedStatement ps = | ||
connection.prepareStatement("INSERT INTO " + tableName + " values(?, ?)")) { | ||
ps.setString(1, "Adam"); | ||
ps.setInt(2, 28); | ||
ps.executeUpdate(); | ||
ps.setString(1, "Eve"); | ||
ps.setInt(2, 26); | ||
ps.executeUpdate(); | ||
} | ||
|
||
LOG.info("Query rows from {}.", tableName); | ||
try (PreparedStatement ps = | ||
connection.prepareStatement( | ||
"SELECT * from " + tableName, | ||
ResultSet.TYPE_FORWARD_ONLY, | ||
ResultSet.CONCUR_READ_ONLY)) { | ||
ResultSet rs = ps.executeQuery(); | ||
int count = 0; | ||
while (rs.next()) { | ||
LOG.info("Row {}: name {}, age {}.", count++, rs.getString(1), rs.getInt(2)); | ||
} | ||
assert count == 2; | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
java/testcontainers-java/src/test/resources/log4j2-test.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
rootLogger.level=INFO | ||
rootLogger.appenderRef.test.ref = TestLogger | ||
|
||
appender.testlogger.name = TestLogger | ||
appender.testlogger.type = CONSOLE | ||
appender.testlogger.target = SYSTEM_ERR | ||
appender.testlogger.layout.type = PatternLayout | ||
appender.testlogger.layout.pattern = %-4r [%t] %-5p %c - %m%n |