Skip to content

Commit 9ca362d

Browse files
authored
Merge pull request #920 from rabbitmq/junit-5-upgrade
Upgrade to JUnit 5
2 parents 728c8a7 + 41f0804 commit 9ca362d

File tree

169 files changed

+1493
-1444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+1493
-1444
lines changed

.github/workflows/test-supported-java-versions-5.x.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ jobs:
4646
./mvnw verify -P '!setup-test-cluster,use-nio' -Drabbitmqctl.bin=DOCKER:rabbitmq \
4747
-Dtest-broker.A.nodename=rabbit@$(hostname) -Dmaven.javadoc.skip=true \
4848
-Dtest-client-cert.password= -Dtest-tls-certs.dir=rabbitmq-configuration/tls \
49-
-Dit.test=ClientTests,FunctionalTests,ServerTests,SSLTests \
49+
-Dit.test=ClientTestSuite,FunctionalTestSuite,ServerTestSuite,SslTestSuite \
5050
--no-transfer-progress
5151
- name: Test with blocking IO
5252
run: |
5353
./mvnw verify -P '!setup-test-cluster' -Drabbitmqctl.bin=DOCKER:rabbitmq \
5454
-Dtest-broker.A.nodename=rabbit@$(hostname) -Dmaven.javadoc.skip=true \
5555
-Dtest-client-cert.password= -Dtest-tls-certs.dir=rabbitmq-configuration/tls \
56-
-Dit.test=ClientTests,FunctionalTests,ServerTests,SSLTests \
56+
-Dit.test=ClientTestSuite,FunctionalTestSuite,ServerTestSuite,SslTestSuite \
5757
--no-transfer-progress
5858
- name: Stop broker
5959
run: docker stop rabbitmq && docker rm rabbitmq

.github/workflows/test-supported-java-versions-main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ jobs:
4444
./mvnw verify -P '!setup-test-cluster,use-nio' -Drabbitmqctl.bin=DOCKER:rabbitmq \
4545
-Dtest-broker.A.nodename=rabbit@$(hostname) -Dmaven.javadoc.skip=true \
4646
-Dtest-client-cert.password= -Dtest-tls-certs.dir=rabbitmq-configuration/tls \
47-
-Dit.test=ClientTests,FunctionalTests,ServerTests,SSLTests \
47+
-Dit.test=ClientTestSuite,FunctionalTestSuite,ServerTestSuite,SslTestSuite \
4848
--no-transfer-progress
4949
- name: Test with blocking IO
5050
run: |
5151
./mvnw verify -P '!setup-test-cluster' -Drabbitmqctl.bin=DOCKER:rabbitmq \
5252
-Dtest-broker.A.nodename=rabbit@$(hostname) -Dmaven.javadoc.skip=true \
5353
-Dtest-client-cert.password= -Dtest-tls-certs.dir=rabbitmq-configuration/tls \
54-
-Dit.test=ClientTests,FunctionalTests,ServerTests,SSLTests \
54+
-Dit.test=ClientTestSuite,FunctionalTestSuite,ServerTestSuite,SslTestSuite \
5555
--no-transfer-progress
5656
- name: Stop broker
5757
run: docker stop rabbitmq && docker rm rabbitmq

pom.xml

