Commit dabbb02d authored by Andy Wilkinson's avatar Andy Wilkinson

Prevents tests that use Elasticsearch from polluting the filesystem

By default, Elasticsearch writes it data to ./data. This led to data
being left on the filesystem after a mvn clean which could cause
failures when moving between branches that use different versions of
Elasticsearch.

This commit updates the tests for the Elasticsearch sample and
the Elasticsearch auto-configuration classes to write the
Elasticsearch data and logs into the target directory.
parent 326bdf29
...@@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.data.elasticsearch.city.CityReposi ...@@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.data.elasticsearch.city.CityReposi
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
...@@ -50,6 +51,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests { ...@@ -50,6 +51,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@Test @Test
public void testDefaultRepositoryConfiguration() throws Exception { public void testDefaultRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(TestConfiguration.class, this.context.register(TestConfiguration.class,
ElasticsearchAutoConfiguration.class, ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class,
...@@ -63,6 +67,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests { ...@@ -63,6 +67,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@Test @Test
public void testNoRepositoryConfiguration() throws Exception { public void testNoRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(EmptyConfiguration.class, this.context.register(EmptyConfiguration.class,
ElasticsearchAutoConfiguration.class, ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class,
...@@ -75,6 +82,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests { ...@@ -75,6 +82,9 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@Test @Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() { public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(CustomizedConfiguration.class, this.context.register(CustomizedConfiguration.class,
ElasticsearchAutoConfiguration.class, ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class,
......
...@@ -56,7 +56,9 @@ public class ElasticsearchAutoConfigurationTests { ...@@ -56,7 +56,9 @@ public class ElasticsearchAutoConfigurationTests {
public void createNodeClient() { public void createNodeClient() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.foo.bar:baz"); "spring.data.elasticsearch.properties.foo.bar:baz",
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class); ElasticsearchAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
...@@ -72,7 +74,9 @@ public class ElasticsearchAutoConfigurationTests { ...@@ -72,7 +74,9 @@ public class ElasticsearchAutoConfigurationTests {
// a port and check the exception // a port and check the exception
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.cluster-nodes:localhost"); "spring.data.elasticsearch.cluster-nodes:localhost",
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class); ElasticsearchAutoConfiguration.class);
this.thrown.expect(BeanCreationException.class); this.thrown.expect(BeanCreationException.class);
......
...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.elasticsearch; ...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.elasticsearch;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
...@@ -42,10 +43,14 @@ public class ElasticsearchDataAutoConfigurationTests { ...@@ -42,10 +43,14 @@ public class ElasticsearchDataAutoConfigurationTests {
@Test @Test
public void templateExists() { public void templateExists() {
this.context = new AnnotationConfigApplicationContext( this.context = new AnnotationConfigApplicationContext();
PropertyPlaceholderAutoConfiguration.class, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class, ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class); ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertEquals(1, assertEquals(1,
this.context.getBeanNamesForType(ElasticsearchTemplate.class).length); this.context.getBeanNamesForType(ElasticsearchTemplate.class).length);
} }
......
...@@ -20,6 +20,7 @@ import java.net.ConnectException; ...@@ -20,6 +20,7 @@ import java.net.ConnectException;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.OutputCapture; import org.springframework.boot.test.OutputCapture;
import org.springframework.core.NestedCheckedException; import org.springframework.core.NestedCheckedException;
...@@ -38,7 +39,11 @@ public class SampleElasticsearchApplicationTests { ...@@ -38,7 +39,11 @@ public class SampleElasticsearchApplicationTests {
@Test @Test
public void testDefaultSettings() throws Exception { public void testDefaultSettings() throws Exception {
try { try {
SampleElasticsearchApplication.main(new String[0]); new SpringApplicationBuilder(SampleElasticsearchApplication.class)
.properties(
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs")
.run();
} }
catch (IllegalStateException ex) { catch (IllegalStateException ex) {
if (serverNotRunning(ex)) { if (serverNotRunning(ex)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment