use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.
the class TestNamingContext method testGlobalNaming.
@Test
public void testGlobalNaming() throws Exception {
Tomcat tomcat = getTomcatInstance();
tomcat.enableNaming();
org.apache.catalina.Context ctx = tomcat.addContext("", null);
tomcat.start();
Context webappInitial = ContextBindings.getContext(ctx);
// Nothing added at the moment so should be null
Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
Assert.assertNull(obj);
ContextEnvironment ce = new ContextEnvironment();
ce.setName(GLOBAL_NAME);
ce.setValue(DATA);
ce.setType(DATA.getClass().getName());
tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);
// No link so still should be null
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
Assert.assertNull(obj);
// Now add a resource link to the context
ContextResourceLink crl = new ContextResourceLink();
crl.setGlobal(GLOBAL_NAME);
crl.setName(LOCAL_NAME);
crl.setType(DATA.getClass().getName());
ctx.getNamingResources().addResourceLink(crl);
// Link exists so should be OK now
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
Assert.assertEquals(DATA, obj);
// Try shortcut
ResourceLinkFactory factory = new ResourceLinkFactory();
ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
obj = factory.getObjectInstance(rlr, null, null, null);
Assert.assertEquals(DATA, obj);
// Remove the link
ctx.getNamingResources().removeResourceLink(LOCAL_NAME);
// No link so should be null
obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
Assert.assertNull(obj);
// Shortcut should fail too
obj = factory.getObjectInstance(rlr, null, null, null);
Assert.assertNull(obj);
}
use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.
the class TestNamingContext method testBug52830.
@Test
public void testBug52830() throws Exception {
Tomcat tomcat = getTomcatInstance();
tomcat.enableNaming();
// No file system docBase required
org.apache.catalina.Context ctx = tomcat.addContext("", null);
// Create the resource
ContextEnvironment env = new ContextEnvironment();
env.setName("boolean");
env.setType(Boolean.class.getName());
env.setValue("true");
ctx.getNamingResources().addEnvironment(env);
// Map the test Servlet
Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
ctx.addServletMappingDecoded("/", "bug52830Servlet");
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
assertEquals(200, rc);
assertTrue(bc.toString().contains("truetrue"));
}
use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.
the class TestNamingContext method testBug53465.
@Test
public void testBug53465() throws Exception {
Tomcat tomcat = getTomcatInstance();
tomcat.enableNaming();
File appDir = new File("test/webapp");
// app dir is relative to server home
org.apache.catalina.Context ctxt = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug53465.jsp", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(bc.toString().contains("<p>10</p>"));
ContextEnvironment ce = ctxt.getNamingResources().findEnvironment("bug53465");
Assert.assertEquals("Bug53465MappedName", ce.getProperty("mappedName"));
}
Aggregations