Skip to content

Commit 6ca71ab

Browse files
committed
Intermittent MBeanServerFactoryBeanTests failure
Prior to this commit the testWithLocateExistingAndExistingServer method would fail if any preceding test called the ManagementFactory getPlatformMBeanServer() method. In such situations the platform server is located instead of the expected freshly created server. These failures are more likely to happen when compiling with JDK 7 due to the fact that the reflection API no longer returns methods in a consistent order. Unfortunately there is no easy way to reset the platform MBean server so the new code must resort to using reflection to access the private static ManagementFactory.platformMBeanServer field. Issue: SPR-9288
1 parent d7bf56d commit 6ca71ab

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.springframework.jmx.support;
1818

19-
import java.util.List;
2019
import java.lang.management.ManagementFactory;
20+
import java.lang.reflect.Field;
21+
import java.util.List;
22+
2123
import javax.management.MBeanServer;
2224
import javax.management.MBeanServerFactory;
2325

@@ -26,9 +28,34 @@
2628
/**
2729
* @author Rob Harrop
2830
* @author Juergen Hoeller
31+
* @author Phillip Webb
2932
*/
3033
public class MBeanServerFactoryBeanTests extends TestCase {
3134

35+
@Override
36+
protected void setUp() throws Exception {
37+
resetPlatformManager();
38+
}
39+
40+
@Override
41+
protected void tearDown() throws Exception {
42+
resetPlatformManager();
43+
}
44+
45+
/**
46+
* Resets MBeanServerFactory and ManagementFactory to a known consistent state.
47+
* This involves releasing all currently registered MBeanServers and resetting
48+
* the platformMBeanServer to null.
49+
*/
50+
private void resetPlatformManager() throws Exception {
51+
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
52+
MBeanServerFactory.releaseMBeanServer(server);
53+
}
54+
Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
55+
field.setAccessible(true);
56+
field.set(null, null);
57+
}
58+
3259
public void testGetObject() throws Exception {
3360
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
3461
bean.afterPropertiesSet();

0 commit comments

Comments
 (0)