1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.management.sampled;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.io.File;
22 import java.util.Set;
23
24 import javax.management.MBeanServer;
25 import javax.management.ObjectName;
26
27 import net.sf.ehcache.AbstractCacheTest;
28 import net.sf.ehcache.CacheManager;
29 import net.sf.ehcache.config.Configuration;
30 import net.sf.ehcache.config.ConfigurationFactory;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /***
39 * Tests for testing the mbean registration provider
40 *
41 * @author Abhishek Sanoujam
42 * @version $Id: MBeanRegistrationProviderTest.java 1178 2009-09-23 23:50:15Z
43 * asingh
44 * $
45 */
46 public class MBeanRegistrationProviderTest extends AbstractCacheTest {
47
48 private static final Logger LOG = LoggerFactory.getLogger(MBeanRegistrationProviderTest.class.getName());
49
50 private MBeanServer mbeanServer;
51
52 private CacheManager cacheManager;
53
54 @Override
55 @Before
56 public void setUp() throws Exception {
57 super.setUp();
58 mbeanServer = createMBeanServer();
59 cleanUpExistingMbeans();
60 }
61
62 @Override
63 @After
64 public void tearDown() throws Exception {
65 if (cacheManager != null) {
66 cacheManager.shutdown();
67 }
68 super.tearDown();
69 assertEquals(0, mbeanServer.queryNames(new ObjectName("net.sf.ehcache:*"), null).size());
70 cacheManager = null;
71 cleanUpExistingMbeans();
72 }
73
74 private void cleanUpExistingMbeans() throws Exception {
75 Set<ObjectName> queryNames = mbeanServer.queryNames(new ObjectName("net.sf.ehcache:*"), null);
76 for (ObjectName name : queryNames) {
77 mbeanServer.unregisterMBean(name);
78 }
79 }
80
81 @Test
82 public void testMonitoringOn() throws Exception {
83 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-on.xml");
84 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
85 cacheManager = new CacheManager(configuration);
86 assertSampledMBeansGroupRegistered(3);
87 assertCacheManagerMBeansRegistered("cacheManagerOn", 1);
88 }
89
90 private void assertSampledMBeansGroupRegistered(int size) throws Exception {
91 Set queryNames = mbeanServer.queryNames(new ObjectName(SampledEhcacheMBeans.GROUP_ID + ":*"), null);
92 assertEquals(size, queryNames.size());
93 }
94
95 private void assertCacheManagerMBeansRegistered(String cacheManagerName, int size) throws Exception {
96 Set queryNames = mbeanServer.queryNames(SampledEhcacheMBeans.getCacheManagerObjectName(null, cacheManagerName), null);
97 assertEquals(size, queryNames.size());
98 }
99
100 private void assertCacheManagerMBeansRegistered(int size) throws Exception {
101 Set queryNames = mbeanServer.queryNames(SampledEhcacheMBeans.getQueryCacheManagersObjectName(null), null);
102 assertEquals(size, queryNames.size());
103 }
104
105 @Test
106 public void testMonitoringOff() throws Exception {
107 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-off.xml");
108 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
109 cacheManager = new CacheManager(configuration);
110 assertSampledMBeansGroupRegistered(0);
111 assertCacheManagerMBeansRegistered("cacheManagerOff", 0);
112 }
113
114 @Test
115 public void testMonitoringAutodetect() throws Exception {
116 System.setProperty("tc.active", "false");
117 doTestMonitoringAutodetect(false);
118
119 System.setProperty("tc.active", "true");
120 doTestMonitoringAutodetect(true);
121
122
123 System.setProperty("tc.active", "false");
124 }
125
126 public void doTestMonitoringAutodetect(boolean dsoActive) throws Exception {
127 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-autodetect.xml");
128 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
129 cacheManager = new CacheManager(configuration);
130 if (dsoActive) {
131 assertSampledMBeansGroupRegistered(3);
132 assertCacheManagerMBeansRegistered("cacheManagerAutoDetect", 1);
133 } else {
134 assertSampledMBeansGroupRegistered(0);
135 assertCacheManagerMBeansRegistered("cacheManagerAutoDetect", 0);
136 }
137 }
138
139 @Test
140 public void testMultipleCacheManagerDifferentNames() throws Exception {
141 System.setProperty("tc.active", "true");
142 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-autodetect.xml");
143 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
144 cacheManager = new CacheManager(configuration);
145 assertSampledMBeansGroupRegistered(3);
146 assertCacheManagerMBeansRegistered("cacheManagerAutoDetect", 1);
147
148 file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-on.xml");
149 configuration = ConfigurationFactory.parseConfiguration(file);
150 CacheManager otherCacheManager = new CacheManager(configuration);
151 assertSampledMBeansGroupRegistered(3 + 3);
152 assertCacheManagerMBeansRegistered(2);
153
154 cacheManager.shutdown();
155 otherCacheManager.shutdown();
156
157 System.setProperty("tc.active", "false");
158 }
159
160 @Test
161 public void testMultipleCacheManagerSameNames() throws Exception {
162 int count = 8;
163 CacheManager[] managers = new CacheManager[count];
164 for (int i = 0; i < count; i++) {
165 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-on.xml");
166 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
167 managers[i] = new CacheManager(configuration);
168 assertSampledMBeansGroupRegistered(3 * (i + 1));
169 assertCacheManagerMBeansRegistered(i + 1);
170 }
171
172 CacheManager[] duplicates = new CacheManager[count];
173 for (int i = 0; i < count; i++) {
174 File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-on.xml");
175 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
176
177 duplicates[i] = new CacheManager(configuration);
178 assertSampledMBeansGroupRegistered(3 * (i + 1) + count * 3);
179 assertCacheManagerMBeansRegistered((i + 1) + count);
180 }
181
182 for (CacheManager mgr : managers) {
183 mgr.shutdown();
184 }
185 for (CacheManager mgr : duplicates) {
186 mgr.shutdown();
187 }
188 assertSampledMBeansGroupRegistered(0);
189 assertCacheManagerMBeansRegistered(0);
190 }
191
192 @Test
193 public void testInvalidCacheNames() throws Exception {
194 System.setProperty("tc.active", "true");
195
196 File file = new File(TEST_CONFIG_DIR + "ehcache-invalid-cache-names.xml");
197 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
198 cacheManager = new CacheManager(configuration);
199 assertSampledMBeansGroupRegistered(4);
200 assertCacheManagerMBeansRegistered("invalidCacheNames", 1);
201
202 System.setProperty("tc.active", "false");
203 }
204
205 @Test
206 public void testInvalidCacheManagerName() throws Exception {
207 System.setProperty("tc.active", "true");
208
209 File file = new File(TEST_CONFIG_DIR + "ehcache-invalid-cache-manager-name.xml");
210 Configuration configuration = ConfigurationFactory.parseConfiguration(file);
211 cacheManager = new CacheManager(configuration);
212 assertSampledMBeansGroupRegistered(1);
213 assertCacheManagerMBeansRegistered("invalid:CacheManagerName", 1);
214
215 System.setProperty("tc.active", "false");
216 }
217 }