Search in sources :

Example 56 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHostConfigAutomaticDeployment method doTestDelete.

private void doTestDelete(boolean startXml, boolean startExternalWar, boolean startExternalDir, boolean startWar, boolean startDir, int toDelete, boolean resultXml, boolean resultWar, boolean resultDir, String resultCookieName) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();
    // Init
    File xml = null;
    File ext = null;
    File war = null;
    File dir = null;
    if (startXml && !startExternalWar && !startExternalDir) {
        xml = createXmlInConfigBaseForAppbase();
    }
    if (startExternalWar) {
        ext = createWar(WAR_XML_SOURCE, false);
        xml = createXmlInConfigBaseForExternal(ext);
    }
    if (startExternalDir) {
        ext = createDirInExternal(true);
        xml = createXmlInConfigBaseForExternal(ext);
    }
    if (startWar) {
        war = createWar(WAR_XML_SOURCE, true);
    }
    if (startDir) {
        dir = createDirInAppbase(true);
    }
    if ((startWar || startExternalWar) && !startDir) {
        host.setUnpackWARs(false);
    }
    // Deploy the files we copied
    tomcat.start();
    host.backgroundProcess();
    // Remove the specified file
    switch(toDelete) {
        case XML:
            ExpandWar.delete(xml);
            break;
        case EXT:
            ExpandWar.delete(ext);
            break;
        case WAR:
            ExpandWar.delete(war);
            break;
        case DIR:
            ExpandWar.delete(dir);
            break;
        default:
            Assert.fail();
    }
    // Trigger an auto-deployment cycle
    host.backgroundProcess();
    Context ctxt = (Context) host.findChild(APP_NAME.getName());
    // behaviour when the external resource is deleted
    if (toDelete == EXT) {
        if (ext == null) {
            Assert.fail();
        } else {
            Assert.assertFalse(ext.exists());
        }
    } else {
        if (startExternalWar) {
            if (ext == null) {
                Assert.fail();
            } else {
                Assert.assertTrue(ext.isFile());
            }
        }
        if (startExternalDir) {
            if (ext == null) {
                Assert.fail();
            } else {
                Assert.assertTrue(ext.isDirectory());
            }
        }
    }
    if (resultXml) {
        if (xml == null) {
            Assert.fail();
        } else {
            Assert.assertTrue(xml.isFile());
        }
    }
    if (resultWar) {
        if (war == null) {
            Assert.fail();
        } else {
            Assert.assertTrue(war.isFile());
        }
    }
    if (resultDir) {
        if (dir == null) {
            Assert.fail();
        } else {
            Assert.assertTrue(dir.isDirectory());
        }
    }
    if (!resultXml && (startExternalWar || startExternalDir)) {
        Assert.assertNull(ctxt);
    }
    if (!resultWar && !resultDir) {
        if (resultXml) {
            Assert.assertNotNull(ctxt);
            Assert.assertEquals(LifecycleState.FAILED, ctxt.getState());
        } else {
            Assert.assertNull(ctxt);
        }
    }
    if (ctxt != null) {
        Assert.assertEquals(resultCookieName, ctxt.getSessionCookieName());
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File)

Example 57 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestListener method testServletContainerInitializer.

/*
     * Check that a ServletContainerInitializer can install a
     * {@link ServletContextListener} and that it gets initialized.
     * @throws Exception
     */
@Test
public void testServletContainerInitializer() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context context = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    context.addServletContainerInitializer(new SCI(), null);
    tomcat.start();
    assertTrue(SCL.initialized);
}
Also used : ServletContext(javax.servlet.ServletContext) Context(org.apache.catalina.Context) Test(org.junit.Test)

Example 58 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestTomcat method testSingleWebapp.

@Test
public void testSingleWebapp() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File(getBuildDirectory(), "webapps/examples");
    // app dir is relative to server home
    Context ctxt = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
    ctxt.addApplicationListener(WsContextListener.class.getName());
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
    String text = res.toString();
    assertTrue(text, text.indexOf("<a href=\"../helloworld.html\">") > 0);
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) Test(org.junit.Test)

Example 59 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestTomcat method testGetDefaultContextPerAddWebapp.

@Test
public void testGetDefaultContextPerAddWebapp() {
    Tomcat tomcat = getTomcatInstance();
    File appFile = new File("test/deployment/context.war");
    Context context = tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
    assertEquals(StandardContext.class.getName(), context.getClass().getName());
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) Test(org.junit.Test)

Example 60 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestTomcat method testEnableNamingGlobal.

/*
     * Test for enabling JNDI and using global resources.
     */
@Test
public void testEnableNamingGlobal() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();
    ContextEnvironment environment = new ContextEnvironment();
    environment.setType("java.lang.String");
    environment.setName("globalTest");
    environment.setValue("Tomcat User");
    tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);
    ContextResourceLink link = new ContextResourceLink();
    link.setGlobal("globalTest");
    link.setName(HelloWorldJndi.JNDI_ENV_NAME);
    link.setType("java.lang.String");
    ctx.getNamingResources().addResourceLink(link);
    Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
    ctx.addServletMappingDecoded("/", "jndiServlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("Hello, Tomcat User", res.toString());
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24