Skip to content

Commit f7f7343

Browse files
committed
Modify MBeanTestUtils to work on JDK 17+
1 parent 5e55558 commit f7f7343

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

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

Lines changed: 18 additions & 7 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.
@@ -18,21 +18,26 @@
1818

1919
import java.lang.management.ManagementFactory;
2020
import java.lang.reflect.Field;
21+
import java.util.EnumSet;
2122

2223
import javax.management.MBeanServer;
2324
import javax.management.MBeanServerFactory;
2425

26+
import org.junit.jupiter.api.condition.JRE;
27+
2528
/**
2629
* Utilities for MBean tests.
2730
*
2831
* @author Phillip Webb
32+
* @author Sam Brannen
2933
*/
3034
public class MBeanTestUtils {
3135

3236
/**
33-
* Resets MBeanServerFactory and ManagementFactory to a known consistent state.
34-
* <p>This involves releasing all currently registered MBeanServers and resetting
35-
* the platformMBeanServer to null.
37+
* Resets {@link MBeanServerFactory} to a known consistent state. This involves
38+
* releasing all currently registered MBeanServers.
39+
* <p>On JDK 8 - JDK 16, this method also resets the platformMBeanServer field
40+
* in {@link ManagementFactory} to {@code null}.
3641
*/
3742
public static synchronized void resetMBeanServers() throws Exception {
3843
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
@@ -46,9 +51,15 @@ public static synchronized void resetMBeanServers() throws Exception {
4651
}
4752
}
4853

49-
Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
50-
field.setAccessible(true);
51-
field.set(null, null);
54+
if (!isCurrentJreWithinRange(JRE.JAVA_16, JRE.OTHER)) {
55+
Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
56+
field.setAccessible(true);
57+
field.set(null, null);
58+
}
59+
}
60+
61+
static boolean isCurrentJreWithinRange(JRE min, JRE max) {
62+
return EnumSet.range(min, max).contains(JRE.currentVersion());
5263
}
5364

5465
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- global -->
66
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis"/>
7-
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]org[\\/]springframework[\\/].+(Tests|Suite)" checks="IllegalImport" id="bannedJUnitJupiterImports"/>
7+
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]org[\\/]springframework[\\/].+(Tests|Suite|TestUtils)" checks="IllegalImport" id="bannedJUnitJupiterImports"/>
88
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]" checks="SpringJUnit5" message="should not be public"/>
99
<!-- generated sources -->
1010
<suppress files="[\\/]build[\\/]generated[\\/]sources[\\/]" checks=".*"/>

0 commit comments

Comments
 (0)