Reinstate test for JmxUtils.locateMBeanServer()

This commit is contained in:
Sam Brannen 2022-12-03 18:23:41 -05:00
parent 69d022712b
commit c24a51323d
2 changed files with 39 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.jmx.support;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import javax.management.DynamicMBean; import javax.management.DynamicMBean;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException; import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName; import javax.management.ObjectName;
@ -30,6 +31,7 @@ import org.springframework.beans.BeanWrapperImpl;
import org.springframework.jmx.IJmxTestBean; import org.springframework.jmx.IJmxTestBean;
import org.springframework.jmx.JmxTestBean; import org.springframework.jmx.JmxTestBean;
import org.springframework.jmx.export.TestDynamicMBean; import org.springframework.jmx.export.TestDynamicMBean;
import org.springframework.util.MBeanTestUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -39,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
*/ */
class JmxUtilsTests { class JmxUtilsTests {
@ -124,6 +127,20 @@ class JmxUtilsTests {
assertThat(uniqueName.getKeyProperty(JmxUtils.IDENTITY_OBJECT_NAME_KEY)).as("Identity key is incorrect").isEqualTo(ObjectUtils.getIdentityHexString(managedResource)); assertThat(uniqueName.getKeyProperty(JmxUtils.IDENTITY_OBJECT_NAME_KEY)).as("Identity key is incorrect").isEqualTo(ObjectUtils.getIdentityHexString(managedResource));
} }
@Test
void locatePlatformMBeanServer() {
MBeanServer server = null;
try {
server = JmxUtils.locateMBeanServer();
assertThat(server).isNotNull();
}
finally {
if (server != null) {
MBeanTestUtils.releaseMBeanServer(server);
}
}
}
public static class AttributeTestBean { public static class AttributeTestBean {

View File

@ -23,22 +23,34 @@ import javax.management.MBeanServerFactory;
* Utilities for MBean tests. * Utilities for MBean tests.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen
*/ */
public class MBeanTestUtils { public class MBeanTestUtils {
/** /**
* Resets {@link MBeanServerFactory} to a known consistent state. * Reset the {@link MBeanServerFactory} to a known consistent state. This involves
* <p>This involves releasing all currently registered MBeanServers. * {@linkplain #releaseMBeanServer(MBeanServer) releasing} all currently registered
* MBeanServers.
*/ */
public static synchronized void resetMBeanServers() throws Exception { public static synchronized void resetMBeanServers() throws Exception {
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) { for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
try { releaseMBeanServer(server);
MBeanServerFactory.releaseMBeanServer(server); }
} }
catch (IllegalArgumentException ex) {
if (!ex.getMessage().contains("not in list")) { /**
throw ex; * Attempt to release the supplied {@link MBeanServer}.
} * <p>Ignores any {@link IllegalArgumentException} thrown by
* {@link MBeanServerFactory#releaseMBeanServer(MBeanServer)} whose error
* message contains the text "not in list".
*/
public static void releaseMBeanServer(MBeanServer server) {
try {
MBeanServerFactory.releaseMBeanServer(server);
}
catch (IllegalArgumentException ex) {
if (!ex.getMessage().contains("not in list")) {
throw ex;
} }
} }
} }