Skip to content

Commit b6c86c6

Browse files
christophstroblodrotbohm
authored andcommitted
#514 - Fix test setup of Spring Data for Apache Solr examples.
Guard test execution of Spring Data for Apache Solr examples with collection requirement. We now make sure to register required Beans just once and check if a specific collection is available via Apache Solr. Move off deprecated API in Solr example.
1 parent 5578511 commit b6c86c6

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Arrays;
2929
import java.util.Optional;
3030

31+
import javax.annotation.PostConstruct;
32+
3133
import org.junit.ClassRule;
3234
import org.junit.Test;
3335
import org.junit.runner.RunWith;
@@ -50,15 +52,15 @@
5052
* @author Mark Paluch
5153
*/
5254
@RunWith(SpringRunner.class)
53-
@SpringBootTest
55+
@SpringBootTest(classes = SolrTestConfiguration.class)
5456
public class AdvancedSolrRepositoryTests {
5557

56-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
58+
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
5759

5860
@Configuration
59-
static class Config extends SolrTestConfiguration {
61+
static class Config {
6062

61-
@Override
63+
@PostConstruct
6264
protected void doInitTestData(CrudRepository<Product, String> repository) {
6365

6466
Product playstation = Product.builder().id("id-1").name("Playstation")
@@ -82,7 +84,7 @@ protected void doInitTestData(CrudRepository<Product, String> repository) {
8284
@Test
8385
public void annotationBasedHighlighting() {
8486

85-
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", new PageRequest(0, 10));
87+
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", PageRequest.of(0, 10));
8688

8789
products.getHighlighted().forEach(entry -> entry.getHighlights().forEach(highligh -> System.out
8890
.println(entry.getEntity().getId() + " | " + highligh.getField() + ":\t" + highligh.getSnipplets())));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@SpringBootTest(classes = SolrTestConfiguration.class)
3333
public class BasicSolrRepositoryTests {
3434

35-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
35+
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
3636

3737
@Autowired ProductRepository repository;
3838

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@SpringBootTest
3434
public class SolrRepositoryTests {
3535

36-
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
36+
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("gettingstarted");
3737

3838
@Autowired ProductRepository repo;
3939

solr/util/src/main/java/example/springdata/solr/test/util/RequiresSolrServer.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.junit.rules.TestRule;
2828
import org.junit.runner.Description;
2929
import org.junit.runners.model.Statement;
30+
import org.springframework.lang.Nullable;
31+
import org.springframework.util.StringUtils;
3032

3133
/**
3234
* {@link TestRule} implementation using {@link CloseableHttpClient} to check if Solr is running by sending
@@ -39,15 +41,26 @@ public class RequiresSolrServer implements TestRule {
3941
private static final String PING_PATH = "/admin/info/system";
4042

4143
private final String baseUrl;
44+
private final @Nullable String collection;
4245

4346
private RequiresSolrServer(String baseUrl) {
47+
this(baseUrl, null);
48+
}
49+
50+
private RequiresSolrServer(String baseUrl, @Nullable String collection) {
51+
4452
this.baseUrl = baseUrl;
53+
this.collection = collection;
4554
}
4655

4756
public static RequiresSolrServer onLocalhost() {
4857
return new RequiresSolrServer("http://localhost:8983/solr");
4958
}
5059

60+
public RequiresSolrServer withCollection(String collection) {
61+
return new RequiresSolrServer(baseUrl, collection);
62+
}
63+
5164
@Override
5265
public Statement apply(Statement base, Description description) {
5366
return new Statement() {
@@ -64,7 +77,10 @@ public void evaluate() throws Throwable {
6477
private void checkServerRunning() {
6578

6679
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
67-
CloseableHttpResponse response = client.execute(new HttpGet(baseUrl + PING_PATH));
80+
81+
String url = StringUtils.hasText(collection) ? baseUrl + "/" + collection + "/select?q=*:*" : baseUrl + PING_PATH;
82+
83+
CloseableHttpResponse response = client.execute(new HttpGet(url));
6884
if (response != null && response.getStatusLine() != null) {
6985
Assume.assumeThat(response.getStatusLine().getStatusCode(), Is.is(200));
7086
}

0 commit comments

Comments
 (0)