Search in sources :

Example 76 with StandardHost

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

the class TestTomcat method testGetCustomContextPerAddContextWithHost.

@Test
public void testGetCustomContextPerAddContextWithHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }
    // No file system docBase required
    Context ctx = tomcat.addContext(host, "", null);
    Assert.assertEquals(ReplicatedContext.class.getName(), ctx.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) StandardHost(org.apache.catalina.core.StandardHost) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) Test(org.junit.Test)

Example 77 with StandardHost

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

the class TestHostConfigAutomaticDeployment method doTestDeployment.

private void doTestDeployment(boolean deployXML, boolean copyXML, boolean unpackWARs, LifecycleState resultState, String cookieName, boolean resultXml, boolean resultWar, boolean resultDir) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Start the instance
    tomcat.start();
    // Set the attributes
    StandardHost host = (StandardHost) tomcat.getHost();
    host.setDeployXML(deployXML);
    host.setCopyXML(copyXML);
    host.setUnpackWARs(unpackWARs);
    // Trigger automatic deployment
    host.backgroundProcess();
    // Test the results
    Context ctxt = (Context) tomcat.getHost().findChild(APP_NAME.getPath());
    if (resultState == null) {
        Assert.assertNull(ctxt);
    } else {
        Assert.assertNotNull(ctxt);
        Assert.assertEquals(resultState, ctxt.getState());
        Assert.assertEquals(cookieName, ctxt.getSessionCookieName());
    }
    File xml = new File(host.getConfigBaseFile(), APP_NAME.getBaseName() + ".xml");
    Assert.assertEquals(Boolean.valueOf(resultXml), Boolean.valueOf(xml.isFile()));
    File war = new File(host.getAppBaseFile(), APP_NAME.getBaseName() + ".war");
    Assert.assertEquals(Boolean.valueOf(resultWar), Boolean.valueOf(war.isFile()));
    File dir = new File(host.getAppBase(), APP_NAME.getBaseName());
    Assert.assertEquals(Boolean.valueOf(resultDir), Boolean.valueOf(dir.isDirectory()));
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File)

Example 78 with StandardHost

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

the class TestHostConfigAutomaticDeployment method doTestCopyXML.

private void doTestCopyXML(boolean copyXmlHost, boolean copyXmlWar, boolean external, boolean resultXml) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();
    host.setCopyXML(copyXmlHost);
    tomcat.start();
    File war;
    if (copyXmlWar) {
        war = createWar(WAR_XML_COPYXML_TRUE_SOURCE, !external);
    } else {
        war = createWar(WAR_XML_COPYXML_FALSE_SOURCE, !external);
    }
    if (external) {
        createXmlInConfigBaseForExternal(war);
    }
    host.backgroundProcess();
    File xml = new File(host.getConfigBaseFile(), APP_NAME.getBaseName() + ".xml");
    Assert.assertEquals(Boolean.valueOf(resultXml), Boolean.valueOf(xml.isFile()));
    Context context = (Context) host.findChild(APP_NAME.getName());
    if (external) {
        Assert.assertEquals(XML_COOKIE_NAME, context.getSessionCookieName());
    } else {
        Assert.assertEquals(WAR_COOKIE_NAME, context.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 79 with StandardHost

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

the class TestHostConfigAutomaticDeployment method doTestAddition.

private void doTestAddition(boolean startXml, boolean startExternalWar, boolean startExternalDir, boolean startWar, boolean startDir, boolean copyXML, boolean deployXML, int toAdd, boolean resultXml, boolean resultWar, boolean resultDir, String resultCookieName, int resultAction, LifecycleState state) 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(toAdd != DIR_XML);
    }
    if ((startWar || startExternalWar) && !startDir) {
        host.setUnpackWARs(false);
    }
    host.setCopyXML(copyXML);
    host.setDeployXML(deployXML);
    // Deploy the files we copied
    tomcat.start();
    host.backgroundProcess();
    // Change the specified file
    switch(toAdd) {
        case XML:
            if (xml == null) {
                xml = createXmlInConfigBaseForAppbase();
            } else {
                Assert.fail();
            }
            break;
        case EXT:
            if (ext == null && xml == null) {
                ext = createWar(WAR_XML_SOURCE, false);
                xml = createXmlInConfigBaseForExternal(ext);
            } else {
                Assert.fail();
            }
            break;
        case WAR:
            if (war == null) {
                war = createWar(WAR_XML_SOURCE, true);
            } else {
                Assert.fail();
            }
            break;
        case DIR:
            if (dir == null) {
                dir = createDirInAppbase(true);
            } else {
                Assert.fail();
            }
            break;
        case DIR_XML:
            dir = createDirXmlInAppbase();
            xml = getXmlInConfigBaseForAppbase();
            break;
        default:
            Assert.fail();
    }
    Context oldContext = (Context) host.findChild(APP_NAME.getName());
    StateTracker tracker = new StateTracker();
    oldContext.addLifecycleListener(tracker);
    // Trigger an auto-deployment cycle
    host.backgroundProcess();
    Context newContext = (Context) host.findChild(APP_NAME.getName());
    // Check the results
    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(newContext);
    }
    if (!resultWar && !resultDir) {
        if (resultXml) {
            Assert.assertNotNull(newContext);
            if (!startExternalWar && !startExternalDir) {
                Assert.assertEquals(LifecycleState.FAILED, newContext.getState());
            } else {
                Assert.assertEquals(LifecycleState.STARTED, newContext.getState());
            }
        } else {
            Assert.assertNull(newContext);
        }
    }
    if (newContext != null) {
        Assert.assertEquals(resultCookieName, newContext.getSessionCookieName());
    }
    if (resultAction == NONE) {
        Assert.assertSame(oldContext, newContext);
        Assert.assertEquals("", tracker.getHistory());
    } else if (resultAction == RELOAD) {
        Assert.assertSame(oldContext, newContext);
        Assert.assertEquals("stopstart", tracker.getHistory());
    } else if (resultAction == REDEPLOY) {
        if (newContext == null) {
            Assert.fail();
        } else {
            Assert.assertEquals(state, newContext.getState());
        }
        Assert.assertNotSame(oldContext, newContext);
        // No init or start as that will be in a new context object
        Assert.assertEquals("stopafter_destroy", tracker.getHistory());
    } else {
        Assert.fail();
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File)

