Skip to content

Commit c164d7b

Browse files
committed
#420 - Solr examples now try to start a Docker container.
If the Solr examples don't find a Solr instance already running a Docker container is started with the correct variant of Solr running. Cleaned up the test setup which was suffering from side effects between the tests and also dependent on the Solr version in use.
1 parent 5d536b1 commit c164d7b

File tree

14 files changed

+345
-85
lines changed

14 files changed

+345
-85
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</parent>
1717

1818
<modules>
19+
<module>util</module>
1920
<module>bom</module>
2021
<module>couchbase</module>
2122
<module>elasticsearch</module>

solr/example/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
<version>${project.version}</version>
1919
<scope>test</scope>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.data.examples</groupId>
23+
<artifactId>spring-data-examples-utils</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.testcontainers</groupId>
28+
<artifactId>testcontainers</artifactId>
29+
<version>1.7.3</version>
30+
<scope>test</scope>
31+
</dependency>
2132
</dependencies>
2233

2334
</project>

solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222

2323
import example.springdata.solr.product.Product;
2424
import example.springdata.solr.product.ProductRepository;
25-
import example.springdata.solr.test.util.RequiresSolrServer;
25+
import example.springdata.test.util.InfrastructureRule;
2626

2727
import java.time.Duration;
2828
import java.util.Arrays;
2929
import java.util.Optional;
3030

