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());
}
}
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);
}
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);
}
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());
}
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());
}
Aggregations