+33-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<opentelemetry.version>1.21.0</opentelemetry.version>
6161
<jackson.version>2.14.1</jackson.version>
6262
<logback.version>1.2.11</logback.version>
63-
<junit.version>4.13.2</junit.version>
63+
<junit.jupiter.version>5.9.1</junit.jupiter.version>
6464
<mockito.version>4.11.0</mockito.version>
6565
<assertj.version>3.24.0</assertj.version>
6666
<jetty.version>9.4.50.v20221201</jetty.version>
@@ -77,8 +77,8 @@
7777
<keytool.maven.plugin.version>1.6</keytool.maven.plugin.version>
7878
<build.helper.maven-plugin.version>3.3.0</build.helper.maven-plugin.version>
7979
<maven.compiler.plugin.version>3.10.1</maven.compiler.plugin.version>
80-
<maven.surefire.plugin.version>2.22.2</maven.surefire.plugin.version>
81-
<maven.failsafe.plugin.version>2.22.2</maven.failsafe.plugin.version>
80+
<maven.surefire.plugin.version>3.0.0-M7</maven.surefire.plugin.version>
81+
<maven.failsafe.plugin.version>3.0.0-M7</maven.failsafe.plugin.version>
8282
<maven.gpg.plugin.version>3.0.1</maven.gpg.plugin.version>
8383
<maven.jar.plugin.version>3.3.0</maven.jar.plugin.version>
8484
<maven.bundle.plugin.version>5.1.8</maven.bundle.plugin.version>
@@ -422,11 +422,11 @@
422422
</environmentVariables>
423423
<failIfNoTests>true</failIfNoTests>
424424
<includes>
425-
<include>**/ClientTests.*</include>
426-
<include>**/FunctionalTests.*</include>
427-
<include>**/SSLTests.*</include>
428-
<include>**/ServerTests.*</include>
429-
<include>**/HATests.*</include>
425+
<include>**/ClientTestSuite.*</include>
426+
<include>**/FunctionalTestSuite.*</include>
427+
<include>**/SslTestSuite.*</include>
428+
<include>**/ServerTestSuite.*</include>
429+
<include>**/HaTestSuite.*</include>
430430
</includes>
431431
<!--
432432
needed because of bug in OpenJDK 8 u181 on Debian distros
@@ -718,9 +718,19 @@
718718
<optional>true</optional>
719719
</dependency>
720720
<dependency>
721-
<groupId>junit</groupId>
722-
<artifactId>junit</artifactId>
723-
<version>${junit.version}</version>
721+
<groupId>org.junit.jupiter</groupId>
722+
<artifactId>junit-jupiter-engine</artifactId>
723+
<scope>test</scope>
724+
</dependency>
725+
<dependency>
726+
<groupId>org.junit.platform</groupId>
727+
<artifactId>junit-platform-suite</artifactId>
728+
<scope>test</scope>
729+
</dependency>
730+
<!-- for parameterized tests -->
731+
<dependency>
732+
<groupId>org.junit.jupiter</groupId>
733+
<artifactId>junit-jupiter-params</artifactId>
724734
<scope>test</scope>
725735
</dependency>
726736
<dependency>
@@ -768,6 +778,18 @@
768778

769779
</dependencies>
770780

781+
782+
<dependencyManagement>
783+
<dependencies>
784+
<dependency>
785+
<groupId>org.junit</groupId>
786+
<artifactId>junit-bom</artifactId>
787+
<version>${junit.jupiter.version}</version>
788+
<type>pom</type>
789+
<scope>import</scope>
790+
</dependency>
791+
</dependencies>
792+
</dependencyManagement>
771793
<build>
772794

773795
<pluginManagement>

src/test/java/com/rabbitmq/client/AbstractJsonRpcTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2018-2023 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -19,8 +19,8 @@
1919
import com.rabbitmq.tools.jsonrpc.JsonRpcClient;
2020
import com.rabbitmq.tools.jsonrpc.JsonRpcMapper;
2121
import com.rabbitmq.tools.jsonrpc.JsonRpcServer;
22-
import org.junit.After;
23-
import org.junit.Before;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
2424

2525
import java.util.Date;
2626

