-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
6a988dd
commit c2e2a0f
Showing
6 changed files
with
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>testcontainers-java-examples</artifactId> | ||
<groupId>org.testcontainers.examples</groupId> | ||
<version>NOVERSION</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>singleton-container</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>3.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>23.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.7.25</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.2.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
10 changes: 10 additions & 0 deletions
10
examples/singleton-container/src/main/java/com/example/cache/Cache.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,10 @@ | ||
package com.example.cache; | ||
|
||
import java.util.Optional; | ||
|
||
public interface Cache { | ||
|
||
void put(String key, Object value); | ||
|
||
<T> Optional<T> get(String key, Class<T> expectedClass); | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/singleton-container/src/main/java/com/example/cache/RedisBackedCache.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,34 @@ | ||
package com.example.cache; | ||
|
||
import com.google.gson.Gson; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.Optional; | ||
|
||
public class RedisBackedCache implements Cache { | ||
|
||
private final Jedis jedis; | ||
private final String cacheName; | ||
private final Gson gson; | ||
|
||
public RedisBackedCache(Jedis jedis, String cacheName) { | ||
this.jedis = jedis; | ||
this.cacheName = cacheName; | ||
this.gson = new Gson(); | ||
} | ||
|
||
public void put(String key, Object value) { | ||
String jsonValue = gson.toJson(value); | ||
this.jedis.hset(this.cacheName, key, jsonValue); | ||
} | ||
|
||
public <T> Optional<T> get(String key, Class<T> expectedClass) { | ||
String foundJson = this.jedis.hget(this.cacheName, key); | ||
|
||
if (foundJson == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(gson.fromJson(foundJson, expectedClass)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/singleton-container/src/test/java/com/example/AbstractIntegrationTest.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,13 @@ | ||
package com.example; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
|
||
public abstract class AbstractIntegrationTest { | ||
|
||
public static final GenericContainer redis = new GenericContainer("redis:3.0.6") | ||
.withExposedPorts(6379); | ||
|
||
static { | ||
redis.start(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/singleton-container/src/test/java/com/example/BarConcreteTestClass.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,34 @@ | ||
package com.example; | ||
|
||
import com.example.cache.Cache; | ||
import com.example.cache.RedisBackedCache; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; | ||
|
||
public class BarConcreteTestClass extends AbstractIntegrationTest { | ||
|
||
private Cache cache; | ||
|
||
@Before | ||
public void setUp() { | ||
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379)); | ||
|
||
cache = new RedisBackedCache(jedis, "bar"); | ||
} | ||
|
||
@Test | ||
public void testInsertValue() { | ||
cache.put("bar", "BAR"); | ||
Optional<String> foundObject = cache.get("bar", String.class); | ||
|
||
assertTrue("When inserting an object into the cache, it can be retrieved", foundObject.isPresent()); | ||
assertEquals("When accessing the value of a retrieved object, the value must be the same", "BAR", foundObject.get()); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
examples/singleton-container/src/test/java/com/example/FooConcreteTestClass.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,33 @@ | ||
package com.example; | ||
|
||
import com.example.cache.Cache; | ||
import com.example.cache.RedisBackedCache; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; | ||
|
||
public class FooConcreteTestClass extends AbstractIntegrationTest { | ||
|
||
private Cache cache; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379)); | ||
|
||
cache = new RedisBackedCache(jedis, "foo"); | ||
} | ||
|
||
@Test | ||
public void testInsertValue() { | ||
cache.put("foo", "FOO"); | ||
Optional<String> foundObject = cache.get("foo", String.class); | ||
|
||
assertTrue("When inserting an object into the cache, it can be retrieved", foundObject.isPresent()); | ||
assertEquals("When accessing the value of a retrieved object, the value must be the same", "FOO", foundObject.get()); | ||
} | ||
} |