/*
 * Copyright 2002-2019 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.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.util.MBeanTestUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * <p>If you run into the <em>"Unsupported protocol: jmxmp"</em> error, you will need to
 * download the <a href="https://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html">JMX
 * Remote API 1.0.1_04 Reference Implementation</a> from Oracle and extract
 * {@code jmxremote_optional.jar} into your classpath, for example in the {@code lib/ext}
 * folder of your JVM.
 *
 * <p>See also:
 * <ul>
 * <li><a href="https://jira.spring.io/browse/SPR-8093">SPR-8093</a></li>
 * <li><a href="https://issuetracker.springsource.com/browse/EBR-349">EBR-349</a></li>
 * </ul>
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Chris Beams
 * @author Stephane Nicoll
 */
public abstract class AbstractMBeanServerTests {

	protected MBeanServer server;


	@BeforeEach
	public final void setUp() throws Exception {
		this.server = MBeanServerFactory.createMBeanServer();
		try {
			onSetUp();
		}
		catch (Exception ex) {
			releaseServer();
			throw ex;
		}
	}

	protected ConfigurableApplicationContext loadContext(String configLocation) {
		GenericApplicationContext ctx = new GenericApplicationContext();
		new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(configLocation);
		ctx.getDefaultListableBeanFactory().registerSingleton("server", this.server);
		ctx.refresh();
		return ctx;
	}

	@AfterEach
	public void tearDown() throws Exception {
		releaseServer();
		onTearDown();
	}

	private void releaseServer() throws Exception {
		MBeanServerFactory.releaseMBeanServer(getServer());
		MBeanTestUtils.resetMBeanServers();
	}

	protected void onTearDown() throws Exception {
	}

	protected void onSetUp() throws Exception {
	}

	public MBeanServer getServer() {
		return this.server;
	}

	/**
	 * Start the specified {@link MBeanExporter}.
	 */
	protected void start(MBeanExporter exporter) {
		exporter.afterPropertiesSet();
		exporter.afterSingletonsInstantiated();
	}

	protected void assertIsRegistered(String message, ObjectName objectName) {
		assertThat(getServer().isRegistered(objectName)).as(message).isTrue();
	}

	protected void assertIsNotRegistered(String message, ObjectName objectName) {
		assertThat(getServer().isRegistered(objectName)).as(message).isFalse();
	}

}

@@ -650,16 +650,48 @@
 k.jmx;%0A%0A
+import java.net.BindException;%0A%0A
 import j
@@ -880,16 +880,188 @@
 oreEach;
+%0Aimport org.junit.jupiter.api.extension.RegisterExtension;%0Aimport org.junit.jupiter.api.extension.TestExecutionExceptionHandler;%0Aimport org.opentest4j.TestAbortedException;
 %0A%0Aimport
@@ -2187,16 +2187,362 @@
 ests %7B%0A%0A
+%09@RegisterExtension%0A%09TestExecutionExceptionHandler bindExceptionHandler = (context, throwable) -%3E %7B%0A%09%09// Abort test?%0A%09%09if (throwable instanceof BindException) %7B%0A%09%09%09throw new TestAbortedException(%22Failed to bind to MBeanServer%22, throwable);%0A%09%09%7D%0A%09%09// Else rethrow to conform to the contract of TestExecutionExceptionHandler%0A%09%09throw throwable;%0A%09%7D;%0A%0A
 %09protect

Abort MBeanServer tests if BindException encountered