diff --git a/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java b/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java index b84996ad6de..ba8775956a4 100644 --- a/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java @@ -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; /** * Note: the JMX test suite requires the presence of the * jmxremote_optional.jar in your classpath. Thus, if you * run into the "Unsupported protocol: jmxmp" error, you will * need to download the - * JMX Remote API 1.0.1_04 Reference Implementation + * JMX Remote API 1.0.1_04 Reference Implementation * from Oracle and extract jmxremote_optional.jar into your * classpath, for example in the lib/ext folder of your JVM. * See also EBR-349. - * + * * @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 { diff --git a/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java b/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java index 71966431245..f2217bfd6ad 100644 --- a/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/support/MBeanServerFactoryBeanTests.java @@ -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 { diff --git a/spring-context/src/test/java/org/springframework/scripting/jruby/AdvisedJRubyScriptFactoryTests.java b/spring-context/src/test/java/org/springframework/scripting/jruby/AdvisedJRubyScriptFactoryTests.java index 8cdeb45aa9a..a3791761801 100644 --- a/spring-context/src/test/java/org/springframework/scripting/jruby/AdvisedJRubyScriptFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/scripting/jruby/AdvisedJRubyScriptFactoryTests.java @@ -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(); + } } } diff --git a/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java new file mode 100644 index 00000000000..eb60fe7648a --- /dev/null +++ b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java @@ -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); + } + +}