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
This commit is contained in:
Phillip Webb 2012-11-26 15:45:21 -08:00
parent d7bf56df49
commit 6ca71abf93
1 changed files with 28 additions and 1 deletions

View File

@ -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();