GH-1190: Remove reference to Junit4 Assume

Resolves https://github.com/spring-projects/spring-amqp/issues/1190

- Remove reference to `Assume` in `BrokerRunningSupport`
- Move example test cases to a new package so they can be easily copied/pasted
- Remove `assumeOnline` field - it looks like it was intended to support running
  tests only if RabbitMQ is NOT running; but there was never any way to set it
  to false

**cherry-pick to 2.2.x**
This commit is contained in:
Gary Russell
2020-05-04 16:47:10 -04:00
committed by Artem Bilan
parent 1572ea8d75
commit cc7270c190
8 changed files with 50 additions and 86 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -70,8 +70,6 @@ public final class BrokerRunning extends TestWatcher {
private final BrokerRunningSupport brokerRunning;
private final boolean assumeOnline;
/**
* Set environment variable overrides for host, port etc. Will override any real
* environment variables, if present.
@@ -100,7 +98,7 @@ public final class BrokerRunning extends TestWatcher {
* @return a new rule that assumes an existing running broker
*/
public static BrokerRunning isRunningWithEmptyQueues(String... names) {
return new BrokerRunning(true, true, names);
return new BrokerRunning(true, names);
}
/**
@@ -121,7 +119,7 @@ public final class BrokerRunning extends TestWatcher {
* @return a new rule that assumes an existing broker with the management plugin
*/
public static BrokerRunning isBrokerAndManagementRunning() {
return new BrokerRunning(true, false, true);
return new BrokerRunning(false, true);
}
/**
@@ -130,28 +128,27 @@ public final class BrokerRunning extends TestWatcher {
* the provided queues declared (and emptied if needed)..
*/
public static BrokerRunning isBrokerAndManagementRunningWithEmptyQueues(String...queues) {
return new BrokerRunning(true, false, true, queues);
return new BrokerRunning(true, true, queues);
}
private BrokerRunning(boolean assumeOnline, boolean purge, String... queues) {
this(assumeOnline, purge, false, queues);
private BrokerRunning(boolean purge, String... queues) {
this(purge, false, queues);
}
private BrokerRunning(boolean assumeOnline, boolean purge, boolean management, String... queues) {
this.assumeOnline = assumeOnline;
this.brokerRunning = new BrokerRunningSupport(assumeOnline, purge, management, queues);
private BrokerRunning(boolean purge, boolean management, String... queues) {
this.brokerRunning = new BrokerRunningSupport(purge, management, queues);
}
private BrokerRunning(boolean assumeOnline, String... queues) {
this(assumeOnline, false, queues);
private BrokerRunning(String... queues) {
this(false, queues);
}
private BrokerRunning(boolean assumeOnline) {
this(assumeOnline, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
private BrokerRunning() {
this(BrokerRunningSupport.DEFAULT_QUEUE_NAME);
}
private BrokerRunning(boolean assumeOnline, boolean purge, boolean management) {
this(assumeOnline, purge, management, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
private BrokerRunning(boolean purge, boolean management) {
this(purge, management, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
}
/**
@@ -269,22 +266,15 @@ public final class BrokerRunning extends TestWatcher {
@Override
public Statement apply(Statement base, Description description) {
try {
this.brokerRunning.test();
}
catch (BrokerNotAliveException e) {
LOGGER.warn("Not executing tests because basic connectivity test failed: " + e.getMessage());
if (this.assumeOnline) {
if (fatal()) {
fail("RabbitMQ Broker is required, but not available");
}
else {
Assume.assumeNoException(e);
}
if (fatal()) {
fail("RabbitMQ Broker is required, but not available");
}
}
return super.apply(base, description);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -29,7 +29,6 @@ import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assume;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
@@ -83,13 +82,8 @@ public final class BrokerRunningSupport {
// Static so that we only test once on failure: speeds up test suite
private static final Map<Integer, Boolean> BROKER_ONLINE = new HashMap<>();
// Static so that we only test once on failure
private static final Map<Integer, Boolean> BROKER_OFFLINE = new HashMap<>();
private static final Map<String, String> ENVIRONMENT_OVERRIDES = new HashMap<>();
private final boolean assumeOnline;
private final boolean purge;
private final boolean management;
@@ -154,7 +148,7 @@ public final class BrokerRunningSupport {
* @return a new rule that assumes an existing running broker
*/
public static BrokerRunningSupport isRunningWithEmptyQueues(String... names) {
return new BrokerRunningSupport(true, true, names);
return new BrokerRunningSupport(true, names);
}
/**
@@ -175,7 +169,7 @@ public final class BrokerRunningSupport {
* @return a new rule that assumes an existing broker with the management plugin
*/
public static BrokerRunningSupport isBrokerAndManagementRunning() {
return new BrokerRunningSupport(true, false, true);
return new BrokerRunningSupport(false, true);
}
/**
@@ -184,15 +178,14 @@ public final class BrokerRunningSupport {
* the provided queues declared (and emptied if needed)..
*/
public static BrokerRunningSupport isBrokerAndManagementRunningWithEmptyQueues(String...queues) {
return new BrokerRunningSupport(true, false, true, queues);
return new BrokerRunningSupport(true, true, queues);
}
private BrokerRunningSupport(boolean assumeOnline, boolean purge, String... queues) {
this(assumeOnline, purge, false, queues);
private BrokerRunningSupport(boolean purge, String... queues) {
this(purge, false, queues);
}
BrokerRunningSupport(boolean assumeOnline, boolean purge, boolean management, String... queues) {
this.assumeOnline = assumeOnline;
BrokerRunningSupport(boolean purge, boolean management, String... queues) {
if (queues != null) {
this.queues = Arrays.copyOf(queues, queues.length);
}
@@ -206,29 +199,15 @@ public final class BrokerRunningSupport {
: Integer.valueOf(fromEnvironment(BROKER_PORT, null)));
}
private BrokerRunningSupport(boolean assumeOnline, String... queues) {
this(assumeOnline, false, queues);
}
private BrokerRunningSupport(boolean assumeOnline) {
this(assumeOnline, DEFAULT_QUEUE_NAME);
}
private BrokerRunningSupport(boolean assumeOnline, boolean purge, boolean management) {
this(assumeOnline, purge, management, DEFAULT_QUEUE_NAME);
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
if (!BROKER_OFFLINE.containsKey(port)) {
BROKER_OFFLINE.put(port, true);
}
if (!BROKER_ONLINE.containsKey(port)) {
BROKER_ONLINE.put(port, true);
}
}
/**
@@ -339,18 +318,15 @@ public final class BrokerRunningSupport {
this.purgeAfterEach = purgeAfterEach;
}
public void test() {
/**
* Check connectivity to the broker and create any queues.
* @throws BrokerNotAliveException if the broker is not available.
*/
public void test() throws BrokerNotAliveException {
// Check at the beginning, so this can be used as a static field
if (this.assumeOnline) {
if (Boolean.FALSE.equals(BROKER_ONLINE.get(this.port))) {
throw new BrokerNotAliveException("Require broker online and it's not");
}
}
else {
if (Boolean.FALSE.equals(BROKER_OFFLINE.get(this.port))) {
throw new BrokerNotAliveException("Require broker offline and it's not");
}
if (Boolean.FALSE.equals(BROKER_ONLINE.get(this.port))) {
throw new BrokerNotAliveException("Require broker online and it's not");
}
Connection connection = null; // NOSONAR (closeResources())
@@ -361,16 +337,8 @@ public final class BrokerRunningSupport {
channel = createQueues(connection);
}
catch (Exception e) {
LOGGER.warn("Not executing tests because basic connectivity test failed: " + e.getMessage());
BROKER_ONLINE.put(this.port, false);
if (this.assumeOnline) {
if (fatal()) {
throw new BrokerNotAliveException("RabbitMQ Broker is required, but not available", e);
}
else {
Assume.assumeNoException(e);
}
}
throw new BrokerNotAliveException("RabbitMQ Broker is required, but not available", e);
}
finally {
closeResources(connection, channel);
@@ -403,11 +371,6 @@ public final class BrokerRunningSupport {
channel.queueDeclare(queueName, true, false, false, null);
}
}
BROKER_OFFLINE.put(this.port, false);
if (!this.assumeOnline) {
Assume.assumeTrue(BROKER_OFFLINE.get(this.port));
}
if (this.management) {
Client client = new Client(getAdminUri(), this.adminUser, this.adminPassword);
if (!client.alivenessTest("/")) {

View File

@@ -84,7 +84,7 @@ public class RabbitAvailableCondition
if (BrokerRunningSupport.fatal()) {
throw new IllegalStateException("Required RabbitMQ is not available", e);
}
return ConditionEvaluationResult.disabled("RabbitMQ is not available");
return ConditionEvaluationResult.disabled("Tests Ignored: RabbitMQ is not available");
}
}
return ENABLED;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.amqp.rabbit.test;
package org.springframework.amqp.rabbit.test.examples;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,6 +32,8 @@ import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.InvocationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.amqp.rabbit.test;
package org.springframework.amqp.rabbit.test.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
@@ -35,6 +35,8 @@ import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.InvocationData;
import org.springframework.amqp.rabbit.test.mockito.LatchCountDownAndCallRealMethodAnswer;
import org.springframework.beans.factory.annotation.Autowired;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.amqp.rabbit.test;
package org.springframework.amqp.rabbit.test.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
@@ -35,6 +35,8 @@ import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
import org.springframework.amqp.rabbit.test.mockito.LatchCountDownAndCallRealMethodAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.amqp.rabbit.test;
package org.springframework.amqp.rabbit.test.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -31,6 +31,7 @@ import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.test.TestRabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -0,0 +1,4 @@
/**
* JUnit test examples.
*/
package org.springframework.amqp.rabbit.test.examples;