Search in sources :

Example 56 with StandardContext

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

the class TestHttpServlet method doTestHead.

private void doTestHead(Servlet servlet) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    Wrapper w = Tomcat.addServlet(ctx, "TestServlet", servlet);
    // Not all need/use this but it is simpler to set it for all
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "TestServlet");
    tomcat.start();
    Map<String, List<String>> getHeaders = new CaseInsensitiveKeyMap<>();
    String path = "http://localhost:" + getPort() + "/test";
    ByteChunk out = new ByteChunk();
    int rc = getUrl(path, out, getHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    out.recycle();
    Map<String, List<String>> headHeaders = new HashMap<>();
    rc = headUrl(path, out, headHeaders);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Headers should be the same (apart from Date)
    Assert.assertEquals(getHeaders.size(), headHeaders.size());
    for (Map.Entry<String, List<String>> getHeader : getHeaders.entrySet()) {
        String headerName = getHeader.getKey();
        if ("date".equalsIgnoreCase(headerName)) {
            continue;
        }
        Assert.assertTrue(headerName, headHeaders.containsKey(headerName));
        List<String> getValues = getHeader.getValue();
        List<String> headValues = headHeaders.get(headerName);
        Assert.assertEquals(getValues.size(), headValues.size());
        for (String value : getValues) {
            Assert.assertTrue(headValues.contains(value));
        }
    }
    tomcat.stop();
}
Also used : Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) StandardContext(org.apache.catalina.core.StandardContext) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) HashMap(java.util.HashMap) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Map(java.util.Map)

Example 57 with StandardContext

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

the class TestPersistentManagerIntegration method testCreateSessionAndPassivate.

@Test
public void testCreateSessionAndPassivate() throws IOException, LifecycleException, ClassNotFoundException {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    ctx.setDistributable(true);
    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMappingDecoded("/dummy", "DummyServlet");
    PersistentManager manager = new PersistentManager();
    TesterStore store = new TesterStore();
    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    manager.setSessionActivityCheck(true);
    ctx.setManager(manager);
    ctx.addValve(new PersistentValve());
    tomcat.start();
    Assert.assertEquals("No active sessions", manager.getActiveSessions(), 0);
    Assert.assertTrue("No sessions managed", manager.getSessionIdsFull().isEmpty());
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy?no_create_session=false").toString();
    Assert.assertNotNull("Session is stored", store.load(sessionId));
    Assert.assertEquals("All sessions are passivated", manager.getActiveSessions(), 0);
    Assert.assertTrue("One session was created", !manager.getSessionIdsFull().isEmpty());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) PersistentValve(org.apache.catalina.valves.PersistentValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 58 with StandardContext

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

the class Benchmarks method doTestManagerBaseCreateSession.

private void doTestManagerBaseCreateSession(int threadCount, int iterCount) throws LifecycleException {
    // Create a default session manager
    StandardManager mgr = new StandardManager();
    mgr.setPathname(null);
    Host host = new StandardHost();
    host.setName("unittest");
    Context context = new StandardContext();
    context.setPath("");
    context.setParent(host);
    mgr.setContext(context);
    mgr.start();
    mgr.generateSessionId();
    while (mgr.sessionCreationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionCreationTiming.add(null);
    }
    while (mgr.sessionExpirationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionExpirationTiming.add(null);
    }
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++) {
        threads[i] = new Thread(new TestThreadCreateSession(mgr, iterCount));
    }
    long start = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        threads[i].start();
    }
    for (int i = 0; i < threadCount; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }
    long end = System.currentTimeMillis();
    StringBuilder result = new StringBuilder();
    result.append("Threads: ");
    result.append(threadCount);
    result.append(", Time(ms): ");
    result.append(end - start);
    System.out.println(result.toString());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host)

Example 59 with StandardContext

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

the class TestMapper method createContext.

private Context createContext(String name) {
    Context context = new StandardContext();
    context.setName(name);
    return context;
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext)

Example 60 with StandardContext

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

the class TestVirtualWebappLoader method testStartInternal.

@Test
public void testStartInternal() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    StandardContext ctx = (StandardContext) tomcat.addContext("", appDir.getAbsolutePath());
    WebappLoader loader = new WebappLoader();
    loader.setContext(ctx);
    ctx.setLoader(loader);
    ctx.setResources(new StandardRoot(ctx));
    ctx.resourcesStart();
    File f1 = new File("test/webapp-fragments/WEB-INF/lib");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", f1.getAbsolutePath(), null, "/");
    loader.start();
    String[] repos = loader.getLoaderRepositories();
    Assert.assertEquals(4, repos.length);
    loader.stop();
    repos = loader.getLoaderRepositories();
    Assert.assertEquals(0, repos.length);
    // no leak
    loader.start();
    repos = loader.getLoaderRepositories();
    Assert.assertEquals(4, repos.length);
    // clear loader
    ctx.setLoader(null);
    // see tearDown()!
    tomcat.start();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) 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)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)181 File (java.io.File)72 Tomcat (org.apache.catalina.startup.Tomcat)64 Test (org.junit.Test)52 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)41 Context (org.apache.catalina.Context)32 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)31 StandardHost (org.apache.catalina.core.StandardHost)25 IOException (java.io.IOException)22 Host (org.apache.catalina.Host)18 MalformedURLException (java.net.MalformedURLException)16 JarFile (java.util.jar.JarFile)16 ServletContext (javax.servlet.ServletContext)16 StandardRoot (org.apache.catalina.webresources.StandardRoot)16 URL (java.net.URL)13 InterceptSupport (com.creditease.monitor.interceptframework.InterceptSupport)12 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Container (org.apache.catalina.Container)12