From bf20204922a7256219da04f8585790bdafddd1b7 Mon Sep 17 00:00:00 2001 From: Onkar Date: Sun, 12 Apr 2026 22:02:15 +0530 Subject: [PATCH] Improve/Refactor MBean Utilities and Add Unit Tests for Catalina MBeans --- .../catalina/mbeans/TestConnectorMBean.java | 104 +++++++++++++ .../catalina/mbeans/TestContextMBean.java | 147 ++++++++++++++++++ .../TestGlobalResourcesLifecycleListener.java | 99 ++++++++++++ .../catalina/mbeans/TestMBeanDumper.java | 120 ++++++++++++++ .../catalina/mbeans/TestMBeanFactory.java | 70 +++++++++ .../catalina/mbeans/TestMBeanUtils.java | 134 ++++++++++++++++ .../catalina/mbeans/TestServiceMBean.java | 126 +++++++++++++++ 7 files changed, 800 insertions(+) create mode 100644 test/org/apache/catalina/mbeans/TestConnectorMBean.java create mode 100644 test/org/apache/catalina/mbeans/TestContextMBean.java create mode 100644 test/org/apache/catalina/mbeans/TestGlobalResourcesLifecycleListener.java create mode 100644 test/org/apache/catalina/mbeans/TestMBeanDumper.java create mode 100644 test/org/apache/catalina/mbeans/TestMBeanFactory.java create mode 100644 test/org/apache/catalina/mbeans/TestMBeanUtils.java create mode 100644 test/org/apache/catalina/mbeans/TestServiceMBean.java diff --git a/test/org/apache/catalina/mbeans/TestConnectorMBean.java b/test/org/apache/catalina/mbeans/TestConnectorMBean.java new file mode 100644 index 000000000000..4ae1d2341e3e --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestConnectorMBean.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link ConnectorMBean}. + */ +public class TestConnectorMBean extends TomcatBaseTest { + + @Test + public void testConnectorMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // A connector MBean should be registered + Set connectors = mbeanServer.queryNames( + new ObjectName("Tomcat:type=Connector,*"), null); + Assert.assertFalse("At least one Connector MBean should be registered", + connectors.isEmpty()); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testConnectorMBeanGetAttribute() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + Set connectors = mbeanServer.queryNames( + new ObjectName("Tomcat:type=Connector,*"), null); + Assert.assertFalse(connectors.isEmpty()); + + ObjectName connectorName = connectors.iterator().next(); + + // getAttribute for scheme should return a value + Object scheme = mbeanServer.getAttribute(connectorName, "scheme"); + Assert.assertNotNull("scheme attribute should not be null", scheme); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testConnectorMBeanSetAttribute() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + Set connectors = mbeanServer.queryNames( + new ObjectName("Tomcat:type=Connector,*"), null); + Assert.assertFalse(connectors.isEmpty()); + + ObjectName connectorName = connectors.iterator().next(); + + // setAttribute for maxPostSize + mbeanServer.setAttribute(connectorName, + new javax.management.Attribute("maxPostSize", "4194304")); + + // Verify the attribute was updated + Object maxPostSize = mbeanServer.getAttribute(connectorName, + "maxPostSize"); + Assert.assertNotNull(maxPostSize); + + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/test/org/apache/catalina/mbeans/TestContextMBean.java b/test/org/apache/catalina/mbeans/TestContextMBean.java new file mode 100644 index 000000000000..36bd7aa902e1 --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestContextMBean.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link ContextMBean} via MBeanServer. + */ +public class TestContextMBean extends TomcatBaseTest { + + @Test + public void testContextMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + Context ctx = tomcat.addContext("/test", + getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + Set contexts = mbeanServer.queryNames( + new ObjectName("Tomcat:j2eeType=WebModule,*"), null); + Assert.assertFalse("At least one WebModule MBean should be registered", + contexts.isEmpty()); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testContextMBeanAttributes() throws Exception { + Tomcat tomcat = getTomcatInstance(); + Context ctx = tomcat.addContext("", + getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + Set contexts = mbeanServer.queryNames( + new ObjectName("Tomcat:j2eeType=WebModule,*"), null); + Assert.assertFalse(contexts.isEmpty()); + + ObjectName contextName = contexts.iterator().next(); + + // Verify the MBeanInfo is accessible + Assert.assertNotNull(mbeanServer.getMBeanInfo(contextName)); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testNamingResourcesMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", + getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // NamingResources MBeans should be registered for each context + Set namingResources = mbeanServer.queryNames( + new ObjectName("Tomcat:type=NamingResources,*"), null); + Assert.assertFalse( + "At least one NamingResources MBean should be registered", + namingResources.isEmpty()); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testValveMBeansRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", + getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // Valve MBeans should include at least StandardEngineValve + Set valves = mbeanServer.queryNames( + new ObjectName("Tomcat:type=Valve,*"), null); + Assert.assertFalse("Valve MBeans should be registered", + valves.isEmpty()); + + // Check for standard valve names + boolean foundEngineValve = false; + for (ObjectName on : valves) { + if (on.toString().contains("StandardEngineValve")) { + foundEngineValve = true; + break; + } + } + Assert.assertTrue("StandardEngineValve should be registered", + foundEngineValve); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testMapperMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", + getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName mapperName = new ObjectName("Tomcat:type=Mapper"); + Assert.assertTrue("Mapper MBean should be registered", + mbeanServer.isRegistered(mapperName)); + + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/test/org/apache/catalina/mbeans/TestGlobalResourcesLifecycleListener.java b/test/org/apache/catalina/mbeans/TestGlobalResourcesLifecycleListener.java new file mode 100644 index 000000000000..8a0e1b7b7053 --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestGlobalResourcesLifecycleListener.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link GlobalResourcesLifecycleListener}. + */ +public class TestGlobalResourcesLifecycleListener extends TomcatBaseTest { + + @Test + public void testListenerRegistration() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.enableNaming(); + + GlobalResourcesLifecycleListener listener = + new GlobalResourcesLifecycleListener(); + tomcat.getServer().addLifecycleListener(listener); + + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + // Listener should have been invoked on start + Assert.assertNotNull("component should be set after start", + listener.component); + + tomcat.stop(); + + // component should be cleared after stop + Assert.assertNull("component should be null after stop", + listener.component); + + tomcat.destroy(); + } + + + @Test + public void testListenerWithUserDatabase() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.enableNaming(); + + // Add GlobalNamingResources with a UserDatabase + org.apache.catalina.deploy.NamingResourcesImpl namingResources = + new org.apache.catalina.deploy.NamingResourcesImpl(); + org.apache.tomcat.util.descriptor.web.ContextResource resource = + new org.apache.tomcat.util.descriptor.web.ContextResource(); + resource.setName("UserDatabase"); + resource.setAuth("Container"); + resource.setType("org.apache.catalina.UserDatabase"); + resource.setProperty("factory", + "org.apache.catalina.users.MemoryUserDatabaseFactory"); + resource.setProperty("pathname", "conf/tomcat-users.xml"); + namingResources.addResource(resource); + tomcat.getServer().setGlobalNamingResources(namingResources); + + GlobalResourcesLifecycleListener listener = + new GlobalResourcesLifecycleListener(); + tomcat.getServer().addLifecycleListener(listener); + + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // Check for UserDatabase MBean + Set userDbBeans = mbeanServer.queryNames( + new ObjectName("Users:type=UserDatabase,*"), null); + Assert.assertFalse("UserDatabase MBeans should be registered", + userDbBeans.isEmpty()); + + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/test/org/apache/catalina/mbeans/TestMBeanDumper.java b/test/org/apache/catalina/mbeans/TestMBeanDumper.java new file mode 100644 index 000000000000..24fd5d3edefc --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestMBeanDumper.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import java.util.HashSet; +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link MBeanDumper}. + */ +public class TestMBeanDumper { + + @Test + public void testDumpBeansEmpty() { + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + Set emptySet = new HashSet<>(); + + String result = MBeanDumper.dumpBeans(mbeanServer, emptySet); + Assert.assertNotNull(result); + Assert.assertEquals("", result); + } + + + @Test + public void testDumpBeansWithExisting() throws Exception { + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // Query for any existing MBeans in the JVM + Set names = mbeanServer.queryNames( + new ObjectName("java.lang:type=Runtime"), null); + + if (!names.isEmpty()) { + String result = MBeanDumper.dumpBeans(mbeanServer, names); + Assert.assertNotNull(result); + Assert.assertTrue("Should contain Name: prefix", + result.contains("Name: ")); + } + } + + + @Test + public void testEscapeNoNewline() { + String input = "simple string without newlines"; + Assert.assertEquals(input, MBeanDumper.escape(input)); + } + + + @Test + public void testEscapeWithNewline() { + String input = "line1\nline2"; + String result = MBeanDumper.escape(input); + + Assert.assertNotNull(result); + Assert.assertTrue("Should contain escaped newline", + result.contains("\\n")); + } + + + @Test + public void testEscapeWithMultipleNewlines() { + String input = "line1\nline2\nline3"; + String result = MBeanDumper.escape(input); + + Assert.assertNotNull(result); + } + + + @Test + public void testEscapeWithTrailingNewline() { + String input = "content\n"; + String result = MBeanDumper.escape(input); + + Assert.assertNotNull(result); + Assert.assertTrue("Should contain escaped newline", + result.contains("\\n")); + } + + + @Test + public void testEscapeLongLine() { + // Create a string longer than 78 chars to test line wrapping + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 100; i++) { + sb.append('a'); + } + sb.append('\n'); + sb.append("second line"); + + String result = MBeanDumper.escape(sb.toString()); + Assert.assertNotNull(result); + } + + + @Test + public void testEscapeEmpty() { + Assert.assertEquals("", MBeanDumper.escape("")); + } +} diff --git a/test/org/apache/catalina/mbeans/TestMBeanFactory.java b/test/org/apache/catalina/mbeans/TestMBeanFactory.java new file mode 100644 index 000000000000..5011a1983bb2 --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestMBeanFactory.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link MBeanFactory}. + */ +public class TestMBeanFactory extends TomcatBaseTest { + + @Test + public void testMBeanFactoryRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + // MBeanFactory should be registered on startup + ObjectName factoryName = new ObjectName("Tomcat:type=MBeanFactory"); + Assert.assertTrue("MBeanFactory should be registered", + mbeanServer.isRegistered(factoryName)); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testMBeanFactoryAttributes() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName factoryName = new ObjectName("Tomcat:type=MBeanFactory"); + Assert.assertTrue(mbeanServer.isRegistered(factoryName)); + + // Verify MBeanInfo is accessible + Assert.assertNotNull(mbeanServer.getMBeanInfo(factoryName)); + + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/test/org/apache/catalina/mbeans/TestMBeanUtils.java b/test/org/apache/catalina/mbeans/TestMBeanUtils.java new file mode 100644 index 000000000000..cf966a1cb932 --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestMBeanUtils.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import javax.management.MBeanServer; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.tomcat.util.modeler.ManagedBean; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link MBeanUtils}. + */ +public class TestMBeanUtils { + + @Test + public void testCreateRegistry() { + Registry registry = MBeanUtils.createRegistry(); + Assert.assertNotNull("Registry should not be null", registry); + } + + + @Test + public void testCreateRegistrySingleton() { + Registry registry1 = MBeanUtils.createRegistry(); + Registry registry2 = MBeanUtils.createRegistry(); + Assert.assertSame("createRegistry should return singleton", + registry1, registry2); + } + + + @Test + public void testCreateServer() { + MBeanServer server = MBeanUtils.createServer(); + Assert.assertNotNull("MBeanServer should not be null", server); + } + + + @Test + public void testCreateServerSingleton() { + MBeanServer server1 = MBeanUtils.createServer(); + MBeanServer server2 = MBeanUtils.createServer(); + Assert.assertSame("createServer should return singleton", + server1, server2); + } + + + @Test + public void testRegistryContainsManagedBeans() { + Registry registry = MBeanUtils.createRegistry(); + + // These managed beans should be loaded from mbeans-descriptors.xml + ManagedBean serverBean = registry.findManagedBean("StandardServer"); + Assert.assertNotNull("StandardServer managed bean should exist", + serverBean); + + ManagedBean engineBean = registry.findManagedBean("StandardEngine"); + Assert.assertNotNull("StandardEngine managed bean should exist", + engineBean); + + ManagedBean hostBean = registry.findManagedBean("StandardHost"); + Assert.assertNotNull("StandardHost managed bean should exist", + hostBean); + + ManagedBean contextBean = registry.findManagedBean("StandardContext"); + Assert.assertNotNull("StandardContext managed bean should exist", + contextBean); + } + + + @Test + public void testRegistryContainsConnectorBean() { + Registry registry = MBeanUtils.createRegistry(); + + ManagedBean connectorBean = registry.findManagedBean("CoyoteConnector"); + Assert.assertNotNull("CoyoteConnector managed bean should exist", + connectorBean); + } + + + @Test + public void testRegistryContainsUserDatabaseBeans() { + Registry registry = MBeanUtils.createRegistry(); + + ManagedBean userDbBean = registry.findManagedBean("MemoryUserDatabase"); + Assert.assertNotNull("MemoryUserDatabase managed bean should exist", + userDbBean); + + ManagedBean groupBean = registry.findManagedBean("Group"); + Assert.assertNotNull("Group managed bean should exist", groupBean); + + ManagedBean roleBean = registry.findManagedBean("Role"); + Assert.assertNotNull("Role managed bean should exist", roleBean); + + ManagedBean userBean = registry.findManagedBean("User"); + Assert.assertNotNull("User managed bean should exist", userBean); + } + + + @Test + public void testRegistryContainsValveBeans() { + Registry registry = MBeanUtils.createRegistry(); + + ManagedBean accessLogBean = registry.findManagedBean("AccessLogValve"); + Assert.assertNotNull("AccessLogValve managed bean should exist", + accessLogBean); + } + + + @Test + public void testRegistryContainsRealmBeans() { + Registry registry = MBeanUtils.createRegistry(); + + ManagedBean realmBean = registry.findManagedBean("LockOutRealm"); + Assert.assertNotNull("LockOutRealm managed bean should exist", + realmBean); + } +} diff --git a/test/org/apache/catalina/mbeans/TestServiceMBean.java b/test/org/apache/catalina/mbeans/TestServiceMBean.java new file mode 100644 index 000000000000..0fb994675cfe --- /dev/null +++ b/test/org/apache/catalina/mbeans/TestServiceMBean.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.catalina.mbeans; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * Tests for {@link ServiceMBean}. + */ +public class TestServiceMBean extends TomcatBaseTest { + + @Test + public void testServiceMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName serviceName = new ObjectName("Tomcat:type=Service"); + Assert.assertTrue("Service MBean should be registered", + mbeanServer.isRegistered(serviceName)); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testServiceMBeanInfo() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName serviceName = new ObjectName("Tomcat:type=Service"); + + // MBeanInfo should be accessible + Assert.assertNotNull(mbeanServer.getMBeanInfo(serviceName)); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testServerMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName serverName = new ObjectName("Tomcat:type=Server"); + Assert.assertTrue("Server MBean should be registered", + mbeanServer.isRegistered(serverName)); + + Object serverInfo = mbeanServer.getAttribute(serverName, "serverInfo"); + Assert.assertNotNull("Server info should not be null", serverInfo); + Assert.assertTrue("Server info should contain Tomcat", + serverInfo.toString().contains("Tomcat")); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testEngineMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + ObjectName engineName = new ObjectName("Tomcat:type=Engine"); + Assert.assertTrue("Engine MBean should be registered", + mbeanServer.isRegistered(engineName)); + + tomcat.stop(); + tomcat.destroy(); + } + + + @Test + public void testHostMBeanRegistered() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.addContext("", getTemporaryDirectory().getAbsolutePath()); + tomcat.start(); + + MBeanServer mbeanServer = Registry.getRegistry(null).getMBeanServer(); + + Set hosts = mbeanServer.queryNames( + new ObjectName("Tomcat:type=Host,*"), null); + Assert.assertFalse("At least one Host MBean should be registered", + hosts.isEmpty()); + + tomcat.stop(); + tomcat.destroy(); + } +}