From 82d6e86f9fb9be33b0d49c57ef24ccb0b6109005 Mon Sep 17 00:00:00 2001 From: Sergei Egorov Date: Tue, 7 Jun 2016 08:18:18 +0300 Subject: [PATCH 1/3] Add Spring Boot example of usage --- disque-job-queue/pom.xml | 12 ++++ pom.xml | 15 +---- redis-backed-cache/pom.xml | 12 ++++ selenium-container/pom.xml | 12 ++++ spring-boot/pom.xml | 59 +++++++++++++++++++ .../java/com/example/DemoApplication.java | 32 ++++++++++ .../com/example/AbstractIntegrationTest.java | 53 +++++++++++++++++ .../java/com/example/DemoApplicationTest.java | 23 ++++++++ .../src/test/resources/logback-test.xml | 8 +++ 9 files changed, 213 insertions(+), 13 deletions(-) create mode 100644 spring-boot/pom.xml create mode 100644 spring-boot/src/main/java/com/example/DemoApplication.java create mode 100644 spring-boot/src/test/java/com/example/AbstractIntegrationTest.java create mode 100644 spring-boot/src/test/java/com/example/DemoApplicationTest.java create mode 100644 spring-boot/src/test/resources/logback-test.xml diff --git a/disque-job-queue/pom.xml b/disque-job-queue/pom.xml index 7799c8b18ca..256584ba69c 100644 --- a/disque-job-queue/pom.xml +++ b/disque-job-queue/pom.xml @@ -26,6 +26,18 @@ guava 18.0 + + org.slf4j + slf4j-api + 1.7.7 + provided + + + ch.qos.logback + logback-classic + 1.1.2 + test + diff --git a/pom.xml b/pom.xml index 65421865605..259fb6253c1 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,7 @@ redis-backed-cache disque-job-queue selenium-container + spring-boot pom @@ -36,22 +37,10 @@ - - org.slf4j - slf4j-api - 1.7.7 - provided - - - ch.qos.logback - logback-classic - 1.1.2 - test - org.rnorth.visible-assertions visible-assertions - 1.0.2 + 1.0.5 test diff --git a/redis-backed-cache/pom.xml b/redis-backed-cache/pom.xml index e95ef6c4475..bcb7646f1cf 100644 --- a/redis-backed-cache/pom.xml +++ b/redis-backed-cache/pom.xml @@ -26,6 +26,18 @@ guava 18.0 + + org.slf4j + slf4j-api + 1.7.7 + provided + + + ch.qos.logback + logback-classic + 1.1.2 + test + diff --git a/selenium-container/pom.xml b/selenium-container/pom.xml index a9196da252f..1187b66f680 100644 --- a/selenium-container/pom.xml +++ b/selenium-container/pom.xml @@ -22,6 +22,18 @@ selenium ${testcontainers.version} + + org.slf4j + slf4j-api + 1.7.7 + provided + + + ch.qos.logback + logback-classic + 1.1.2 + test + diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml new file mode 100644 index 00000000000..e0952987c77 --- /dev/null +++ b/spring-boot/pom.xml @@ -0,0 +1,59 @@ + + + + + testcontainers-java-examples + org.testcontainers.examples + NOVERSION + + 4.0.0 + + spring-boot + + + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-dependencies + 1.3.5.RELEASE + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-redis + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 1.3.5.RELEASE + + + + + + diff --git a/spring-boot/src/main/java/com/example/DemoApplication.java b/spring-boot/src/main/java/com/example/DemoApplication.java new file mode 100644 index 00000000000..3373643d4d7 --- /dev/null +++ b/spring-boot/src/main/java/com/example/DemoApplication.java @@ -0,0 +1,32 @@ +package com.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@RestController +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Autowired + StringRedisTemplate stringRedisTemplate; + + @RequestMapping(value = "/foo", method = RequestMethod.GET) + public String get() { + return stringRedisTemplate.opsForValue().get("foo"); + } + + @RequestMapping(value = "/foo", method = RequestMethod.PUT) + public void set(@RequestBody String value) { + stringRedisTemplate.opsForValue().set("foo", value); + } +} diff --git a/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java new file mode 100644 index 00000000000..3e9bc238092 --- /dev/null +++ b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java @@ -0,0 +1,53 @@ +package com.example; + +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.testcontainers.containers.GenericContainer; + +import javax.annotation.PostConstruct; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(DemoApplicationTest.TestConfiguration.class) +@WebIntegrationTest(randomPort = true) +public abstract class AbstractIntegrationTest { + + @ClassRule + public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379); + + @Configuration + @Import(DemoApplication.class) + static class TestConfiguration { + + @Autowired + ConfigurableEnvironment environment; + + @PostConstruct + public void init() { + environment.getPropertySources().addFirst(new PropertySource("containers") { + @Override + public Object getProperty(String name) { + switch (name) { + case "spring.redis.host": + return redis.getContainerIpAddress(); + case "spring.redis.port": + return redis.getMappedPort(6379); + default: + return null; + } + } + }); + } + } + + @Value("${local.server.port}") + protected int port; +} diff --git a/spring-boot/src/test/java/com/example/DemoApplicationTest.java b/spring-boot/src/test/java/com/example/DemoApplicationTest.java new file mode 100644 index 00000000000..baa050195cd --- /dev/null +++ b/spring-boot/src/test/java/com/example/DemoApplicationTest.java @@ -0,0 +1,23 @@ +package com.example; + +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + + +import static org.rnorth.visibleassertions.VisibleAssertions.*; + +public class DemoApplicationTest extends AbstractIntegrationTest { + + RestTemplate restTemplate = new RestTemplate(); + + @Test + public void simpleTest() { + String fooResource = "http://localhost:" + port + "/foo"; + + info("putting 'bar' to " + fooResource); + restTemplate.put(fooResource, "bar"); + + assertEquals("value is set", "bar", restTemplate.getForObject(fooResource, String.class)); + } + +} diff --git a/spring-boot/src/test/resources/logback-test.xml b/spring-boot/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..86c4c861a78 --- /dev/null +++ b/spring-boot/src/test/resources/logback-test.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file From a87c3c12e7420b74edfdfac099ba997f36d593f7 Mon Sep 17 00:00:00 2001 From: Sergei Egorov Date: Mon, 8 Aug 2016 13:21:24 +0300 Subject: [PATCH 2/3] code review fixes --- spring-boot/pom.xml | 100 +++++++++--------- .../com/example/AbstractIntegrationTest.java | 21 ++-- 2 files changed, 56 insertions(+), 65 deletions(-) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index e0952987c77..0594b26396c 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -1,59 +1,57 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - - testcontainers-java-examples - org.testcontainers.examples - NOVERSION - - 4.0.0 + + testcontainers-java-examples + org.testcontainers.examples + NOVERSION + + 4.0.0 - spring-boot + spring-boot - - UTF-8 - 1.8 - + + UTF-8 + 1.8 + - - - - org.springframework.boot - spring-boot-dependencies - 1.3.5.RELEASE - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-redis - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - 1.3.5.RELEASE - - - - + + + + org.springframework.boot + spring-boot-dependencies + 1.3.5.RELEASE + pom + import + + + + + + org.springframework.boot + spring-boot-starter-redis + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 1.3.5.RELEASE + + + diff --git a/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java index 3e9bc238092..d6a03511e21 100644 --- a/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java +++ b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java @@ -9,7 +9,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.PropertySource; +import org.springframework.mock.env.MockPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.testcontainers.containers.GenericContainer; @@ -32,19 +32,12 @@ static class TestConfiguration { @PostConstruct public void init() { - environment.getPropertySources().addFirst(new PropertySource("containers") { - @Override - public Object getProperty(String name) { - switch (name) { - case "spring.redis.host": - return redis.getContainerIpAddress(); - case "spring.redis.port": - return redis.getMappedPort(6379); - default: - return null; - } - } - }); + MockPropertySource propertySource = new MockPropertySource("TestContainers"); + + propertySource.setProperty("spring.redis.host", redis.getContainerIpAddress()); + propertySource.setProperty("spring.redis.port", redis.getMappedPort(6379)); + + environment.getPropertySources().addFirst(propertySource); } } From 269c58ca5108e0d779af0968c59899d9167b04d7 Mon Sep 17 00:00:00 2001 From: Sergei Egorov Date: Fri, 9 Sep 2016 14:31:56 +0300 Subject: [PATCH 3/3] Simplify Spring Boot example --- .../com/example/AbstractIntegrationTest.java | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java index d6a03511e21..3d2f54fc482 100644 --- a/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java +++ b/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java @@ -2,42 +2,30 @@ import org.junit.ClassRule; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.mock.env.MockPropertySource; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.testcontainers.containers.GenericContainer; -import javax.annotation.PostConstruct; - @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(DemoApplicationTest.TestConfiguration.class) +@SpringApplicationConfiguration(value = DemoApplication.class, initializers = AbstractIntegrationTest.Initializer.class) @WebIntegrationTest(randomPort = true) public abstract class AbstractIntegrationTest { @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379); - @Configuration - @Import(DemoApplication.class) - static class TestConfiguration { - - @Autowired - ConfigurableEnvironment environment; - - @PostConstruct - public void init() { - MockPropertySource propertySource = new MockPropertySource("TestContainers"); - - propertySource.setProperty("spring.redis.host", redis.getContainerIpAddress()); - propertySource.setProperty("spring.redis.port", redis.getMappedPort(6379)); - - environment.getPropertySources().addFirst(propertySource); + public static class Initializer implements ApplicationContextInitializer { + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(), + "spring.redis.host=" + redis.getContainerIpAddress(), + "spring.redis.port=" + redis.getMappedPort(6379) + ); } }