From c838bcff3aa1a52c854ab70f4fcd4f70bd542ff0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 2 Dec 2022 16:32:40 +0100 Subject: [PATCH] Fix/Disable JMX & JNDI tests on JDK 16+ --- .../jmx/support/JmxUtilsTests.java | 8 +++-- .../jndi/JndiLocatorDelegateTests.java | 12 ++++--- .../springframework/util/MBeanTestUtils.java | 31 +++++++++++++------ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/jmx/support/JmxUtilsTests.java b/spring-context/src/test/java/org/springframework/jmx/support/JmxUtilsTests.java index f8ef4348bc..30b8eb122c 100644 --- a/spring-context/src/test/java/org/springframework/jmx/support/JmxUtilsTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/support/JmxUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.beans.PropertyDescriptor; import javax.management.DynamicMBean; import javax.management.MBeanServer; -import javax.management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; @@ -32,6 +31,7 @@ import org.springframework.beans.BeanWrapperImpl; import org.springframework.jmx.IJmxTestBean; import org.springframework.jmx.JmxTestBean; import org.springframework.jmx.export.TestDynamicMBean; +import org.springframework.util.MBeanTestUtils; import org.springframework.util.ObjectUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Rob Harrop * @author Juergen Hoeller + * @author Sam Brannen */ class JmxUtilsTests { @@ -131,10 +132,11 @@ class JmxUtilsTests { MBeanServer server = null; try { server = JmxUtils.locateMBeanServer(); + assertThat(server).isNotNull(); } finally { if (server != null) { - MBeanServerFactory.releaseMBeanServer(server); + MBeanTestUtils.releaseMBeanServer(server); } } } diff --git a/spring-context/src/test/java/org/springframework/jndi/JndiLocatorDelegateTests.java b/spring-context/src/test/java/org/springframework/jndi/JndiLocatorDelegateTests.java index 700860e11d..5e139967ad 100644 --- a/spring-context/src/test/java/org/springframework/jndi/JndiLocatorDelegateTests.java +++ b/spring-context/src/test/java/org/springframework/jndi/JndiLocatorDelegateTests.java @@ -21,21 +21,25 @@ import java.lang.reflect.Field; import javax.naming.spi.NamingManager; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledForJreRange; import static org.assertj.core.api.Assertions.assertThat; - - +import static org.junit.jupiter.api.condition.JRE.JAVA_16; /** * Tests for {@link JndiLocatorDelegate}. * * @author Phillip Webb * @author Juergen Hoeller + * @author Sam Brannen */ -public class JndiLocatorDelegateTests { +@DisabledForJreRange( + min = JAVA_16, + disabledReason = "Cannot use reflection to set private static field in javax.naming.spi.NamingManager") +class JndiLocatorDelegateTests { @Test - public void isDefaultJndiEnvironmentAvailableFalse() throws Exception { + void isDefaultJndiEnvironmentAvailableFalse() throws Exception { Field builderField = NamingManager.class.getDeclaredField("initctx_factory_builder"); builderField.setAccessible(true); Object oldBuilder = builderField.get(null); diff --git a/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java index a177b16d63..2365651934 100644 --- a/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java +++ b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java @@ -34,21 +34,15 @@ import org.junit.jupiter.api.condition.JRE; public class MBeanTestUtils { /** - * Resets {@link MBeanServerFactory} to a known consistent state. This involves - * releasing all currently registered MBeanServers. + * Reset the {@link MBeanServerFactory} to a known consistent state. This involves + * {@linkplain #releaseMBeanServer(MBeanServer) releasing} all currently registered + * MBeanServers. *

On JDK 8 - JDK 16, this method also resets the platformMBeanServer field * in {@link ManagementFactory} to {@code null}. */ public static synchronized void resetMBeanServers() throws Exception { for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) { - try { - MBeanServerFactory.releaseMBeanServer(server); - } - catch (IllegalArgumentException ex) { - if (!ex.getMessage().contains("not in list")) { - throw ex; - } - } + releaseMBeanServer(server); } if (!isCurrentJreWithinRange(JRE.JAVA_16, JRE.OTHER)) { @@ -58,6 +52,23 @@ public class MBeanTestUtils { } } + /** + * Attempt to release the supplied {@link MBeanServer}. + *

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; + } + } + } + static boolean isCurrentJreWithinRange(JRE min, JRE max) { return EnumSet.range(min, max).contains(JRE.currentVersion()); }