View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.tests.services.monitoring.jmx;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import javax.management.MBeanServer;
24  import javax.management.MalformedObjectNameException;
25  import javax.management.ObjectName;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.junit.Test;
30  import org.springframework.context.ConfigurableApplicationContext;
31  import org.springframework.context.support.ClassPathXmlApplicationContext;
32  
33  import ch.elca.el4j.core.context.ModuleApplicationContext;
34  
35  /**
36   * JUnit Test Class for the JMX package.
37   *
38   * <p>
39   * If one wants to observe the SpringBeans of this test environment, then the
40   * commented part at the end of the second method should be uncommented. The
41   * SpringBeans of both ApplicationContexts of the first test can be accessed by
42   * entering <A HREF="http://localhost:9092/">http://localhost:9092/ </A> at your
43   * web browser. The SpringBeans of the second test of the first
44   * ApplicationContext can be accessed by <A
45   * HREF="http://localhost:9093/">http://localhost:9093/ </A>. The ones of the
46   * second ApplicationContext can be viewed by a management console which
47   * supports JSR 160 (JMX Remoting) connections. One such console is MC4J which
48   * can be found at <A
49   * HREF="http://mc4j.sourceforge.net/">http://mc4j.sourceforge.net/ </A>.
50   *
51   * @svnLink $Revision: 3879 $;$Date: 2009-08-04 15:13:46 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/test/java/ch/elca/el4j/tests/services/monitoring/jmx/JmxTest.java $
52   *
53   * @author Raphael Boog (RBO)
54   */
55  public class JmxTest {
56  
57  	/**
58  	 * Private logger of this class.
59  	 */
60  	private static Logger s_logger = LoggerFactory.getLogger(JmxTest.class);
61  
62  	/**
63  	 * All xml files in the 'mandatory' folder.
64  	 */
65  	String m_mandatoryFiles = "classpath*:mandatory/*.xml";
66  
67  	/**
68  	 * Configuration file number 1.
69  	 */
70  	String m_fileName1 = "classpath:optional/beans1.xml";
71  
72  	/**
73  	 * Configuration file number 2.
74  	 */
75  	String m_fileName2 = "classpath:optional/beans2.xml";
76  
77  	/**
78  	 * Configuration file number 3.
79  	 */
80  	String m_fileName3 = "classpath:optional/beans3.xml";
81  
82  	/**
83  	 * Configuration file number 4.
84  	 */
85  	String m_fileName4 = "classpath:optional/beans4.xml";
86  
87  	/**
88  	 * This test loads two ApplicationContexts where MBeans are registered at
89  	 * the same MBean Server and checks if the MBeans are really registered at
90  	 * the MBean Server.
91  	 *
92  	 * @throws MalformedObjectNameException
93  	 *             The format of the String does not correspond to a valid
94  	 *             ObjectName
95  	 */
96  	@Test
97  	public void testRegisteredMBeanWithTwoAppCxtsAndOneJmxInstance()
98  		throws MalformedObjectNameException {
99  
100 		ConfigurableApplicationContext tac1
101 			= new ClassPathXmlApplicationContext(
102 				new String[] {m_mandatoryFiles, m_fileName1});
103 
104 		ConfigurableApplicationContext tac2
105 			= new ModuleApplicationContext(new String[] {
106 				m_mandatoryFiles, m_fileName2}, false);
107 
108 		ObjectName on1 = new ObjectName("MBean:name=foo1");
109 		ObjectName on2 = new ObjectName("MBean:name=foo2");
110 		ObjectName on3 = new ObjectName("SpringBean1:name=mBeanExporter");
111 		ObjectName on4 = new ObjectName("SpringBean2:name=foo2");
112 
113 		MBeanServer mBeanServer1 = (MBeanServer) tac1.getBean("mBeanServer");
114 
115 		MBeanServer mBeanServer2 = (MBeanServer) tac2.getBean("mBeanServer");
116 
117 		assertEquals("The two MBean Servers in the two Application Contexts "
118 			+ "should be the same.", mBeanServer1, mBeanServer2);
119 
120 		assertTrue("MBean with object name 'MBean:name=foo1' is not "
121 			+ "registered at the MBean Server", mBeanServer1.isRegistered(on1));
122 
123 		assertTrue("MBean with object name 'MBean:name=foo2' is not "
124 			+ "registered at the MBean Server", mBeanServer1.isRegistered(on2));
125 
126 		assertTrue("MBean with object name 'SpringBean1:name=mBeanExporter1' "
127 			+ "is not registered at the MBean Server", mBeanServer1
128 			.isRegistered(on3));
129 
130 		assertTrue("MBean with object name 'SpringBean2:name=foo2' is not "
131 			+ "registered at the MBean Server", mBeanServer1.isRegistered(on4));
132 
133 		// Shut down the application contexts
134 		tac1.close();
135 		tac2.close();
136 	}
137 
138 	/**
139 	 * This test loads two ApplicationContexts where MBeans are registered at
140 	 * two different MBean Servers and checks if the MBeans are really
141 	 * registered at the corresponding MBean Server. The only difference in
142 	 * configuration to the test before is that the 'defaultDomain' property of
143 	 * the MBean Server is overridden in both ApplicationContexts and that the
144 	 * second ApplicationContext can be connected by a JSR 160 JMX connection
145 	 * instead of html.
146 	 *
147 	 * @throws MalformedObjectNameException
148 	 *             The format of the string does not correspond to a valid
149 	 *             ObjectName
150 	 */
151 	@Test
152 	public void testRegisteredMBeanWithTwoAppCxtsAndTwoMBeanServers()
153 		throws MalformedObjectNameException {
154 
155 		ConfigurableApplicationContext tac1
156 			= new ClassPathXmlApplicationContext(
157 				new String[] {m_mandatoryFiles, m_fileName3});
158 
159 		ConfigurableApplicationContext tac2
160 			= new ClassPathXmlApplicationContext(
161 				new String[] {m_mandatoryFiles, m_fileName4});
162 
163 		ObjectName on1 = new ObjectName("MBean:name=foo3");
164 		ObjectName on2 = new ObjectName("MBean:name=foo4");
165 		ObjectName on3 = new ObjectName("SpringBean3:name=mBeanExporter");
166 		ObjectName on4 = new ObjectName("SpringBean4:name=foo4");
167 
168 		MBeanServer mBeanServer1 = (MBeanServer) tac1.getBean("mBeanServer");
169 
170 		MBeanServer mBeanServer2 = (MBeanServer) tac2.getBean("mBeanServer");
171 
172 		assertTrue("MBean with object name 'MBean:name=foo3' is not "
173 			+ "registered at the MBean Server 1", mBeanServer1
174 			.isRegistered(on1));
175 
176 		assertTrue("MBean with object name 'MBean:name=foo4' is not "
177 			+ "registered at the MBean Server 2", mBeanServer2
178 			.isRegistered(on2));
179 
180 		assertTrue("MBean with object name 'SpringBean3:name=mBeanExporter3' "
181 			+ "is not registered at the MBean Server 1", mBeanServer1
182 			.isRegistered(on3));
183 
184 		assertTrue("MBean with object name 'SpringBean4:name=foo4' is not "
185 			+ "registered at the MBean Server 2", mBeanServer2
186 			.isRegistered(on4));
187 
188 		s_logger.info("see source file to run it in non-stopping mode.");
189 		/*
190 		System.out.println("Waiting forever...");
191 		try {
192 			Thread.sleep(Long.MAX_VALUE);
193 		} catch (InterruptedException e) {
194 			String message = "Another thread has interrupted the current "
195 				+ "thread. The interrupted status of the current thread "
196 				+ "is cleared when this exception is thrown.";
197 			s_logger.error(message);
198 			throw new BaseRTException(message, e);
199 		}
200 		*/
201 		
202 		// Shut down the application contexts
203 		tac1.close();
204 		tac2.close();
205 	}
206 }