Reset MBean Servers after JRuby and JMX tests
Refactor MBean Server reset code from MBeanServerFactoryBeanTests and reuse in AdvisedJRubyScriptFactoryTests and AbstractMBeanServerTests. Issue: SPR-9288
This commit is contained in:
parent
ff945bdca9
commit
0d73d199ec
|
|
@ -25,17 +25,18 @@ import junit.framework.TestCase;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
|
import org.springframework.util.MBeanTestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <strong>Note:</strong> the JMX test suite requires the presence of the
|
* <strong>Note:</strong> the JMX test suite requires the presence of the
|
||||||
* <code>jmxremote_optional.jar</code> in your classpath. Thus, if you
|
* <code>jmxremote_optional.jar</code> in your classpath. Thus, if you
|
||||||
* run into the <em>"Unsupported protocol: jmxmp"</em> error, you will
|
* run into the <em>"Unsupported protocol: jmxmp"</em> error, you will
|
||||||
* need to download the
|
* need to download the
|
||||||
* <a href="http://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html">JMX Remote API 1.0.1_04 Reference Implementation</a>
|
* <a href="http://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</code> into your
|
* from Oracle and extract <code>jmxremote_optional.jar</code> into your
|
||||||
* classpath, for example in the <code>lib/ext</code> folder of your JVM.
|
* classpath, for example in the <code>lib/ext</code> folder of your JVM.
|
||||||
* See also <a href="https://issuetracker.springsource.com/browse/EBR-349">EBR-349</a>.
|
* See also <a href="https://issuetracker.springsource.com/browse/EBR-349">EBR-349</a>.
|
||||||
*
|
*
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
|
|
@ -67,8 +68,9 @@ public abstract class AbstractMBeanServerTests extends TestCase {
|
||||||
onTearDown();
|
onTearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void releaseServer() {
|
private void releaseServer() throws Exception {
|
||||||
MBeanServerFactory.releaseMBeanServer(getServer());
|
MBeanServerFactory.releaseMBeanServer(getServer());
|
||||||
|
MBeanTestUtils.resetMBeanServers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onTearDown() throws Exception {
|
protected void onTearDown() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.jmx.support;
|
package org.springframework.jmx.support;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.management.MBeanServer;
|
import javax.management.MBeanServer;
|
||||||
|
|
@ -25,6 +24,8 @@ import javax.management.MBeanServerFactory;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.springframework.util.MBeanTestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
|
@ -34,26 +35,12 @@ public class MBeanServerFactoryBeanTests extends TestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
resetPlatformManager();
|
MBeanTestUtils.resetMBeanServers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
resetPlatformManager();
|
MBeanTestUtils.resetMBeanServers();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 {
|
public void testGetObject() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,16 @@
|
||||||
|
|
||||||
package org.springframework.scripting.jruby;
|
package org.springframework.scripting.jruby;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.aop.framework.Advised;
|
import org.springframework.aop.framework.Advised;
|
||||||
import org.springframework.aop.support.AopUtils;
|
import org.springframework.aop.support.AopUtils;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
import org.springframework.scripting.Messenger;
|
import org.springframework.scripting.Messenger;
|
||||||
|
import org.springframework.util.MBeanTestUtils;
|
||||||
|
|
||||||
import test.advice.CountingBeforeAdvice;
|
import test.advice.CountingBeforeAdvice;
|
||||||
|
|
||||||
|
|
@ -31,41 +34,52 @@ import test.advice.CountingBeforeAdvice;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
*/
|
*/
|
||||||
public final class AdvisedJRubyScriptFactoryTests {
|
public final class AdvisedJRubyScriptFactoryTests {
|
||||||
|
|
||||||
private static final Class<?> CLASS = AdvisedJRubyScriptFactoryTests.class;
|
private static final Class<?> CLASS = AdvisedJRubyScriptFactoryTests.class;
|
||||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||||
|
|
||||||
private static final String FACTORYBEAN_CONTEXT = CLASSNAME + "-factoryBean.xml";
|
private static final String FACTORYBEAN_CONTEXT = CLASSNAME + "-factoryBean.xml";
|
||||||
private static final String APC_CONTEXT = CLASSNAME + "-beanNameAutoProxyCreator.xml";
|
private static final String APC_CONTEXT = CLASSNAME + "-beanNameAutoProxyCreator.xml";
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void resetMBeanServers() throws Exception {
|
||||||
|
MBeanTestUtils.resetMBeanServers();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdviseWithProxyFactoryBean() {
|
public void testAdviseWithProxyFactoryBean() {
|
||||||
ClassPathXmlApplicationContext ctx =
|
ClassPathXmlApplicationContext ctx =
|
||||||
new ClassPathXmlApplicationContext(FACTORYBEAN_CONTEXT, CLASS);
|
new ClassPathXmlApplicationContext(FACTORYBEAN_CONTEXT, CLASS);
|
||||||
|
try {
|
||||||
|
Messenger bean = (Messenger) ctx.getBean("messenger");
|
||||||
|
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
||||||
|
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
||||||
|
|
||||||
Messenger bean = (Messenger) ctx.getBean("messenger");
|
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||||
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
assertEquals(0, advice.getCalls());
|
||||||
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
bean.getMessage();
|
||||||
|
assertEquals(1, advice.getCalls());
|
||||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
} finally {
|
||||||
assertEquals(0, advice.getCalls());
|
ctx.close();
|
||||||
bean.getMessage();
|
}
|
||||||
assertEquals(1, advice.getCalls());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdviseWithBeanNameAutoProxyCreator() {
|
public void testAdviseWithBeanNameAutoProxyCreator() {
|
||||||
ClassPathXmlApplicationContext ctx =
|
ClassPathXmlApplicationContext ctx =
|
||||||
new ClassPathXmlApplicationContext(APC_CONTEXT, CLASS);
|
new ClassPathXmlApplicationContext(APC_CONTEXT, CLASS);
|
||||||
|
try {
|
||||||
|
Messenger bean = (Messenger) ctx.getBean("messenger");
|
||||||
|
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
||||||
|
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
||||||
|
|
||||||
Messenger bean = (Messenger) ctx.getBean("messenger");
|
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||||
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
assertEquals(0, advice.getCalls());
|
||||||
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
bean.getMessage();
|
||||||
|
assertEquals(1, advice.getCalls());
|
||||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
} finally {
|
||||||
assertEquals(0, advice.getCalls());
|
ctx.close();
|
||||||
bean.getMessage();
|
}
|
||||||
assertEquals(1, advice.getCalls());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2012 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
|
||||||
|
*
|
||||||
|
* http://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.util;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.MBeanServerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utilities for MBean tests.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
public class MBeanTestUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets MBeanServerFactory and ManagementFactory to a known consistent state.
|
||||||
|
* This involves releasing all currently registered MBeanServers and resetting
|
||||||
|
* the platformMBeanServer to null.
|
||||||
|
*/
|
||||||
|
public static void resetMBeanServers() throws Exception {
|
||||||
|
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
|
||||||
|
MBeanServerFactory.releaseMBeanServer(server);
|
||||||
|
}
|
||||||
|
Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue