Author: kkolinko Date: Thu Jan 21 18:56:26 2016 New Revision: 1726064 URL: http://svn.apache.org/viewvc?rev=1726064&view=rev Log: Backport additional tests from Tomcat 7. Implement (uncomment) Tomcat.enableNaming().
Modified: tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/TestTomcat.java tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/Tomcat.java Modified: tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/TestTomcat.java URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/TestTomcat.java?rev=1726064&r1=1726063&r2=1726064&view=diff ============================================================================== --- tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/TestTomcat.java (original) +++ tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/TestTomcat.java Thu Jan 21 18:56:26 2016 @@ -93,35 +93,36 @@ public class TestTomcat extends TomcatBa } } -//FIXME -// /** -// * Simple servlet to test JNDI -// */ -// public static class HelloWorldJndi extends HttpServlet { -// -// private static final long serialVersionUID = 1L; -// -// private static final String JNDI_ENV_NAME = "test"; -// -// @Override -// public void doGet(HttpServletRequest req, HttpServletResponse res) -// throws IOException { -// -// String name = null; -// -// try { -// javax.naming.Context initCtx = new InitialContext(); -// javax.naming.Context envCtx = -// (javax.naming.Context) initCtx.lookup("java:comp/env"); -// name = (String) envCtx.lookup(JNDI_ENV_NAME); -// } catch (NamingException e) { -// throw new IOException(e); -// } -// -// res.getWriter().write("Hello, " + name); -// } -// } -// + /** + * Simple servlet to test JNDI + */ + public static class HelloWorldJndi extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private static final String JNDI_ENV_NAME = "test"; + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + String name = null; + + try { + javax.naming.Context initCtx = new InitialContext(); + javax.naming.Context envCtx = + (javax.naming.Context) initCtx.lookup("java:comp/env"); + name = (String) envCtx.lookup(JNDI_ENV_NAME); + } catch (NamingException e) { + IOException ioe = new IOException(e.getMessage()); + ioe.initCause(e); + throw ioe; + } + + res.getWriter().write("Hello, " + name); + } + } + // /** // * Servlet that tries to obtain a URL for WEB-INF/web.xml // */ @@ -281,4 +282,114 @@ public class TestTomcat extends TomcatBa String text = res.toString(); assertTrue(text, text.indexOf("<h1>Hello World!</h1>") > 0); } + + @Test + public void testJsps() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File(getBuildDirectory(), "webapps/examples"); + // app dir is relative to server home + tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + + "/examples/jsp/jsp2/el/basic-arithmetic.jsp"); + String text = res.toString(); + assertTrue(text, text.indexOf("<td>${(1==2) ? 3 : 4}</td>") > 0); + } + + @Test + public void testSession() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = tomcat.addContext("", null); + // You can customize the context by calling + // its API + + Tomcat.addServlet(ctx, "myServlet", new HelloWorldSession()); + ctx.addServletMapping("/", "myServlet"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); + assertEquals("Hello world", res.toString()); + } + + @Test + public void testLaunchTime() throws Exception { + Tomcat tomcat = getTomcatInstance(); + long t0 = System.currentTimeMillis(); + tomcat.addContext(null, "", "."); + tomcat.start(); + log.info("Tomcat started in [" + (System.currentTimeMillis() - t0) + + "] ms"); + } + + + /** + * Test for enabling JNDI. + */ + @Test + public void testEnableNaming() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = tomcat.addContext("", null); + + // You can customise the context by calling its API + + // Enable JNDI - it is disabled by default + tomcat.enableNaming(); + + ContextEnvironment environment = new ContextEnvironment(); + environment.setType("java.lang.String"); + environment.setName(HelloWorldJndi.JNDI_ENV_NAME); + environment.setValue("Tomcat User"); + ctx.getNamingResources().addEnvironment(environment); + + Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi()); + ctx.addServletMapping("/", "jndiServlet"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); + assertEquals("Hello, Tomcat User", res.toString()); + } + + /** + * 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); + + // You can customise the context by calling its API + + // 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); + ctx.getNamingResources().addResourceLink(link); + + Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi()); + ctx.addServletMapping("/", "jndiServlet"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); + assertEquals("Hello, Tomcat User", res.toString()); + } } Modified: tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/Tomcat.java URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/Tomcat.java?rev=1726064&r1=1726063&r2=1726064&view=diff ============================================================================== --- tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/Tomcat.java (original) +++ tomcat/tc6.0.x/branches/tomcat6-testing_20160106/test/org/apache/catalina/startup/Tomcat.java Thu Jan 21 18:56:26 2016 @@ -804,41 +804,41 @@ public class Tomcat { } } -// /** -// * Enables JNDI naming which is disabled by default. Server must implement -// * {@link Lifecycle} in order for the {@link NamingContextListener} to be -// * used. -// * -// */ -// public void enableNaming() { -// // Make sure getServer() has been called as that is where naming is -// // disabled -// getServer(); -// server.addLifecycleListener(new NamingContextListener()); -// -// System.setProperty("catalina.useNaming", "true"); -// -// String value = "org.apache.naming"; -// String oldValue = -// System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); -// if (oldValue != null) { -// if (oldValue.contains(value)) { -// value = oldValue; -// } else { -// value = value + ":" + oldValue; -// } -// } -// System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value); -// -// value = System.getProperty -// (javax.naming.Context.INITIAL_CONTEXT_FACTORY); -// if (value == null) { -// System.setProperty -// (javax.naming.Context.INITIAL_CONTEXT_FACTORY, -// "org.apache.naming.java.javaURLContextFactory"); -// } -// } -// + /** + * Enables JNDI naming which is disabled by default. Server must implement + * {@link Lifecycle} in order for the {@link NamingContextListener} to be + * used. + * + */ + public void enableNaming() { + // Make sure getServer() has been called as that is where naming is + // disabled + getServer(); + server.addLifecycleListener(new NamingContextListener()); + + System.setProperty("catalina.useNaming", "true"); + + String value = "org.apache.naming"; + String oldValue = + System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); + if (oldValue != null) { + if (oldValue.contains(value)) { + value = oldValue; + } else { + value = value + ":" + oldValue; + } + } + System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value); + + value = System.getProperty + (javax.naming.Context.INITIAL_CONTEXT_FACTORY); + if (value == null) { + System.setProperty + (javax.naming.Context.INITIAL_CONTEXT_FACTORY, + "org.apache.naming.java.javaURLContextFactory"); + } + } + // /** // * Provide default configuration for a context. This is the programmatic // * equivalent of the default web.xml. --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org