Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions test/org/apache/catalina/mbeans/TestConnectorMBean.java
Original file line number Diff line number Diff line change
@@ -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<ObjectName> 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<ObjectName> 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<ObjectName> 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();
}
}
147 changes: 147 additions & 0 deletions test/org/apache/catalina/mbeans/TestContextMBean.java
Original file line number Diff line number Diff line change
@@ -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<ObjectName> 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<ObjectName> 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<ObjectName> 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<ObjectName> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ObjectName> userDbBeans = mbeanServer.queryNames(
new ObjectName("Users:type=UserDatabase,*"), null);
Assert.assertFalse("UserDatabase MBeans should be registered",
userDbBeans.isEmpty());

tomcat.stop();
tomcat.destroy();
}
}
Loading