Search in sources :

Example 86 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat by apache.

the class TestWebappClassLoaderThreadLocalMemoryLeak method testThreadLocalLeak2.

@Test
public void testThreadLocalLeak2() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "leakServlet2", "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMappingDecoded("/leak2", "leakServlet2");
    tomcat.start();
    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);
    // Configure logging filter to check leak message appears
    TesterLogValidationFilter f = TesterLogValidationFilter.add(null, "The web application [ROOT] created a ThreadLocal with key of", null, "org.apache.catalina.loader.WebappClassLoaderBase");
    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2", new ByteChunk(), null);
    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;
    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost()).findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);
    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TesterLogValidationFilter(org.apache.tomcat.unittest.TesterLogValidationFilter) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardHost(org.apache.catalina.core.StandardHost) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 87 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat by apache.

the class TestMapper method createHost.

private synchronized Host createHost(String name) {
    Host host = hostMap.get(name);
    if (host == null) {
        host = new StandardHost();
        host.setName(name);
        hostMap.put(name, host);
    }
    return host;
}
Also used : StandardHost(org.apache.catalina.core.StandardHost) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host)

Example 88 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat by apache.

the class TestAbstractArchiveResource method testJarGetURL.

@Test
public void testJarGetURL() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File("test/webapp");
    Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
    skipTldsForResourceJars(ctx);
    ((StandardHost) tomcat.getHost()).setUnpackWARs(false);
    tomcat.start();
    WebResource webResource = ctx.getResources().getClassLoaderResource("/META-INF/tags/echo.tag");
    StringBuilder expectedURL = new StringBuilder("jar:");
    expectedURL.append(docBase.getCanonicalFile().toURI().toURL().toString());
    expectedURL.append("WEB-INF/lib/test-lib.jar!/META-INF/tags/echo.tag");
    Assert.assertEquals(expectedURL.toString(), webResource.getURL().toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) StandardHost(org.apache.catalina.core.StandardHost) WebResource(org.apache.catalina.WebResource) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 89 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat by apache.

the class TestCachedResource method testDirectoryListingsPackedWar.

@Test
public void testDirectoryListingsPackedWar() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File("test/webresources/war-url-connection.war");
    Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
    ((StandardHost) tomcat.getHost()).setUnpackWARs(false);
    tomcat.start();
    WebResourceRoot root = ctx.getResources();
    URL d1 = root.getResource("/").getURL();
    try (InputStream is = d1.openStream()) {
        Assert.assertNotNull(is);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) StandardHost(org.apache.catalina.core.StandardHost) InputStream(java.io.InputStream) File(java.io.File) URL(java.net.URL) WebResourceRoot(org.apache.catalina.WebResourceRoot) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 90 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat by apache.

the class TestHandlerIntegration method testToURI.

@Test
public void testToURI() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File("test/webresources/war-url-connection.war");
    Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
    skipTldsForResourceJars(ctx);
    ((StandardHost) tomcat.getHost()).setUnpackWARs(false);
    tomcat.start();
    URL url = ctx.getServletContext().getResource("/index.html");
    try {
        url.toURI();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File) URL(java.net.URL) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

StandardHost (org.apache.catalina.core.StandardHost)90 File (java.io.File)48 Context (org.apache.catalina.Context)38 StandardContext (org.apache.catalina.core.StandardContext)36 Host (org.apache.catalina.Host)29 Test (org.junit.Test)26 Tomcat (org.apache.catalina.startup.Tomcat)16 Container (org.apache.catalina.Container)12 StandardEngine (org.apache.catalina.core.StandardEngine)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 IOException (java.io.IOException)10 URL (java.net.URL)8 Service (org.apache.catalina.Service)8 HostConfig (org.apache.catalina.startup.HostConfig)8 InitialContext (javax.naming.InitialContext)7 Engine (org.apache.catalina.Engine)7 InputStream (java.io.InputStream)6 ObjectName (javax.management.ObjectName)6 ReplicatedContext (org.apache.catalina.ha.context.ReplicatedContext)6 ArrayList (java.util.ArrayList)5