@@ -35,7 +35,7 @@ public abstract class AbstractJsonRpcTest {
3535

3636
abstract JsonRpcMapper createMapper();
3737

38-
@Before
38+
@BeforeEach
3939
public void init() throws Exception {
4040
clientConnection = TestUtils.connectionFactory().newConnection();
4141
clientChannel = clientConnection.createChannel();
@@ -57,7 +57,7 @@ public void init() throws Exception {
5757
service = client.createProxy(RpcService.class);
5858
}
5959

60-
@After
60+
@AfterEach
6161
public void tearDown() throws Exception {
6262
if (server != null) {
6363
server.terminateMainloop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.client;
17+
18+
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
19+
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
20+
21+
import com.rabbitmq.client.test.TestUtils;
22+
import com.rabbitmq.client.test.functional.FunctionalTestSuite;
23+
import com.rabbitmq.client.test.server.HaTestSuite;
24+
import com.rabbitmq.client.test.server.LastHaTestSuite;
25+
import com.rabbitmq.client.test.server.ServerTestSuite;
26+
import com.rabbitmq.client.test.ssl.SslTestSuite;
27+
import com.rabbitmq.tools.Host;
28+
import java.io.File;
29+
import java.lang.reflect.Field;
30+
import java.net.Socket;
31+
import java.util.Properties;
32+
import org.junit.jupiter.api.extension.AfterEachCallback;
33+
import org.junit.jupiter.api.extension.BeforeAllCallback;
34+
import org.junit.jupiter.api.extension.BeforeEachCallback;
35+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
36+
import org.junit.jupiter.api.extension.ExecutionCondition;
37+
import org.junit.jupiter.api.extension.ExtensionContext;
38+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
39+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
42+
43+
public class AmqpClientTestExtension implements ExecutionCondition, BeforeAllCallback,
44+
BeforeEachCallback,
45+
AfterEachCallback {
46+
47+
private static final Logger LOGGER = LoggerFactory.getLogger(AmqpClientTestExtension.class);
48+
private static final Namespace NAMESPACE = Namespace.create(AmqpClientTestExtension.class);
49+
50+
static {
51+
Properties TESTS_PROPS = new Properties(System.getProperties());
52+
String make = System.getenv("MAKE");
53+
if (make != null) {
54+
TESTS_PROPS.setProperty("make.bin", make);
55+
}
56+
try {
57+
TESTS_PROPS.load(Host.class.getClassLoader().getResourceAsStream("config.properties"));
58+
} catch (Exception e) {
59+
System.out.println(
60+
"\"build.properties\" or \"config.properties\" not found" +
61+
" in classpath. Please copy \"build.properties\" and" +
62+
" \"config.properties\" into src/test/resources. Ignore" +
63+
" this message if running with ant.");
64+
} finally {
65+
System.setProperties(TESTS_PROPS);
66+
}
67+
}
68+
69+
private static void maybeSetHaFieldValue(ExtensionContext context, boolean value) {
70+
if (context.getTestClass().isPresent() && context.getTestInstance().isPresent()) {
71+
try {
72+
Field haField = findField(context.getTestClass().get(), "ha");
73+
if (haField != null) {
74+
haField.setAccessible(true);
75+
haField.set(context.getTestInstance().get(), value);
76+
}
77+
} catch (Exception e) {
78+
// OK
79+
}
80+
}
81+
}
82+
83+
private static Field findField(Class<?> clazz, String fieldName) {
84+
try {
85+
return clazz.getDeclaredField(fieldName);
86+
} catch (NoSuchFieldException e) {
87+
if (clazz.getSuperclass() != null) {
88+
return findField(clazz.getSuperclass(), fieldName);
89+
}
90+
}
91+
return null;
92+
}
93+
94+
private static boolean isFunctionalSuite(ExtensionContext context) {
95+
return isTestSuite(context, FunctionalTestSuite.class);
96+
}
97+
98+
private static boolean isSslSuite(ExtensionContext context) {
99+
return isTestSuite(context, SslTestSuite.class);
100+
}
101+
102+
private static boolean isServerSuite(ExtensionContext context) {
103+
return isTestSuite(context, ServerTestSuite.class);
104+
}
105+
106+
private static boolean isHaSuite(ExtensionContext context) {
107+
return isTestSuite(context, HaTestSuite.class);
108+
}
109+
110+
private static boolean isLastHaSuite(ExtensionContext context) {
111+
return isTestSuite(context, LastHaTestSuite.class);
112+
}
113+
114+
private static boolean isTestSuite(ExtensionContext context, Class<?> clazz) {
115+
return context.getUniqueId().contains(clazz.getName());
116+
}
117+
118+
public static boolean requiredProperties() {
119+
/* Path to rabbitmqctl. */
120+
String rabbitmqctl = Host.rabbitmqctlCommand();
121+
if (rabbitmqctl == null) {
122+
System.err.println(
123+
"rabbitmqctl required; please set \"rabbitmqctl.bin\" system" +
124+
" property");
125+
return false;
126+
}
127+
128+
return true;
129+
}
130+
131+
public static boolean isSSLAvailable() {
132+
String sslClientCertsDir = System.getProperty("test-client-cert.path");
133+
String hostname = System.getProperty("broker.hostname");
134+
String port = System.getProperty("broker.sslport");
135+
if (sslClientCertsDir == null || hostname == null || port == null) {
136+
return false;
137+
}
138+
139+
// If certificate is present and some server is listening on port 5671
140+
if (new File(sslClientCertsDir).exists() &&
141+
checkServerListening(hostname, Integer.parseInt(port))) {
142+
return true;
143+
} else {
144+
return false;
145+
}
146+
}
147+
148+
private static boolean checkServerListening(String host, int port) {
149+
Socket s = null;
150+
try {
151+
s = new Socket(host, port);
152+
return true;
153+
} catch (Exception e) {
154+
return false;
155+
} finally {
156+
if (s != null) {
157+
try {
158+
s.close();
159+
} catch (Exception e) {
160+
}
161+
}
162+
}
163+
}
164+
165+
private static Store store(ExtensionContext context) {
166+
return context.getRoot().getStore(NAMESPACE);
167+
}
168+
169+
private static boolean hasHaSuiteStarted(ExtensionContext context) {
170+
return "true".equals(store(context).get("ha"));
171+
}
172+
173+
private static void markHaSuiteStarted(ExtensionContext context) {
174+
store(context).put("ha", "true");
175+
}
176+
177+
private static void markHaSuiteFinished(ExtensionContext context) {
178+
store(context).remove("ha");
179+
}
180+
181+
@Override
182+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
183+
// HA test suite must be checked first because it contains other test suites
184+
if (isHaSuite(context)) {
185+
return requiredProperties() ? enabled("Required properties available")
186+
: disabled("Required properties not available");
187+
} else if (isServerSuite(context)) {
188+
return requiredProperties() ? enabled("Required properties available")
189+
: disabled("Required properties not available");
190+
} else if (isFunctionalSuite(context)) {
191+
return requiredProperties() ? enabled("Required properties available")
192+
: disabled("Required properties not available");
193+
} else if (isSslSuite(context)) {
194+
return requiredProperties() && isSSLAvailable() ? enabled(
195+
"Required properties and TLS available")
196+
: disabled("Required properties or TLS not available");
197+
}
198+
return enabled("ok");
199+
}
200+
201+
@Override
202+
public void beforeAll(ExtensionContext context) throws Exception {
203+
if (isHaSuite(context) && !hasHaSuiteStarted(context)) {
204+
LOGGER.info("Starting HA test suite");
205+
Host.rabbitmqctl("set_policy HA '.*' '{\"ha-mode\":\"all\"}'");
206+
markHaSuiteStarted(context);
207+
}
208+
if (isLastHaSuite(context)) {
209+
LOGGER.info("HA suite done, clearing HA state");
210+
Host.rabbitmqctl("clear_policy HA");
211+
markHaSuiteFinished(context);
212+
}
213+
}
214+
215+
@Override
216+
public void beforeEach(ExtensionContext context) {
217+
LOGGER.info(
218+
"Starting test: {}.{} (nio? {}, HA? {})",
219+
context.getTestClass().get().getSimpleName(), context.getTestMethod().get().getName(),
220+
TestUtils.USE_NIO,
221+
hasHaSuiteStarted(context)
222+
);
223+
if (isHaSuite(context)) {
224+
maybeSetHaFieldValue(context, true);
225+
}
226+
}
227+
228+
@Override
229+
public void afterEach(ExtensionContext context) {
230+
LOGGER.info("Test finished: {}.{}",
231+
context.getTestClass().get().getSimpleName(), context.getTestMethod().get().getName());
232+
if (isHaSuite(context)) {
233+
maybeSetHaFieldValue(context, false);
234+
}
235+
}
236+
237+
}

0 commit comments

Comments
 (0)