Skip to content

Commit b9f48ab

Browse files
authored
Add zookeeper example (#6059)
Demonstrate how to use Apache Zookeeper along with `GenericContainer`. Fixes #1514
1 parent 84b1ad4 commit b9f48ab

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

docs/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Examples of different use cases provided by Testcontainers can be found below:
1414
- [Spring Boot with Kotlin](https://github.com/testcontainers/testcontainers-java/tree/main/examples/spring-boot-kotlin-redis)
1515
- [TestNG](https://github.com/testcontainers/testcontainers-java/tree/main/examples/redis-backed-cache-testng)
1616
- [ImmuDb](https://github.com/testcontainers/testcontainers-java/tree/main/examples/immudb)
17+
- [Zookeeper](https://github.com/testcontainers/testcontainers-java/tree/main/examples/zookeper)

examples/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ include 'spring-boot'
3131
include 'cucumber'
3232
include 'spring-boot-kotlin-redis'
3333
include 'immudb'
34+
include 'zookeeper'
3435

3536
ext.isCI = System.getenv("CI") != null
3637

examples/zookeeper/build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation 'org.apache.curator:curator-framework:5.3.0'
11+
testImplementation 'org.testcontainers:testcontainers'
12+
testImplementation 'org.assertj:assertj-core:3.23.1'
13+
testImplementation 'ch.qos.logback:logback-classic:1.2.11'
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example;
2+
3+
import org.apache.curator.framework.CuratorFramework;
4+
import org.apache.curator.framework.CuratorFrameworkFactory;
5+
import org.apache.curator.retry.RetryOneTime;
6+
import org.junit.Test;
7+
import org.testcontainers.containers.GenericContainer;
8+
9+
import java.nio.charset.StandardCharsets;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class ZookeeperContainerTest {
14+
15+
private static final int ZOOKEEPER_PORT = 2181;
16+
17+
@Test
18+
public void test() throws Exception {
19+
String path = "/messages/zk-tc";
20+
String content = "Running Zookeeper with Testcontainers";
21+
try (
22+
GenericContainer<?> zookeeper = new GenericContainer<>("zookeeper:3.8.0").withExposedPorts(ZOOKEEPER_PORT)
23+
) {
24+
zookeeper.start();
25+
26+
String connectionString = zookeeper.getHost() + ":" + zookeeper.getMappedPort(ZOOKEEPER_PORT);
27+
CuratorFramework curatorFramework = CuratorFrameworkFactory
28+
.builder()
29+
.connectString(connectionString)
30+
.retryPolicy(new RetryOneTime(100))
31+
.build();
32+
curatorFramework.start();
33+
curatorFramework.create().creatingParentsIfNeeded().forPath(path, content.getBytes());
34+
35+
byte[] bytes = curatorFramework.getData().forPath(path);
36+
curatorFramework.close();
37+
38+
assertThat(new String(bytes, StandardCharsets.UTF_8)).isEqualTo(content);
39+
}
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT"/>
13+
</root>
14+
15+
<logger name="org.testcontainers" level="INFO"/>
16+
</configuration>

0 commit comments

Comments
 (0)