Skip to content

Commit

Permalink
docs: add README for testcontainers-java
Browse files Browse the repository at this point in the history
  • Loading branch information
whhe committed Apr 22, 2024
1 parent bcfc966 commit 6bf00a1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
60 changes: 60 additions & 0 deletions java/testcontainers-java/README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TestContainers Java

[English](README.md) | 简体中文

本文介绍了如何通过 testcontainers-java 启动和测试 OceanBase Docker 容器,更多详细信息可以参见 https://java.testcontainers.org/modules/databases/oceanbase

## 快速开始

将 TestContainers OceanBase 模块添加到 POM。

```xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oceanbase</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
```

[OceanBaseCEContainerTest.java](src/test/java/com/oceanbase/samples/OceanBaseCEContainerTest.java) 代码为例。

以下代码实现了`OceanBaseCEContainer`的生命周期管理。 它将在执行任何测试用例之前启动容器实例,并在执行所有测试用例后停止容器。

```java
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.");
}
```

您可以在测试用例中使用容器实例,如下所示:

```java
@Test
public void test() {
try (Connection connection = CONTAINER.createConnection("?useSSL=false")) {
...
}
}
```
60 changes: 60 additions & 0 deletions java/testcontainers-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TestContainers Java

English | [简体中文](README-CN.md)

This article describes how to start and test OceanBase Docker container through `testcontainers-java`, you can see here for more details https://java.testcontainers.org/modules/databases/oceanbase/.

## Quick Start

Add TestContainers OceanBase module to POM.

```xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oceanbase</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
```

Take [OceanBaseCEContainerTest.java](src/test/java/com/oceanbase/samples/OceanBaseCEContainerTest.java) code as an example.

The following code implements life cycle management of `OceanBaseCEContainer`. It will start the container instance before executing any test cases and stop the container after all test cases have been executed.

```java
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.");
}
```

You can use the container instance in test cases like below:

```java
@Test
public void test() {
try (Connection connection = CONTAINER.createConnection("?useSSL=false")) {
...
}
}
```

0 comments on commit 6bf00a1

Please sign in to comment.