Polishing.

Rename AssumeConnection to EnabledOnElasticsearch following the typical design of JUnit 5 EnabledOn… annotation programming model. Enable tests by default if the test element isn't annotated with EnabledOnElasticsearch as we assume that the extension was activated by the parent.

Move EnabledOnElasticsearch into utility project.

See #583
Original pull request: #609
This commit is contained in:
Mark Paluch
2021-04-08 10:24:20 +02:00
parent 75b2a08d60
commit b5cecf730b
15 changed files with 168 additions and 207 deletions

View File

@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-elasticsearch-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-elasticsearch-example-utils</artifactId>
<name>Spring Data Elasticsearch - Example Utilities</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.elasticsearch.util;
import java.util.Optional;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
/**
* {@link ExecutionCondition} evaluating {@link EnabledOnElasticsearch @EnableOnElasticsearch}.
*
* @author Christoph Strobl
* @author Prakhar Gupta
*/
class ElasticsearchAvailableCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
Optional<EnabledOnElasticsearch> annotation = AnnotationSupport.findAnnotation(extensionContext.getElement(),
EnabledOnElasticsearch.class);
return annotation.map(EnabledOnElasticsearch::url) //
.map(ElasticsearchAvailableCondition::checkServerRunning) //
.orElse(ConditionEvaluationResult.enabled("Enabled by default"));
}
private static ConditionEvaluationResult checkServerRunning(String url) {
RestTemplate template = new RestTemplate();
try {
ResponseEntity<byte[]> entity = template.exchange(url, HttpMethod.HEAD, null, byte[].class);
return ConditionEvaluationResult
.enabled("Successfully connected to Elasticsearch server: " + entity.getStatusCode());
} catch (RuntimeException e) {
return ConditionEvaluationResult.disabled("Elasticsearch unavailable at " + url + "; " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.elasticsearch.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* {@code @EnableOnElasticsearch} is used to signal that the annotated test class or test method is only
* <em>enabled</em> if Elasticsearch is available at {@link #url()}.
* <p>
* When applied at the class level, all test methods within that class will be enabled if Elasticsearch is available.
* <p>
* If a test method is disabled via this annotation, that does not prevent the test class from being instantiated.
* Rather, it prevents the execution of the test method and method-level lifecycle callbacks such as {@code @BeforeEach}
* methods, {@code @AfterEach} methods, and corresponding extension APIs.
*
* @author Prakhar Gupta
* @author Mark Paluch
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(ElasticsearchAvailableCondition.class)
public @interface EnabledOnElasticsearch {
/**
* URL pointing at the Elasticsearch instance to check.
*/
String url() default "http://localhost:9200";
}