Migrate Couchbase tests to JUnit 5.

Closes #664
This commit is contained in:
Mark Paluch
2023-05-15 11:32:12 +02:00
parent 545d957031
commit ea17c12f93
10 changed files with 304 additions and 309 deletions

View File

@@ -1,5 +1,6 @@
<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">
<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>
@@ -15,8 +16,9 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@@ -1,82 +0,0 @@
/*
* Copyright 2017-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
*
* https://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.couchbase.util;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import javax.net.SocketFactory;
import org.junit.AssumptionViolatedException;
import org.junit.rules.ExternalResource;
/**
* Rule to check Couchbase server availability. If Couchbase is not running, tests are skipped.
*
* @author Mark Paluch
*/
public class CouchbaseAvailableRule extends ExternalResource {
private final String host;
private final int port;
private final Duration timeout = Duration.ofSeconds(1);
private CouchbaseAvailableRule(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Create a new rule requiring Couchbase running on {@code localhost} on {@code 8091}.
*
* @return the test rule.
*/
public static CouchbaseAvailableRule onLocalhost() {
return new CouchbaseAvailableRule("localhost", 8091);
}
@Override
protected void before() throws Throwable {
check((b, throwable) -> {
if (throwable != null) {
throw new AssumptionViolatedException(
String.format("Couchbase not available on on %s:%d. Skipping tests.", host, port), throwable);
}
});
}
public boolean isAvailable() {
AtomicBoolean b = new AtomicBoolean();
check((avail, t) -> b.set(avail));
return b.get();
}
private void check(BiConsumer<Boolean, Throwable> c) {
try (Socket socket = SocketFactory.getDefault().createSocket()) {
socket.connect(new InetSocketAddress(host, port), Math.toIntExact(timeout.toMillis()));
c.accept(true, null);
} catch (IOException e) {
c.accept(false, e);
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2020-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
*
* https://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.couchbase.util;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* {@code @EnabledOnCouchbaseAvailable} is used to signal that the annotated test class or test method is only <em>enabled</em>
* if Couchbase is running.
* <p/>
* When applied at the class level, all test methods within that class will be enabled.
*
* @author Mark Paluch
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@ExtendWith(EnabledOnCouchbaseCondition.class)
public @interface EnabledOnCouchbaseAvailable {
/**
* Couchbase host.
*/
String host() default "localhost";
/**
* Couchbase port number.
*/
int port() default 8091;
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2020-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
*
* https://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.couchbase.util;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
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.util.AnnotationUtils;
/**
* {@link ExecutionCondition} for {@link EnabledOnCouchbaseCondition @EnabledOnCouchbaseAvailable}.
*
* @author Mark Paluch
* @see EnabledOnCouchbaseCondition
*/
class EnabledOnCouchbaseCondition implements ExecutionCondition {
private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@EnabledOnCouchbaseAvailable is not present");
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
var optional = AnnotationUtils.findAnnotation(context.getElement(), EnabledOnCouchbaseAvailable.class);
if (optional.isEmpty()) {
return ENABLED_BY_DEFAULT;
}
var annotation = optional.get();
try (var socket = new Socket()) {
socket.connect(new InetSocketAddress(annotation.host(), annotation.port()), 100);
return enabled(String.format("Connection successful to Couchbase at %s:%d", annotation.host(), annotation.port()));
} catch (IOException e) {
return disabled(
String.format("Cannot connect to Couchbase at %s:%d (%s)", annotation.host(), annotation.port(), e));
}
}
}