Skip to content

Commit

Permalink
GH-1190: Remove reference to Junit4 Assume
Browse files Browse the repository at this point in the history
Resolves #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**
  • Loading branch information
garyrussell authored and artembilan committed May 5, 2020
1 parent 9ccd52a commit 568854b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -100,7 +98,7 @@ public static void clearEnvironmentVariableOverrides() {
* @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);
}

/**
Expand All @@ -121,7 +119,7 @@ public static BrokerRunning isNotRunning() {
* @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);
}

/**
Expand All @@ -130,28 +128,27 @@ public static BrokerRunning isBrokerAndManagementRunning() {
* 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);
}

/**
Expand Down Expand Up @@ -269,22 +266,15 @@ public String getAdminPassword() {

@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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,6 @@

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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -154,7 +148,7 @@ public static void clearEnvironmentVariableOverrides() {
* @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);
}

/**
Expand All @@ -175,7 +169,7 @@ public static BrokerRunningSupport isNotRunning() {
* @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);
}

/**
Expand All @@ -184,15 +178,14 @@ public static BrokerRunningSupport isBrokerAndManagementRunning() {
* 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);
}
Expand All @@ -206,29 +199,15 @@ private BrokerRunningSupport(boolean assumeOnline, boolean purge, String... queu
: 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);
}
}

/**
Expand Down Expand Up @@ -339,18 +318,15 @@ public void setPurgeAfterEach(boolean purgeAfterEach) {
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())
Expand All @@ -361,16 +337,8 @@ public void test() {
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);
Expand Down Expand Up @@ -403,11 +371,6 @@ private Channel createQueues(Connection connection) throws IOException, URISynta
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("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -32,6 +32,8 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -35,6 +35,8 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -35,6 +35,8 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* JUnit test examples.
*/
package org.springframework.amqp.rabbit.test.examples;

0 comments on commit 568854b

Please sign in to comment.