Skip to content

Commit c838bcf

Browse files
committed
Fix/Disable JMX & JNDI tests on JDK 16+
1 parent 109b00d commit c838bcf

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

spring-context/src/test/java/org/springframework/jmx/support/JmxUtilsTests.java

Lines changed: 5 additions & 3 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-2022 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.
@@ -20,7 +20,6 @@
2020

2121
import javax.management.DynamicMBean;
2222
import javax.management.MBeanServer;
23-
import javax.management.MBeanServerFactory;
2423
import javax.management.MalformedObjectNameException;
2524
import javax.management.NotCompliantMBeanException;
2625
import javax.management.ObjectName;
@@ -32,6 +31,7 @@
3231
import org.springframework.jmx.IJmxTestBean;
3332
import org.springframework.jmx.JmxTestBean;
3433
import org.springframework.jmx.export.TestDynamicMBean;
34+
import org.springframework.util.MBeanTestUtils;
3535
import org.springframework.util.ObjectUtils;
3636

3737
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,6 +41,7 @@
4141
*
4242
* @author Rob Harrop
4343
* @author Juergen Hoeller
44+
* @author Sam Brannen
4445
*/
4546
class JmxUtilsTests {
4647

@@ -131,10 +132,11 @@ void locatePlatformMBeanServer() {
131132
MBeanServer server = null;
132133
try {
133134
server = JmxUtils.locateMBeanServer();
135+
assertThat(server).isNotNull();
134136
}
135137
finally {
136138
if (server != null) {
137-
MBeanServerFactory.releaseMBeanServer(server);
139+
MBeanTestUtils.releaseMBeanServer(server);
138140
}
139141
}
140142
}

spring-context/src/test/java/org/springframework/jndi/JndiLocatorDelegateTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@
2121
import javax.naming.spi.NamingManager;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.condition.DisabledForJreRange;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
26-
27-
27+
import static org.junit.jupiter.api.condition.JRE.JAVA_16;
2828

2929
/**
3030
* Tests for {@link JndiLocatorDelegate}.
3131
*
3232
* @author Phillip Webb
3333
* @author Juergen Hoeller
34+
* @author Sam Brannen
3435
*/
35-
public class JndiLocatorDelegateTests {
36+
@DisabledForJreRange(
37+
min = JAVA_16,
38+
disabledReason = "Cannot use reflection to set private static field in javax.naming.spi.NamingManager")
39+
class JndiLocatorDelegateTests {
3640

3741
@Test
38-
public void isDefaultJndiEnvironmentAvailableFalse() throws Exception {
42+
void isDefaultJndiEnvironmentAvailableFalse() throws Exception {
3943
Field builderField = NamingManager.class.getDeclaredField("initctx_factory_builder");
4044
builderField.setAccessible(true);
4145
Object oldBuilder = builderField.get(null);

spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,15 @@
3434
public class MBeanTestUtils {
3535

3636
/**
37-
* Resets {@link MBeanServerFactory} to a known consistent state. This involves
38-
* releasing all currently registered MBeanServers.
37+
* Reset the {@link MBeanServerFactory} to a known consistent state. This involves
38+
* {@linkplain #releaseMBeanServer(MBeanServer) releasing} all currently registered
39+
* MBeanServers.
3940
* <p>On JDK 8 - JDK 16, this method also resets the platformMBeanServer field
4041
* in {@link ManagementFactory} to {@code null}.
4142
*/
4243
public static synchronized void resetMBeanServers() throws Exception {
4344
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
44-
try {
45-
MBeanServerFactory.releaseMBeanServer(server);
46-
}
47-
catch (IllegalArgumentException ex) {
48-
if (!ex.getMessage().contains("not in list")) {
49-
throw ex;
50-
}
51-
}
45+
releaseMBeanServer(server);
5246
}
5347

5448
if (!isCurrentJreWithinRange(JRE.JAVA_16, JRE.OTHER)) {
@@ -58,6 +52,23 @@ public static synchronized void resetMBeanServers() throws Exception {
5852
}
5953
}
6054

55+
/**
56+
* Attempt to release the supplied {@link MBeanServer}.
57+
* <p>Ignores any {@link IllegalArgumentException} thrown by
58+
* {@link MBeanServerFactory#releaseMBeanServer(MBeanServer)} whose error
59+
* message contains the text "not in list".
60+
*/
61+
public static void releaseMBeanServer(MBeanServer server) {
62+
try {
63+
MBeanServerFactory.releaseMBeanServer(server);
64+
}
65+
catch (IllegalArgumentException ex) {
66+
if (!ex.getMessage().contains("not in list")) {
67+
throw ex;
68+
}
69+
}
70+
}
71+
6172
static boolean isCurrentJreWithinRange(JRE min, JRE max) {
6273
return EnumSet.range(min, max).contains(JRE.currentVersion());
6374
}

0 commit comments

Comments
 (0)