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.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.MBeanTestUtils;
|
||||
|
||||
/**
|
||||
* <strong>Note:</strong> the JMX test suite requires the presence of the
|
||||
* <code>jmxremote_optional.jar</code> in your classpath. Thus, if you
|
||||
* run into the <em>"Unsupported protocol: jmxmp"</em> error, you will
|
||||
* 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
|
||||
* 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>.
|
||||
*
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
|
|
@ -67,8 +68,9 @@ public abstract class AbstractMBeanServerTests extends TestCase {
|
|||
onTearDown();
|
||||
}
|
||||
|
||||
private void releaseServer() {
|
||||
private void releaseServer() throws Exception {
|
||||
MBeanServerFactory.releaseMBeanServer(getServer());
|
||||
MBeanTestUtils.resetMBeanServers();
|
||||
}
|
||||
|
||||
protected void onTearDown() throws Exception {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.jmx.support;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
|
@ -25,6 +24,8 @@ import javax.management.MBeanServerFactory;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.util.MBeanTestUtils;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
|
|
@ -34,26 +35,12 @@ public class MBeanServerFactoryBeanTests extends TestCase {
|
|||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
resetPlatformManager();
|
||||
MBeanTestUtils.resetMBeanServers();
|
||||
}
|
||||
|
||||
@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);
|
||||
MBeanTestUtils.resetMBeanServers();
|
||||
}
|
||||
|
||||
public void testGetObject() throws Exception {
|
||||
|
|
|
|||
|
|
@ -16,13 +16,16 @@
|
|||
|
||||
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.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.scripting.Messenger;
|
||||
import org.springframework.util.MBeanTestUtils;
|
||||
|
||||
import test.advice.CountingBeforeAdvice;
|
||||
|
||||
|
|
@ -31,41 +34,52 @@ import test.advice.CountingBeforeAdvice;
|
|||
* @author Chris Beams
|
||||
*/
|
||||
public final class AdvisedJRubyScriptFactoryTests {
|
||||
|
||||
|
||||
private static final Class<?> CLASS = AdvisedJRubyScriptFactoryTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
|
||||
private static final String FACTORYBEAN_CONTEXT = CLASSNAME + "-factoryBean.xml";
|
||||
private static final String APC_CONTEXT = CLASSNAME + "-beanNameAutoProxyCreator.xml";
|
||||
|
||||
@After
|
||||
public void resetMBeanServers() throws Exception {
|
||||
MBeanTestUtils.resetMBeanServers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdviseWithProxyFactoryBean() {
|
||||
ClassPathXmlApplicationContext ctx =
|
||||
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");
|
||||
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
||||
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
||||
|
||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||
assertEquals(0, advice.getCalls());
|
||||
bean.getMessage();
|
||||
assertEquals(1, advice.getCalls());
|
||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||
assertEquals(0, advice.getCalls());
|
||||
bean.getMessage();
|
||||
assertEquals(1, advice.getCalls());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdviseWithBeanNameAutoProxyCreator() {
|
||||
ClassPathXmlApplicationContext ctx =
|
||||
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");
|
||||
assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
|
||||
assertTrue("Bean is not an Advised object", bean instanceof Advised);
|
||||
|
||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||
assertEquals(0, advice.getCalls());
|
||||
bean.getMessage();
|
||||
assertEquals(1, advice.getCalls());
|
||||
CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
|
||||
assertEquals(0, advice.getCalls());
|
||||
bean.getMessage();
|
||||
assertEquals(1, advice.getCalls());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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