Skip to content

Commit

Permalink
Prevents tests that use Elasticsearch from polluting the filesystem
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wilkinsona committed Apr 21, 2015
1 parent 326bdf2 commit dabbb02
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 0 additions & 1 deletion spring-boot-autoconfigure/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
Expand All @@ -50,6 +51,9 @@ public void close() {
@Test
public void testDefaultRepositoryConfiguration() throws Exception {
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,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand All @@ -63,6 +67,9 @@ public void testDefaultRepositoryConfiguration() throws Exception {
@Test
public void testNoRepositoryConfiguration() throws Exception {
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,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand All @@ -75,6 +82,9 @@ public void testNoRepositoryConfiguration() throws Exception {
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
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,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void close() {
public void createNodeClient() {
this.context = new AnnotationConfigApplicationContext();
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,
ElasticsearchAutoConfiguration.class);
this.context.refresh();
Expand All @@ -72,7 +74,9 @@ public void createTransportClient() throws Exception {
// a port and check the exception
this.context = new AnnotationConfigApplicationContext();
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,
ElasticsearchAutoConfiguration.class);
this.thrown.expect(BeanCreationException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

Expand All @@ -42,10 +43,14 @@ public void close() {

@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class,
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(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(ElasticsearchTemplate.class).length);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.OutputCapture;
import org.springframework.core.NestedCheckedException;

Expand All @@ -38,7 +39,11 @@ public class SampleElasticsearchApplicationTests {
@Test
public void testDefaultSettings() throws Exception {
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) {
if (serverNotRunning(ex)) {
Expand Down

0 comments on commit dabbb02

Please sign in to comment.