Example 80 with StandardHost

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

the class TestHostConfigAutomaticDeployment method doTestModify.

private void doTestModify(boolean startXml, boolean startExternalWar, boolean startExternalDir, boolean startWar, boolean startDir, int toModify, boolean resultXml, boolean resultWar, boolean resultDir, String resultCookieName, int resultAction, LifecycleState resultState) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();
    // Init
    File xml = null;
    File ext = null;
    File war = null;
    File dir = null;
    long testStartTime = System.currentTimeMillis();
    if (startXml && !startExternalWar && !startExternalDir) {
        xml = createXmlInConfigBaseForAppbase();
    }
    if (startExternalWar) {
        ext = createWar(WAR_XML_SOURCE, false);
        xml = createXmlInConfigBaseForExternal(ext);
    }
    if (startExternalDir) {
        ext = createDirInAppbase(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();
    // sufficiently different.
    switch(toModify) {
        case XML:
            if (xml == null) {
                Assert.fail();
            } else {
                Assert.assertTrue("Failed to set last modified for [" + xml + "]", xml.setLastModified(testStartTime - 10 * HostConfig.FILE_MODIFICATION_RESOLUTION_MS));
            }
            break;
        case EXT:
            if (ext == null) {
                Assert.fail();
            } else {
                Assert.assertTrue("Failed to set last modified for [" + ext + "]", ext.setLastModified(testStartTime - 10 * HostConfig.FILE_MODIFICATION_RESOLUTION_MS));
            }
            break;
        case WAR:
            if (war == null) {
                Assert.fail();
            } else {
                Assert.assertTrue("Failed to set last modified for [" + war + "]", war.setLastModified(testStartTime - 10 * HostConfig.FILE_MODIFICATION_RESOLUTION_MS));
            }
            break;
        case DIR:
            if (dir == null) {
                Assert.fail();
            } else {
                Assert.assertTrue("Failed to set last modified for [" + dir + "]", dir.setLastModified(testStartTime - 10 * HostConfig.FILE_MODIFICATION_RESOLUTION_MS));
            }
            break;
        default:
            Assert.fail();
    }
    Context oldContext = (Context) host.findChild(APP_NAME.getName());
    StateTracker tracker = new StateTracker();
    oldContext.addLifecycleListener(tracker);
    // Trigger an auto-deployment cycle
    host.backgroundProcess();
    Context newContext = (Context) host.findChild(APP_NAME.getName());
    // Check the results
    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(newContext);
    }
    if (!resultWar && !resultDir) {
        if (resultXml) {
            Assert.assertNotNull(newContext);
            if (!startExternalWar && !startExternalDir) {
                Assert.assertEquals(LifecycleState.FAILED, newContext.getState());
            } else {
                Assert.assertEquals(LifecycleState.STARTED, newContext.getState());
            }
        } else {
            Assert.assertNull(newContext);
        }
    }
    if (newContext != null) {
        Assert.assertEquals(resultCookieName, newContext.getSessionCookieName());
        Assert.assertEquals(resultState, newContext.getState());
    }
    if (resultAction == NONE) {
        Assert.assertSame(oldContext, newContext);
        Assert.assertEquals("", tracker.getHistory());
    } else if (resultAction == RELOAD) {
        Assert.assertSame(oldContext, newContext);
        Assert.assertEquals("stopstart", tracker.getHistory());
    } else if (resultAction == REDEPLOY) {
        Assert.assertNotSame(oldContext, newContext);
        // No init or start as that will be in a new context object
        Assert.assertEquals("stopafter_destroy", tracker.getHistory());
    } else {
        Assert.fail();
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File)

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