View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.jmx;
18  
19  import javax.management.MBeanServer;
20  import javax.management.MBeanServerFactory;
21  import javax.management.ObjectName;
22  
23  import org.junit.After;
24  import org.junit.Before;
25  
26  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
27  import org.springframework.context.ConfigurableApplicationContext;
28  import org.springframework.context.support.GenericApplicationContext;
29  import org.springframework.jmx.export.MBeanExporter;
30  import org.springframework.tests.TestGroup;
31  import org.springframework.util.MBeanTestUtils;
32  
33  import static org.junit.Assert.*;
34  
35  /**
36   * <strong>Note:</strong> certain tests throughout this hierarchy require the presence of
37   * the {@code jmxremote_optional.jar} in your classpath. For this reason, these tests are
38   * run only if {@link TestGroup#JMXMP} is enabled. If you wish to run these tests, follow
39   * the instructions in the TestGroup class to enable JMXMP tests. If you run into the
40   * <em>"Unsupported protocol: jmxmp"</em> error, you will need to download the
41   * <a href="http://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html">
42   * JMX Remote API 1.0.1_04 Reference Implementation</a> from Oracle and extract
43   * {@code jmxremote_optional.jar} into your classpath, for example in the {@code lib/ext}
44   * folder of your JVM.
45   * See also <a href="https://issuetracker.springsource.com/browse/EBR-349">EBR-349</a>.
46   *
47   * @author Rob Harrop
48   * @author Juergen Hoeller
49   * @author Sam Brannen
50   * @author Chris Beams
51   * @author Stephane Nicoll
52   */
53  public abstract class AbstractMBeanServerTests {
54  
55  	protected MBeanServer server;
56  
57  
58  	@Before
59  	public final void setUp() throws Exception {
60  		this.server = MBeanServerFactory.createMBeanServer();
61  		try {
62  			onSetUp();
63  		}
64  		catch (Exception ex) {
65  			releaseServer();
66  			throw ex;
67  		}
68  	}
69  
70  	protected ConfigurableApplicationContext loadContext(String configLocation) {
71  		GenericApplicationContext ctx = new GenericApplicationContext();
72  		new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(configLocation);
73  		ctx.getDefaultListableBeanFactory().registerSingleton("server", this.server);
74  		ctx.refresh();
75  		return ctx;
76  	}
77  
78  	@After
79  	public void tearDown() throws Exception {
80  		releaseServer();
81  		onTearDown();
82  	}
83  
84  	private void releaseServer() throws Exception {
85  		MBeanServerFactory.releaseMBeanServer(getServer());
86  		MBeanTestUtils.resetMBeanServers();
87  	}
88  
89  	protected void onTearDown() throws Exception {
90  	}
91  
92  	protected void onSetUp() throws Exception {
93  	}
94  
95  	public MBeanServer getServer() {
96  		return this.server;
97  	}
98  
99  	/**
100 	 * Start the specified {@link MBeanExporter}.
101 	 */
102 	protected void start(MBeanExporter exporter) {
103 		exporter.afterPropertiesSet();
104 		exporter.afterSingletonsInstantiated();
105 	}
106 
107 	protected void assertIsRegistered(String message, ObjectName objectName) {
108 		assertTrue(message, getServer().isRegistered(objectName));
109 	}
110 
111 	protected void assertIsNotRegistered(String message, ObjectName objectName) {
112 		assertFalse(message, getServer().isRegistered(objectName));
113 	}
114 
115 }