Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/singleton-container/pom.xml
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>
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);
}
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));
}
}
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();
}
}
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());
}

}
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());
}
}