From 6ca71abf934ee689f227d5ff864e7a4c4d8ae9d5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 26 Nov 2012 15:45:21 -0800 Subject: [PATCH] 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 --- .../support/MBeanServerFactoryBeanTests.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java b/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java index d2b224dad7..7196643124 100644 --- a/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java @@ -16,8 +16,10 @@ package org.springframework.jmx.support; -import java.util.List; import java.lang.management.ManagementFactory; +import java.lang.reflect.Field; +import java.util.List; + import javax.management.MBeanServer; import javax.management.MBeanServerFactory; @@ -26,9 +28,34 @@ import junit.framework.TestCase; /** * @author Rob Harrop * @author Juergen Hoeller + * @author Phillip Webb */ public class MBeanServerFactoryBeanTests extends TestCase { + @Override + protected void setUp() throws Exception { + resetPlatformManager(); + } + + @Override + protected void tearDown() throws Exception { + resetPlatformManager(); + } + + /** + * Resets MBeanServerFactory and ManagementFactory to a known consistent state. + * This involves releasing all currently registered MBeanServers and resetting + * the platformMBeanServer to null. + */ + private void resetPlatformManager() throws Exception { + for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) { + MBeanServerFactory.releaseMBeanServer(server); + } + Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer"); + field.setAccessible(true); + field.set(null, null); + } + public void testGetObject() throws Exception { MBeanServerFactoryBean bean = new MBeanServerFactoryBean(); bean.afterPropertiesSet();