use of org.apache.catalina.Context in project tomcat by apache.
the class TomcatBaseTest method getTomcatInstanceTestWebapp.
/**
* Make the Tomcat instance preconfigured with test/webapp available to
* sub-classes.
* @param addJstl Should JSTL support be added to the test webapp
* @param start Should the Tomcat instance be started
*
* @return A Tomcat instance pre-configured with the web application located
* at test/webapp
*
* @throws LifecycleException If a problem occurs while starting the
* instance
*/
public Tomcat getTomcatInstanceTestWebapp(boolean addJstl, boolean start) throws LifecycleException {
File appDir = new File("test/webapp");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
filter.setTldSkip(filter.getTldSkip() + ",testclasses");
filter.setPluggabilitySkip(filter.getPluggabilitySkip() + ",testclasses");
if (addJstl) {
File lib = new File("webapps/examples/WEB-INF/lib");
ctx.setResources(new StandardRoot(ctx));
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
}
if (start) {
tomcat.start();
}
return tomcat;
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHostConfigAutomaticDeployment method testSetContextClassName.
@Test
public void testSetContextClassName() throws Exception {
Tomcat tomcat = getTomcatInstance();
Host host = tomcat.getHost();
if (host instanceof StandardHost) {
StandardHost standardHost = (StandardHost) host;
standardHost.setContextClass(TesterContext.class.getName());
}
// Copy the WAR file
File war = new File(host.getAppBaseFile(), APP_NAME.getBaseName() + ".war");
Files.copy(WAR_XML_SOURCE.toPath(), war.toPath());
// Deploy the copied war
tomcat.start();
host.backgroundProcess();
// Check the Context class
Context ctxt = (Context) host.findChild(APP_NAME.getName());
Assert.assertTrue(ctxt instanceof TesterContext);
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestDefaultServlet method testGetWithSubpathmount.
/*
* Test https://bz.apache.org/bugzilla/show_bug.cgi?id=50026
* Verify serving of resources from context root with subpath mapping.
*/
@Test
public void testGetWithSubpathmount() throws Exception {
Tomcat tomcat = getTomcatInstance();
String contextPath = "/examples";
File appDir = new File(getBuildDirectory(), "webapps" + contextPath);
// app dir is relative to server home
org.apache.catalina.Context ctx = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
ctx.addApplicationListener(WsContextListener.class.getName());
// Override the default servlet with our own mappings
Tomcat.addServlet(ctx, "default2", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default2");
ctx.addServletMappingDecoded("/servlets/*", "default2");
ctx.addServletMappingDecoded("/static/*", "default2");
tomcat.start();
final ByteChunk res = new ByteChunk();
// Make sure DefaultServlet isn't exposing special directories
// by remounting the webapp under a sub-path
int rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/web.xml", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/doesntexistanywhere", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/MANIFEST.MF", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/doesntexistanywhere", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
// Make sure DefaultServlet is serving resources relative to the
// context root regardless of where the it is mapped
final ByteChunk rootResource = new ByteChunk();
rc = getUrl("http://localhost:" + getPort() + contextPath + "/index.html", rootResource, null);
assertEquals(HttpServletResponse.SC_OK, rc);
final ByteChunk subpathResource = new ByteChunk();
rc = getUrl("http://localhost:" + getPort() + contextPath + "/servlets/index.html", subpathResource, null);
assertEquals(HttpServletResponse.SC_OK, rc);
assertFalse(rootResource.toString().equals(subpathResource.toString()));
rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/index.html", res, null);
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
use of org.apache.catalina.Context 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();
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());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestContextConfig method testBug54448and54450.
@Test
public void testBug54448and54450() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-fragments");
Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
Tomcat.addServlet(context, "TestServlet", "org.apache.catalina.startup.TesterServletWithAnnotations");
context.addServletMappingDecoded("/testServlet", "TestServlet");
tomcat.enableNaming();
tomcat.start();
assertPageContains("/test/testServlet", "envEntry1: 1 envEntry2: 2 envEntry3: 33 envEntry4: 4 envEntry5: 55 envEntry6: 66");
}
Aggregations