Skip to content

Commit c2e2a0f

Browse files
chungngoopsbsideup
authored andcommitted
Add "singleton container" example (#1178)
Closes #1138 * add example for singleton container * address review comment. * change method name to not starts with capital letter * Update examples/singleton-container/src/test/java/com/example/FooConcreteTestClass.java Co-Authored-By: chungngoops <[email protected]> * Update FooConcreteTestClass.java * Update BarConcreteTestClass.java * Update assert texts * Update FooConcreteTestClass.java
1 parent 6a988dd commit c2e2a0f

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>testcontainers-java-examples</artifactId>
7+
<groupId>org.testcontainers.examples</groupId>
8+
<version>NOVERSION</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>singleton-container</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>redis.clients</groupId>
17+
<artifactId>jedis</artifactId>
18+
<version>3.0.1</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.google.code.gson</groupId>
22+
<artifactId>gson</artifactId>
23+
<version>2.8.5</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.guava</groupId>
27+
<artifactId>guava</artifactId>
28+
<version>23.0</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.slf4j</groupId>
32+
<artifactId>slf4j-api</artifactId>
33+
<version>1.7.25</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>ch.qos.logback</groupId>
38+
<artifactId>logback-classic</artifactId>
39+
<version>1.2.3</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.cache;
2+
3+
import java.util.Optional;
4+
5+
public interface Cache {
6+
7+
void put(String key, Object value);
8+
9+
<T> Optional<T> get(String key, Class<T> expectedClass);
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.cache;
2+
3+
import com.google.gson.Gson;
4+
import redis.clients.jedis.Jedis;
5+
6+
import java.util.Optional;
7+
8+
public class RedisBackedCache implements Cache {
9+
10+
private final Jedis jedis;
11+
private final String cacheName;
12+
private final Gson gson;
13+
14+
public RedisBackedCache(Jedis jedis, String cacheName) {
15+
this.jedis = jedis;
16+
this.cacheName = cacheName;
17+
this.gson = new Gson();
18+
}
19+
20+
public void put(String key, Object value) {
21+
String jsonValue = gson.toJson(value);
22+
this.jedis.hset(this.cacheName, key, jsonValue);
23+
}
24+
25+
public <T> Optional<T> get(String key, Class<T> expectedClass) {
26+
String foundJson = this.jedis.hget(this.cacheName, key);
27+
28+
if (foundJson == null) {
29+
return Optional.empty();
30+
}
31+
32+
return Optional.of(gson.fromJson(foundJson, expectedClass));
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example;
2+
3+
import org.testcontainers.containers.GenericContainer;
4+
5+
public abstract class AbstractIntegrationTest {
6+
7+
public static final GenericContainer redis = new GenericContainer("redis:3.0.6")
8+
.withExposedPorts(6379);
9+
10+
static {
11+
redis.start();
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example;
2+
3+
import com.example.cache.Cache;
4+
import com.example.cache.RedisBackedCache;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import redis.clients.jedis.Jedis;
8+
9+
import java.util.Optional;
10+
11+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
12+
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
13+
14+
public class BarConcreteTestClass extends AbstractIntegrationTest {
15+
16+
private Cache cache;
17+
18+
@Before
19+
public void setUp() {
20+
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379));
21+
22+
cache = new RedisBackedCache(jedis, "bar");
23+
}
24+
25+
@Test
26+
public void testInsertValue() {
27+
cache.put("bar", "BAR");
28+
Optional<String> foundObject = cache.get("bar", String.class);
29+
30+
assertTrue("When inserting an object into the cache, it can be retrieved", foundObject.isPresent());
31+
assertEquals("When accessing the value of a retrieved object, the value must be the same", "BAR", foundObject.get());
32+
}
33+
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example;
2+
3+
import com.example.cache.Cache;
4+
import com.example.cache.RedisBackedCache;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import redis.clients.jedis.Jedis;
8+
9+
import java.util.Optional;
10+
11+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
12+
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
13+
14+
public class FooConcreteTestClass extends AbstractIntegrationTest {
15+
16+
private Cache cache;
17+
18+
@Before
19+
public void setUp() throws Exception {
20+
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379));
21+
22+
cache = new RedisBackedCache(jedis, "foo");
23+
}
24+
25+
@Test
26+
public void testInsertValue() {
27+
cache.put("foo", "FOO");
28+
Optional<String> foundObject = cache.get("foo", String.class);
29+
30+
assertTrue("When inserting an object into the cache, it can be retrieved", foundObject.isPresent());
31+
assertEquals("When accessing the value of a retrieved object, the value must be the same", "FOO", foundObject.get());
32+
}
33+
}

0 commit comments

Comments
 (0)