Skip to content

Commit 568854b

Browse files
garyrussellartembilan
authored andcommitted
GH-1190: Remove reference to Junit4 Assume
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**
1 parent 9ccd52a commit 568854b

File tree

8 files changed

+50
-86
lines changed

8 files changed

+50
-86
lines changed

spring-rabbit-junit/src/main/java/org/springframework/amqp/rabbit/junit/BrokerRunning.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,8 +70,6 @@ public final class BrokerRunning extends TestWatcher {
7070

7171
private final BrokerRunningSupport brokerRunning;
7272

73-
private final boolean assumeOnline;
74-
7573
/**
7674
* Set environment variable overrides for host, port etc. Will override any real
7775
* environment variables, if present.
@@ -100,7 +98,7 @@ public static void clearEnvironmentVariableOverrides() {
10098
* @return a new rule that assumes an existing running broker
10199
*/
102100
public static BrokerRunning isRunningWithEmptyQueues(String... names) {
103-
return new BrokerRunning(true, true, names);
101+
return new BrokerRunning(true, names);
104102
}
105103

106104
/**
@@ -121,7 +119,7 @@ public static BrokerRunning isNotRunning() {
121119
* @return a new rule that assumes an existing broker with the management plugin
122120
*/
123121
public static BrokerRunning isBrokerAndManagementRunning() {
124-
return new BrokerRunning(true, false, true);
122+
return new BrokerRunning(false, true);
125123
}
126124

127125
/**
@@ -130,28 +128,27 @@ public static BrokerRunning isBrokerAndManagementRunning() {
130128
* the provided queues declared (and emptied if needed)..
131129
*/
132130
public static BrokerRunning isBrokerAndManagementRunningWithEmptyQueues(String...queues) {
133-
return new BrokerRunning(true, false, true, queues);
131+
return new BrokerRunning(true, true, queues);
134132
}
135133

136-
private BrokerRunning(boolean assumeOnline, boolean purge, String... queues) {
137-
this(assumeOnline, purge, false, queues);
134+
private BrokerRunning(boolean purge, String... queues) {
135+
this(purge, false, queues);
138136
}
139137

140-
private BrokerRunning(boolean assumeOnline, boolean purge, boolean management, String... queues) {
141-
this.assumeOnline = assumeOnline;
142-
this.brokerRunning = new BrokerRunningSupport(assumeOnline, purge, management, queues);
138+
private BrokerRunning(boolean purge, boolean management, String... queues) {
139+
this.brokerRunning = new BrokerRunningSupport(purge, management, queues);
143140
}
144141

145-
private BrokerRunning(boolean assumeOnline, String... queues) {
146-
this(assumeOnline, false, queues);
142+
private BrokerRunning(String... queues) {
143+
this(false, queues);
147144
}
148145

149-
private BrokerRunning(boolean assumeOnline) {
150-
this(assumeOnline, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
146+
private BrokerRunning() {
147+
this(BrokerRunningSupport.DEFAULT_QUEUE_NAME);
151148
}
152149

153-
private BrokerRunning(boolean assumeOnline, boolean purge, boolean management) {
154-
this(assumeOnline, purge, management, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
150+
private BrokerRunning(boolean purge, boolean management) {
151+
this(purge, management, BrokerRunningSupport.DEFAULT_QUEUE_NAME);
155152
}
156153

157154
/**
@@ -269,22 +266,15 @@ public String getAdminPassword() {
269266

270267
@Override
271268
public Statement apply(Statement base, Description description) {
272-
273269
try {
274270
this.brokerRunning.test();
275271
}
276272
catch (BrokerNotAliveException e) {
277273
LOGGER.warn("Not executing tests because basic connectivity test failed: " + e.getMessage());
278-
if (this.assumeOnline) {
279-
if (fatal()) {
280-
fail("RabbitMQ Broker is required, but not available");
281-
}
282-
else {
283-
Assume.assumeNoException(e);
284-
}
274+
if (fatal()) {
275+
fail("RabbitMQ Broker is required, but not available");
285276
}
286277
}
287-
288278
return super.apply(base, description);
289279
}
290280

spring-rabbit-junit/src/main/java/org/springframework/amqp/rabbit/junit/BrokerRunningSupport.java

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929

3030
import org.apache.commons.logging.Log;
3131
import org.apache.commons.logging.LogFactory;
32-
import org.junit.Assume;
3332

3433
import org.springframework.util.Base64Utils;
3534
import org.springframework.util.StringUtils;
@@ -83,13 +82,8 @@ public final class BrokerRunningSupport {
8382
// Static so that we only test once on failure: speeds up test suite
8483
private static final Map<Integer, Boolean> BROKER_ONLINE = new HashMap<>();
8584

86-
// Static so that we only test once on failure
87-
private static final Map<Integer, Boolean> BROKER_OFFLINE = new HashMap<>();
88-
8985
private static final Map<String, String> ENVIRONMENT_OVERRIDES = new HashMap<>();
9086

91-
private final boolean assumeOnline;
92-
9387
private final boolean purge;
9488

9589
private final boolean management;
@@ -154,7 +148,7 @@ public static void clearEnvironmentVariableOverrides() {
154148
* @return a new rule that assumes an existing running broker
155149
*/
156150
public static BrokerRunningSupport isRunningWithEmptyQueues(String... names) {
157-
return new BrokerRunningSupport(true, true, names);
151+
return new BrokerRunningSupport(true, names);
158152
}
159153

160154
/**
@@ -175,7 +169,7 @@ public static BrokerRunningSupport isNotRunning() {
175169
* @return a new rule that assumes an existing broker with the management plugin
176170
*/
177171
public static BrokerRunningSupport isBrokerAndManagementRunning() {
178-
return new BrokerRunningSupport(true, false, true);
172+
return new BrokerRunningSupport(false, true);
179173
}
180174

181175
/**
@@ -184,15 +178,14 @@ public static BrokerRunningSupport isBrokerAndManagementRunning() {
184178
* the provided queues declared (and emptied if needed)..
185179
*/
186180
public static BrokerRunningSupport isBrokerAndManagementRunningWithEmptyQueues(String...queues) {
187-
return new BrokerRunningSupport(true, false, true, queues);
181+
return new BrokerRunningSupport(true, true, queues);
188182
}
189183

190-
private BrokerRunningSupport(boolean assumeOnline, boolean purge, String... queues) {
191-
this(assumeOnline, purge, false, queues);
184+
private BrokerRunningSupport(boolean purge, String... queues) {
185+
this(purge, false, queues);
192186
}
193187

194-
BrokerRunningSupport(boolean assumeOnline, boolean purge, boolean management, String... queues) {
195-
this.assumeOnline = assumeOnline;
188+
BrokerRunningSupport(boolean purge, boolean management, String... queues) {
196189
if (queues != null) {
197190
this.queues = Arrays.copyOf(queues, queues.length);
198191
}
@@ -206,29 +199,15 @@ private BrokerRunningSupport(boolean assumeOnline, boolean purge, String... queu
206199
: Integer.valueOf(fromEnvironment(BROKER_PORT, null)));
207200
}
208201

209-
private BrokerRunningSupport(boolean assumeOnline, String... queues) {
210-
this(assumeOnline, false, queues);
211-
}
212-
213202
private BrokerRunningSupport(boolean assumeOnline) {
214203
this(assumeOnline, DEFAULT_QUEUE_NAME);
215204
}
216205

217-
private BrokerRunningSupport(boolean assumeOnline, boolean purge, boolean management) {
218-
this(assumeOnline, purge, management, DEFAULT_QUEUE_NAME);
219-
}
220-
221206
/**
222207
* @param port the port to set
223208
*/
224209
public void setPort(int port) {
225210
this.port = port;
226-
if (!BROKER_OFFLINE.containsKey(port)) {
227-
BROKER_OFFLINE.put(port, true);
228-
}
229-
if (!BROKER_ONLINE.containsKey(port)) {
230-
BROKER_ONLINE.put(port, true);
231-
}
232211
}
233212

234213
/**
@@ -339,18 +318,15 @@ public void setPurgeAfterEach(boolean purgeAfterEach) {
339318
this.purgeAfterEach = purgeAfterEach;
340319
}
341320

342-
public void test() {
321+
/**
322+
* Check connectivity to the broker and create any queues.
323+
* @throws BrokerNotAliveException if the broker is not available.
324+
*/
325+
public void test() throws BrokerNotAliveException {
343326

344327
// Check at the beginning, so this can be used as a static field
345-
if (this.assumeOnline) {
346-
if (Boolean.FALSE.equals(BROKER_ONLINE.get(this.port))) {
347-
throw new BrokerNotAliveException("Require broker online and it's not");
348-
}
349-
}
350-
else {
351-
if (Boolean.FALSE.equals(BROKER_OFFLINE.get(this.port))) {
352-
throw new BrokerNotAliveException("Require broker offline and it's not");
353-
}
328+
if (Boolean.FALSE.equals(BROKER_ONLINE.get(this.port))) {
329+
throw new BrokerNotAliveException("Require broker online and it's not");
354330
}
355331

356332
Connection connection = null; // NOSONAR (closeResources())
@@ -361,16 +337,8 @@ public void test() {
361337
channel = createQueues(connection);
362338
}
363339
catch (Exception e) {
364-
LOGGER.warn("Not executing tests because basic connectivity test failed: " + e.getMessage());
365340
BROKER_ONLINE.put(this.port, false);
366-
if (this.assumeOnline) {
367-
if (fatal()) {
368-
throw new BrokerNotAliveException("RabbitMQ Broker is required, but not available", e);
369-
}
370-
else {
371-
Assume.assumeNoException(e);
372-
}
373-
}
341+
throw new BrokerNotAliveException("RabbitMQ Broker is required, but not available", e);
374342
}
375343
finally {
376344
closeResources(connection, channel);
@@ -403,11 +371,6 @@ private Channel createQueues(Connection connection) throws IOException, URISynta
403371
channel.queueDeclare(queueName, true, false, false, null);
404372
}
405373
}
406-
BROKER_OFFLINE.put(this.port, false);
407-
if (!this.assumeOnline) {
408-
Assume.assumeTrue(BROKER_OFFLINE.get(this.port));
409-
}
410-
411374
if (this.management) {
412375
Client client = new Client(getAdminUri(), this.adminUser, this.adminPassword);
413376
if (!client.alivenessTest("/")) {

spring-rabbit-junit/src/main/java/org/springframework/amqp/rabbit/junit/RabbitAvailableCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
8484
if (BrokerRunningSupport.fatal()) {
8585
throw new IllegalStateException("Required RabbitMQ is not available", e);
8686
}
87-
return ConditionEvaluationResult.disabled("RabbitMQ is not available");
87+
return ConditionEvaluationResult.disabled("Tests Ignored: RabbitMQ is not available");
8888
}
8989
}
9090
return ENABLED;

spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/ExampleRabbitListenerCaptureTest.java renamed to spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerCaptureTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.amqp.rabbit.test;
17+
package org.springframework.amqp.rabbit.test.examples;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

@@ -32,6 +32,8 @@
3232
import org.springframework.amqp.rabbit.core.RabbitAdmin;
3333
import org.springframework.amqp.rabbit.core.RabbitTemplate;
3434
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
35+
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
36+
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
3537
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.InvocationData;
3638
import org.springframework.beans.factory.annotation.Autowired;
3739
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.amqp.rabbit.test;
17+
package org.springframework.amqp.rabbit.test.examples;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.ArgumentMatchers.anyString;
@@ -35,6 +35,8 @@
3535
import org.springframework.amqp.rabbit.core.RabbitAdmin;
3636
import org.springframework.amqp.rabbit.core.RabbitTemplate;
3737
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
38+
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
39+
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
3840
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.InvocationData;
3941
import org.springframework.amqp.rabbit.test.mockito.LatchCountDownAndCallRealMethodAnswer;
4042
import org.springframework.beans.factory.annotation.Autowired;

spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/ExampleRabbitListenerSpyTest.java renamed to spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.amqp.rabbit.test;
17+
package org.springframework.amqp.rabbit.test.examples;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.ArgumentMatchers.anyString;
@@ -35,6 +35,8 @@
3535
import org.springframework.amqp.rabbit.core.RabbitAdmin;
3636
import org.springframework.amqp.rabbit.core.RabbitTemplate;
3737
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
38+
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
39+
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
3840
import org.springframework.amqp.rabbit.test.mockito.LatchCountDownAndCallRealMethodAnswer;
3941
import org.springframework.beans.factory.annotation.Autowired;
4042
import org.springframework.context.annotation.Bean;

spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/TestRabbitTemplateTests.java renamed to spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/TestRabbitTemplateTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.amqp.rabbit.test;
17+
package org.springframework.amqp.rabbit.test.examples;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -31,6 +31,7 @@
3131
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
3232
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
3333
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
34+
import org.springframework.amqp.rabbit.test.TestRabbitTemplate;
3435
import org.springframework.beans.factory.annotation.Autowired;
3536
import org.springframework.context.annotation.Bean;
3637
import org.springframework.context.annotation.Configuration;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* JUnit test examples.
3+
*/
4+
package org.springframework.amqp.rabbit.test.examples;

0 commit comments

Comments
 (0)