Search in sources :

Example 21 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project tomcat by apache.

the class TestWarDirContext method testReservedJNDIFileNamesNoCache.

/*
     * Additional test following on from SPR-7350 above to check files that
     * contain JNDI reserved characters can be served when caching is disabled.
     */
@Test
public void testReservedJNDIFileNamesNoCache() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-fragments");
    // app dir is relative to server home
    StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    StandardRoot root = new StandardRoot();
    root.setCachingAllowed(true);
    ctxt.setResources(root);
    skipTldsForResourceJars(ctxt);
    tomcat.start();
    // Should be found in resources.jar
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/test/'singlequote.jsp");
    Assert.assertEquals("<p>'singlequote.jsp in resources.jar</p>", bc.toString());
    // Should be found in file system
    bc = getUrl("http://localhost:" + getPort() + "/test/'singlequote2.jsp");
    Assert.assertEquals("<p>'singlequote2.jsp in file system</p>", bc.toString());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardContext(org.apache.catalina.core.StandardContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 22 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project tomcat by apache.

the class TestWarDirContext method testReservedJNDIFileNamesWithCache.

/*
     * Additional test following on from SPR-7350 above to check files that
     * contain JNDI reserved characters can be served when caching is enabled.
     */
@Test
public void testReservedJNDIFileNamesWithCache() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-fragments");
    // app dir is relative to server home
    StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    StandardRoot root = new StandardRoot();
    root.setCachingAllowed(true);
    ctxt.setResources(root);
    tomcat.start();
    // Should be found in resources.jar
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/test/'singlequote.jsp");
    Assert.assertEquals("<p>'singlequote.jsp in resources.jar</p>", bc.toString());
    // Should be found in file system
    bc = getUrl("http://localhost:" + getPort() + "/test/'singlequote2.jsp");
    Assert.assertEquals("<p>'singlequote2.jsp in file system</p>", bc.toString());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardContext(org.apache.catalina.core.StandardContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 23 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project hazelcast by hazelcast.

the class ServiceLoaderTest method testClassIteratorInTomcat_whenClassesInBothLibs.

@Test
public void testClassIteratorInTomcat_whenClassesInBothLibs() throws Exception {
    ClassLoader launchClassLoader = this.getClass().getClassLoader();
    ClassLoader webappClassLoader;
    // setup embedded tomcat
    Tomcat tomcat = new Tomcat();
    // 8080 may be used by some other tests
    tomcat.setPort(13256);
    Context ctx = tomcat.addContext("", null);
    // Map target/classes as WEB-INF/classes, so webapp classloader
    // will locate compiled production classes in the webapp classpath.
    // The purpose of this setup is to make project classes available
    // to both launch classloader and webapplication classloader,
    // modeling a Tomcat deployment in which Hazelcast JARs are deployed
    // in both tomcat/lib and webapp/lib
    File webInfClasses = new File("target/classes");
    WebResourceRoot resources = new StandardRoot(ctx);
    resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", webInfClasses.getAbsolutePath(), "/"));
    ctx.setResources(resources);
    TestServiceLoaderServlet testServlet = new TestServiceLoaderServlet();
    Wrapper wrapper = tomcat.addServlet("", "testServlet", testServlet);
    wrapper.setLoadOnStartup(1);
    ctx.addServletMappingDecoded("/", "testServlet");
    tomcat.start();
    try {
        assertTrueEventually(() -> assertTrue(testServlet.isInitDone()));
        assertNull("No failure is expected from servlet init() method", testServlet.failure());
        webappClassLoader = testServlet.getWebappClassLoader();
        assertNotEquals(launchClassLoader, webappClassLoader);
        Iterator<? extends Class<?>> iterator = ServiceLoader.classIterator(DataSerializerHook.class, "com.hazelcast.DataSerializerHook", webappClassLoader);
        assertTrue(iterator.hasNext());
        while (iterator.hasNext()) {
            Class<?> klass = iterator.next();
            assertEquals(launchClassLoader, klass.getClassLoader());
        }
    } finally {
        tomcat.stop();
    }
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) DirResourceSet(org.apache.catalina.webresources.DirResourceSet) StandardRoot(org.apache.catalina.webresources.StandardRoot) URLClassLoader(java.net.URLClassLoader) File(java.io.File) WebResourceRoot(org.apache.catalina.WebResourceRoot) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

StandardRoot (org.apache.catalina.webresources.StandardRoot)23 File (java.io.File)18 StandardContext (org.apache.catalina.core.StandardContext)14 Tomcat (org.apache.catalina.startup.Tomcat)13 Test (org.junit.Test)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 Context (org.apache.catalina.Context)6 WebResourceRoot (org.apache.catalina.WebResourceRoot)4 StandardJarScanner (org.apache.tomcat.util.scan.StandardJarScanner)4 URLClassLoader (java.net.URLClassLoader)3 ServletContext (jakarta.servlet.ServletContext)2 MalformedURLException (java.net.MalformedURLException)2 HashSet (java.util.HashSet)2 WebappLoader (org.apache.catalina.loader.WebappLoader)2 DirResourceSet (org.apache.catalina.webresources.DirResourceSet)2 CoreContainerSystem (org.apache.openejb.core.CoreContainerSystem)2 JarScanner (org.apache.tomcat.JarScanner)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 TomEEWebappClassLoader (org.apache.tomee.catalina.TomEEWebappClassLoader)2