Skip to content

Commit 00acccc

Browse files
committed
Enable some TLS tests only from Java 11
They fail on Java 8-9-10 on Ubuntu 22.04, possibly because the OS makes TLS gen uses algorithm that "old" JRE do not support. This is acceptable if we want to test against latest environments. (cherry picked from commit ba9316d)
1 parent 9b88e6e commit 00acccc

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ public void evaluate() throws Throwable {
322322

323323
private static class BrokerIsNotOnDocker implements TestRule {
324324

325-
326325
@Override
327326
public Statement apply(Statement base, Description description) {
328327
return new Statement() {
@@ -421,4 +420,67 @@ public void evaluate() throws Throwable {
421420
};
422421
}
423422
}
423+
424+
public static TestRule atLeastJava11() {
425+
return new AtLeastJavaVersion(11);
426+
}
427+
428+
private static class AtLeastJavaVersion implements TestRule {
429+
430+
private final int expectedMinVersion;
431+
432+
private AtLeastJavaVersion(int expectedMinVersion) {
433+
this.expectedMinVersion = expectedMinVersion;
434+
}
435+
436+
@Override
437+
public Statement apply(Statement base, Description description) {
438+
return new Statement() {
439+
@Override
440+
public void evaluate() throws Throwable {
441+
try {
442+
int javaMajorVersion = javaMajorVersion();
443+
if (javaMajorVersion < expectedMinVersion) {
444+
throw new AssumptionViolatedException("Java version is " + javaMajorVersion
445+
+ ", expecting at least " + expectedMinVersion);
446+
}
447+
} catch (AssumptionViolatedException e) {
448+
throw e;
449+
} catch (Exception e) {
450+
throw new AssumptionViolatedException("Could determine Java version", e);
451+
}
452+
base.evaluate();
453+
}
454+
};
455+
}
456+
}
457+
458+
private static int javaMajorVersion() {
459+
String javaVersion = System.getProperty("java.version");
460+
if (javaVersion == null || javaVersion.trim().isEmpty()) {
461+
throw new IllegalStateException("JVM system property 'java.version' is undefined");
462+
}
463+
464+
if (javaVersion.startsWith("1.8")) {
465+
return 8;
466+
}
467+
468+
try {
469+
// from JUnit 5 JRE class
470+
// java.lang.Runtime.version() is a static method available on Java 9+
471+
// that returns an instance of java.lang.Runtime.Version which has the
472+
// following method: public int major()
473+
Method versionMethod = Runtime.class.getMethod("version");
474+
Object version = versionMethod.invoke(null);
475+
Method majorMethod = version.getClass().getMethod("major");
476+
int major = (int) majorMethod.invoke(version);
477+
if (major < 9) {
478+
throw new IllegalStateException("Invalid Java major version: " + major);
479+
}
480+
return major;
481+
} catch (Exception ex) {
482+
LOGGER.warn("Error while computing Java major version", ex);
483+
}
484+
throw new IllegalStateException("Could not determine Java major version");
485+
}
424486
}

src/test/java/com/rabbitmq/client/test/ssl/BadVerifiedConnection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2022 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
@@ -15,12 +15,15 @@
1515

1616
package com.rabbitmq.client.test.ssl;
1717

18+
import com.rabbitmq.client.test.TestUtils;
19+
import org.junit.ClassRule;
1820
import org.junit.Test;
1921

2022
import javax.net.ssl.SSLContext;
2123
import javax.net.ssl.SSLHandshakeException;
2224
import java.io.IOException;
2325
import java.util.concurrent.TimeoutException;
26+
import org.junit.rules.TestRule;
2427

2528
import static org.junit.Assert.fail;
2629

@@ -29,6 +32,10 @@
2932
*
3033
*/
3134
public class BadVerifiedConnection extends UnverifiedConnection {
35+
36+
@ClassRule
37+
public static TestRule atLeastJava11TestRule = TestUtils.atLeastJava11();
38+
3239
public void openConnection()
3340
throws IOException, TimeoutException {
3441
try {

src/test/java/com/rabbitmq/client/test/ssl/HostnameVerification.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2022 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
@@ -20,7 +20,9 @@
2020
import com.rabbitmq.client.ConnectionFactory;
2121
import com.rabbitmq.client.test.TestUtils;
2222
import org.junit.BeforeClass;
23+
import org.junit.ClassRule;
2324
import org.junit.Test;
25+
import org.junit.rules.TestRule;
2426
import org.junit.runner.RunWith;
2527
import org.junit.runners.Parameterized;
2628

@@ -35,6 +37,9 @@
3537
@RunWith(Parameterized.class)
3638
public class HostnameVerification {
3739

40+
@ClassRule
41+
public static TestRule atLeastJava11TestRule = TestUtils.atLeastJava11();
42+
3843
static SSLContext sslContext;
3944
@Parameterized.Parameter
4045
public Consumer<ConnectionFactory> customizer;

src/test/java/com/rabbitmq/client/test/ssl/VerifiedConnection.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2022 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
@@ -36,7 +36,9 @@
3636

3737
import com.rabbitmq.client.ConnectionFactory;
3838
import com.rabbitmq.client.test.TestUtils;
39+
import org.junit.ClassRule;
3940
import org.junit.Test;
41+
import org.junit.rules.TestRule;
4042
import org.slf4j.LoggerFactory;
4143

4244
/**
@@ -45,6 +47,9 @@
4547
*/
4648
public class VerifiedConnection extends UnverifiedConnection {
4749

50+
@ClassRule
51+
public static TestRule atLeastJava11TestRule = TestUtils.atLeastJava11();
52+
4853
public void openConnection()
4954
throws IOException, TimeoutException {
5055
try {

0 commit comments

Comments
 (0)