diff --git a/binders/rabbit-binder/pom.xml b/binders/rabbit-binder/pom.xml index db4a86a79..3cc3b0d9f 100644 --- a/binders/rabbit-binder/pom.xml +++ b/binders/rabbit-binder/pom.xml @@ -50,6 +50,13 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + true + + org.apache.maven.plugins maven-compiler-plugin diff --git a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/pom.xml b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/pom.xml index 585b19548..9acb7c504 100644 --- a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/pom.xml +++ b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/pom.xml @@ -9,11 +9,6 @@ spring-cloud-stream-binder-rabbit-test-support Rabbit related test classes - - org.springframework.cloud - spring-cloud-stream-test-support-internal - compile - org.springframework.boot spring-boot-starter-logging @@ -23,5 +18,15 @@ spring-boot-starter-amqp true + + org.springframework.boot + spring-boot-starter-test + + + org.junit.jupiter + junit-jupiter + compile + + diff --git a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/AbstractExternalResourceTestSupport.java b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/AbstractExternalResourceTestSupport.java new file mode 100644 index 000000000..2566a5b58 --- /dev/null +++ b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/AbstractExternalResourceTestSupport.java @@ -0,0 +1,159 @@ +/* + * Copyright 2013-2022 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 org.springframework.cloud.stream.binder.test.junit.rabbit; + + + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import org.springframework.util.Assert; + +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Abstract base class for JUnit {@link Rule}s that detect the presence of some external + * resource. If the resource is indeed present, it will be available during the test + * lifecycle through {@link #getResource()}. If it is not, tests will either fail or be + * skipped, depending on the value of system property + * {@value #SCS_EXTERNAL_SERVERS_REQUIRED}. + * + * @param resource type + * @author Eric Bottard + * @author Gary Russell + */ +public abstract class AbstractExternalResourceTestSupport implements BeforeEachCallback { + + /** + * SCS external servers required environment variable. + */ + public static final String SCS_EXTERNAL_SERVERS_REQUIRED = "SCS_EXTERNAL_SERVERS_REQUIRED"; + + protected final Log logger = LogFactory.getLog(getClass()); + + protected R resource; + + private String resourceDescription; + + protected AbstractExternalResourceTestSupport(String resourceDescription) { + Assert.hasText(resourceDescription, "resourceDescription is required"); + this.resourceDescription = resourceDescription; + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + try { + obtainResource(); + } + catch (Exception e) { + maybeCleanup(); + +// failOrSkip(e); + fail(); + } + } + +// @Override +// public Statement apply(final Statement base, Description description) { +// try { +// obtainResource(); +// } +// catch (Exception e) { +// maybeCleanup(); +// +// return failOrSkip(e); +// } +// +// return new Statement() { +// +// @Override +// public void evaluate() throws Throwable { +// try { +// base.evaluate(); +// } +// finally { +// try { +// cleanupResource(); +// } +// catch (Exception ignored) { +// AbstractExternalResourceTestSupport.this.logger.warn( +// "Exception while trying to cleanup proper resource", +// ignored); +// } +// } +// } +// +// }; +// } + +// private Statement failOrSkip(final Exception e) { +// String serversRequired = System.getenv(SCS_EXTERNAL_SERVERS_REQUIRED); +// if ("true".equalsIgnoreCase(serversRequired)) { +// this.logger.error(this.resourceDescription + " IS REQUIRED BUT NOT AVAILABLE", +// e); +// fail(this.resourceDescription + " IS NOT AVAILABLE"); +// // Never reached, here to satisfy method signature +// return null; +// } +// else { +// this.logger.error( +// this.resourceDescription + " IS NOT AVAILABLE, SKIPPING TESTS", e); +// return new Statement() { +// +// @Override +// public void evaluate() throws Throwable { +// Assume.assumeTrue("Skipping test due to " +// + AbstractExternalResourceTestSupport.this.resourceDescription +// + " not being available " + e, false); +// } +// }; +// } +// } + + private void maybeCleanup() { + if (this.resource != null) { + try { + cleanupResource(); + } + catch (Exception ignored) { + this.logger.warn("Exception while trying to cleanup failed resource", + ignored); + } + } + } + + public R getResource() { + return this.resource; + } + + /** + * Perform cleanup of the {@link #resource} field, which is guaranteed to be non null. + * @throws Exception any exception thrown by this method will be logged and swallowed + */ + protected abstract void cleanupResource() throws Exception; + + /** + * Try to obtain and validate a resource. Implementors should either set the + * {@link #resource} field with a valid resource and return normally, or throw an + * exception. + * @throws Exception when resource couldn't be obtained + */ + protected abstract void obtainResource() throws Exception; + +} diff --git a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/RabbitTestSupport.java b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/RabbitTestSupport.java index e44cadf8a..37c59f69a 100644 --- a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/RabbitTestSupport.java +++ b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit-test-support/src/main/java/org/springframework/cloud/stream/binder/test/junit/rabbit/RabbitTestSupport.java @@ -31,7 +31,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; -import org.springframework.cloud.stream.test.junit.AbstractExternalResourceTestSupport; /** * JUnit {@link org.junit.Rule} that detects the fact that RabbitMQ is available on diff --git a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit/pom.xml b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit/pom.xml index 36ece8895..28b88b37b 100644 --- a/binders/rabbit-binder/spring-cloud-stream-binder-rabbit/pom.xml +++ b/binders/rabbit-binder/spring-cloud-stream-binder-rabbit/pom.xml @@ -80,11 +80,6 @@ spring-cloud-stream-binder-test test - - org.springframework.cloud - spring-cloud-stream-test-support-internal - test - org.springframework.cloud spring-cloud-stream-binder-rabbit-test-support @@ -102,15 +97,4 @@ httpclient - - - - - - - - - - - diff --git a/pom.xml b/pom.xml index a39434452..c98a11567 100644 --- a/pom.xml +++ b/pom.xml @@ -47,11 +47,6 @@ pom import - - org.springframework.cloud - spring-cloud-stream-test-support-internal - ${project.version} - org.springframework.boot spring-boot-actuator