1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .management .ManagementFactory ;
20
20
import java .lang .reflect .Field ;
21
+ import java .util .EnumSet ;
21
22
22
23
import javax .management .MBeanServer ;
23
24
import javax .management .MBeanServerFactory ;
24
25
26
+ import org .junit .jupiter .api .condition .JRE ;
27
+
25
28
/**
26
29
* Utilities for MBean tests.
27
30
*
28
31
* @author Phillip Webb
32
+ * @author Sam Brannen
29
33
*/
30
34
public class MBeanTestUtils {
31
35
32
36
/**
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}.
36
41
*/
37
42
public static synchronized void resetMBeanServers () throws Exception {
38
43
for (MBeanServer server : MBeanServerFactory .findMBeanServer (null )) {
@@ -46,9 +51,15 @@ public static synchronized void resetMBeanServers() throws Exception {
46
51
}
47
52
}
48
53
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 ());
52
63
}
53
64
54
65
}
0 commit comments