31-
import org.junit.ClassRule;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Rule;
3234
import org.junit.Test;
3335
import org.junit.runner.RunWith;
3436
import org.springframework.beans.factory.annotation.Autowired;
3537
import org.springframework.boot.test.context.SpringBootTest;
36-
import org.springframework.context.annotation.Configuration;
3738
import org.springframework.data.domain.PageRequest;
38-
import org.springframework.data.repository.CrudRepository;
3939
import org.springframework.data.solr.core.SolrOperations;
4040
import org.springframework.data.solr.core.query.Function;
4141
import org.springframework.data.solr.core.query.Query;
@@ -48,33 +48,41 @@
4848
* @author Christoph Strobl
4949
* @author Oliver Gierke
5050
* @author Mark Paluch
51+
* @author Jens Schauder
5152
*/
5253
@RunWith(SpringRunner.class)
5354
@SpringBootTest
5455
public class AdvancedSolrRepositoryTests {
5556

56-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
57+
@Rule @Autowired public InfrastructureRule requiresRunningServer;
5758

58-
@Configuration
59-
static class Config extends SolrTestConfiguration {
60-
61-
@Override
62-
protected void doInitTestData(CrudRepository<Product, String> repository) {
59+
@Autowired ProductRepository repository;
60+
@Autowired SolrOperations operations;
6361

64-
Product playstation = Product.builder().id("id-1").name("Playstation")
65-
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
66-
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
67-
.description("Playstation two is the successor of playstation in 2000.").build();
68-
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
69-
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();
7062

71-
repository.saveAll(Arrays.asList(playstation, playstation2, superNES, nintendo64));
72-
}
63+
/**
64+
* Remove test data when context is shut down.
65+
*/
66+
public @After void deleteDocumentsOnShutdown() {
67+
repository.deleteAll();
7368
}
7469

75-
@Autowired ProductRepository repository;
76-
@Autowired SolrOperations operations;
70+
/**
71+
* Initialize Solr instance with test data once context has started.
72+
*/
73+
public @Before void initWithTestData() throws InterruptedException {
7774

75+
repository.deleteAll();
76+
77+
Product playstation = Product.builder().id("id-1").name("Playstation")
78+
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
79+
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
80+
.description("Playstation two is the successor of playstation in 2000.").build();
81+
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
82+
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();
83+
84+
repository.saveAll(Arrays.asList(playstation, playstation2, superNES, nintendo64));
85+
}
7886
/**
7987
* {@link HighlightPage} holds next to the entities found also information about where a match was found within the
8088
* document. This allows to fine grained display snipplets of data containing the matching term in context.

solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package example.springdata.solr;
1717

18+
import example.springdata.solr.product.Product;
1819
import example.springdata.solr.product.ProductRepository;
19-
import example.springdata.solr.test.util.RequiresSolrServer;
20+
import example.springdata.test.util.InfrastructureRule;
2021

21-
import org.junit.ClassRule;
22+
import java.util.stream.IntStream;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Rule;
2227
import org.junit.Test;
2328
import org.junit.runner.RunWith;
2429
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,15 +32,34 @@
2732

2833
/**
2934
* @author Christoph Strobl
35+
* @author Jens Schauder
3036
*/
3137
@RunWith(SpringRunner.class)
32-
@SpringBootTest(classes = SolrTestConfiguration.class)
38+
@SpringBootTest
3339
public class BasicSolrRepositoryTests {
3440

35-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
41+
@Rule @Autowired public InfrastructureRule requiresRunningServer;
3642

3743
@Autowired ProductRepository repository;
3844

45+
/**
46+
* Remove test data when context is shut down.
47+
*/
48+
public @After void deleteDocumentsOnShutdown() {
49+
repository.deleteAll();
50+
}
51+
52+
/**
53+
* Initialize Solr instance with test data once context has started.
54+
*/
55+
public @Before void initWithTestData() {
56+
57+
repository.deleteAll();
58+
59+
IntStream.range(0, 100)
60+
.forEach(index -> repository.save(Product.builder().id("p-" + index).name("foobar").build()));
61+
}
62+
3963
/**
4064
* Finds all entries using a single request.
4165
*/

solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
package example.springdata.solr;
1717

1818
import example.springdata.solr.product.Product;
19-
20-
import java.util.stream.IntStream;
21-
22-
import javax.annotation.PostConstruct;
23-
import javax.annotation.PreDestroy;
19+
import example.springdata.solr.test.util.SolrInfrastructureRule;
20+
import example.springdata.test.util.InfrastructureRule;
2421

2522
import org.apache.solr.client.solrj.impl.HttpSolrClient;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2625
import org.springframework.beans.factory.annotation.Autowired;
2726
import org.springframework.boot.autoconfigure.SpringBootApplication;
2827
import org.springframework.context.annotation.Bean;
@@ -32,33 +31,22 @@
3231
/**
3332
* @author Christoph Strobl
3433
* @author Oliver Gierke
34+
* @author Jens Schauder
3535
*/
3636
@SpringBootApplication
3737
public class SolrTestConfiguration {
3838

3939
@Autowired CrudRepository<Product, String> repo;
4040

41-
public @Bean SolrTemplate solrTemplate() {
42-
return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build());
43-
}
41+
private static final Logger LOG = LoggerFactory.getLogger(SolrTestConfiguration.class);
4442

45-
/**
46-
* Remove test data when context is shut down.
47-
*/
48-
public @PreDestroy void deleteDocumentsOnShutdown() {
49-
repo.deleteAll();
50-
}
43+
public @Bean InfrastructureRule infrastructureRule() {
5144

52-
/**
53-
* Initialize Solr instance with test data once context has started.
54-
*/
55-
public @PostConstruct void initWithTestData() {
56-
doInitTestData(repo);
45+
return new SolrInfrastructureRule("techproducts");
5746
}
5847

59-
protected void doInitTestData(CrudRepository<Product, String> repository) {
60-
61-
IntStream.range(0, 100)
62-
.forEach(index -> repository.save(Product.builder().id("p-" + index).name("foobar").build()));
48+
public @Bean SolrTemplate solrTemplate() {
49+
return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl(infrastructureRule().getInfo()).build());
6350
}
51+
6452
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
logging.level.root=WARN
2+
logging.level.org.springframework.data.solr.core.SolrTemplate=DEBUG

solr/managed-schema/src/test/java/example/springdata/solr/SolrRepositoryTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import example.springdata.solr.product.ManagedProduct;
1919
import example.springdata.solr.product.ProductRepository;
20-
import example.springdata.solr.test.util.RequiresSolrServer;
20+
import example.springdata.test.util.InfrastructureRule;
2121

22-
import org.junit.ClassRule;
22+
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,12 +28,13 @@
2828

2929
/**
3030
* @author Christoph Strobl
31+
* @author Jens Schauder
3132
*/
3233
@RunWith(SpringRunner.class)
3334
@SpringBootTest
3435
public class SolrRepositoryTests {
3536

36-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
37+
@Rule @Autowired public InfrastructureRule requiresRunningServer;
3738

3839
@Autowired ProductRepository repo;
3940

solr/managed-schema/src/test/java/example/springdata/solr/SolrTestConfiguration.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import javax.annotation.PreDestroy;
2121

22+
import example.springdata.solr.test.util.SolrInfrastructureRule;
23+
import example.springdata.test.util.InfrastructureRule;
2224
import org.apache.solr.client.solrj.SolrClient;
2325
import org.apache.solr.client.solrj.impl.HttpSolrClient;
2426
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,8 +43,14 @@ public class SolrTestConfiguration {
4143

4244
@Autowired ProductRepository repo;
4345

46+
public @Bean
47+
InfrastructureRule infrastructureRule() {
48+
49+
return new SolrInfrastructureRule("schemaless");
50+
}
51+
4452
public @Bean SolrClient solrClient() {
45-
return new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build();
53+
return new HttpSolrClient.Builder().withBaseSolrUrl(infrastructureRule().getInfo()).build();
4654
}
4755

4856
/**

solr/util/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,15 @@
1616
<groupId>junit</groupId>
1717
<artifactId>junit</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>org.testcontainers</groupId>
21+
<artifactId>testcontainers</artifactId>
22+
<version>1.7.3</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.data.examples</groupId>
26+
<artifactId>spring-data-examples-utils</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
1929
</dependencies>
2030
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.solr.test.util;
17+
18+
import org.apache.http.client.methods.CloseableHttpResponse;
19+
import org.apache.http.client.methods.HttpGet;
20+
import org.apache.http.impl.client.CloseableHttpClient;
21+
import org.apache.http.impl.client.HttpClientBuilder;
22+
import org.testcontainers.containers.GenericContainer;
23+
24+
/**
25+
* @author Jens Schauder
26+
*/
27+
public class SolrExampleContainer extends GenericContainer<SolrExampleContainer> {
28+
29+
private static final String IMAGE_NAME = "solr";
30+
private static final String DEFAULT_VERSION = "7";
31+
private static final int DEFAULT_PORT = 8983;
32+
private final String example;
33+
34+
public SolrExampleContainer(final String example) {
35+
36+
super(IMAGE_NAME + ":" + DEFAULT_VERSION);
37+
this.example = example;
38+
this.withExposedPorts(DEFAULT_PORT) //
39+
.withCommand("bash", "-c", "solr start -e " + example + " && tail -f /dev/null");
40+
41+
}
42+
43+
@Override
44+
public void start() {
45+
super.start();
46+
47+
Integer port = getMappedPort(DEFAULT_PORT);
48+
String url = "http://localhost:" + port + "/" + IMAGE_NAME;
49+
50+
waitForSolr(url);
51+
52+
}
53+
54+
private void waitForSolr(String url) {
55+
56+
long waitStart = System.currentTimeMillis();
57+
long maxWaitDuration = 10000;
58+
59+
do {
60+
61+
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
62+
63+
CloseableHttpResponse response = client.execute(new HttpGet(url + "/" + example + "/admin/ping"));
64+
if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 200) {
65+
66+
break;
67+
}
68+
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
}
72+
73+
} while (System.currentTimeMillis() < waitStart + maxWaitDuration);
74+
}
75+
76+
public String getSolrBaseUrl() {
77+
return "http://localhost:" + getMappedPort(8983) + "/solr";
78+
}
79+
}

0 commit comments

Comments
 (0)