PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/tomcat-7.0.2/test/org/apache/catalina/startup/TestTomcat.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 315 lines | 186 code | 71 blank | 58 comment | 7 complexity | f5a2768e7de257a38a50ec9c13ab0e9e MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.catalina.startup;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.Reader;
  23. import java.net.URL;
  24. import java.net.URLConnection;
  25. import java.security.Principal;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import javax.naming.Context;
  29. import javax.naming.InitialContext;
  30. import javax.naming.NamingException;
  31. import javax.servlet.http.HttpServlet;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import org.apache.catalina.deploy.ContextEnvironment;
  35. import org.apache.catalina.deploy.ContextResourceLink;
  36. import org.apache.catalina.realm.GenericPrincipal;
  37. import org.apache.catalina.realm.RealmBase;
  38. import org.apache.tomcat.util.buf.ByteChunk;
  39. public class TestTomcat extends TomcatBaseTest {
  40. /**
  41. * Simple servlet to test in-line registration
  42. */
  43. public static class HelloWorld extends HttpServlet {
  44. private static final long serialVersionUID = 1L;
  45. @Override
  46. public void doGet(HttpServletRequest req, HttpServletResponse res)
  47. throws IOException {
  48. res.getWriter().write("Hello world");
  49. }
  50. }
  51. /**
  52. * Simple servlet to test JNDI
  53. */
  54. public static class HelloWorldJndi extends HttpServlet {
  55. private static final long serialVersionUID = 1L;
  56. private static final String JNDI_ENV_NAME = "test";
  57. @Override
  58. public void doGet(HttpServletRequest req, HttpServletResponse res)
  59. throws IOException {
  60. String name = null;
  61. try {
  62. Context initCtx = new InitialContext();
  63. Context envCtx = (Context) initCtx.lookup("java:comp/env");
  64. name = (String) envCtx.lookup(JNDI_ENV_NAME);
  65. } catch (NamingException e) {
  66. throw new IOException(e);
  67. }
  68. res.getWriter().write("Hello, " + name);
  69. }
  70. }
  71. /**
  72. * Servlet that tries to obtain a URL for WEB-INF/web.xml
  73. */
  74. public static class GetResource extends HttpServlet {
  75. private static final long serialVersionUID = 1L;
  76. @Override
  77. public void doGet(HttpServletRequest req, HttpServletResponse res)
  78. throws IOException {
  79. URL url = req.getServletContext().getResource("/WEB-INF/web.xml");
  80. res.getWriter().write("The URL obtained for /WEB-INF/web.xml was ");
  81. if (url == null) {
  82. res.getWriter().write("null");
  83. } else {
  84. res.getWriter().write(url.toString() + "\n");
  85. res.getWriter().write("The first 20 characters of that resource are:\n");
  86. // Read some content from the resource
  87. URLConnection conn = url.openConnection();
  88. InputStream is = null;
  89. Reader reader = null;
  90. char cbuf[] = new char[20];
  91. try {
  92. is = conn.getInputStream();
  93. reader = new InputStreamReader(is);
  94. reader.read(cbuf);
  95. res.getWriter().write(cbuf);
  96. } finally {
  97. if (reader != null) {
  98. try { reader.close(); } catch(IOException ioe) {/*Ignore*/}
  99. }
  100. if (is != null) {
  101. try { is.close(); } catch(IOException ioe) {/*Ignore*/}
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * Simple Realm that uses a configurable {@link Map} to link user names and
  109. * passwords. No roles are supported at this stage.
  110. */
  111. public static final class MapRealm extends RealmBase {
  112. private Map<String,String> users = new HashMap<String,String>();
  113. public void addUser(String username, String password) {
  114. users.put(username, password);
  115. }
  116. @Override
  117. protected String getName() {
  118. return "MapRealm";
  119. }
  120. @Override
  121. protected String getPassword(String username) {
  122. return users.get(username);
  123. }
  124. @Override
  125. protected Principal getPrincipal(String username) {
  126. return new GenericPrincipal(username, getPassword(username));
  127. }
  128. }
  129. /**
  130. * Start tomcat with a single context and one
  131. * servlet - all programmatic, no server.xml or
  132. * web.xml used.
  133. *
  134. * @throws Exception
  135. */
  136. public void testProgrammatic() throws Exception {
  137. Tomcat tomcat = getTomcatInstance();
  138. // Must have a real docBase - just use temp
  139. org.apache.catalina.Context ctx =
  140. tomcat.addContext("/", System.getProperty("java.io.tmpdir"));
  141. // You can customize the context by calling
  142. // its API
  143. Tomcat.addServlet(ctx, "myServlet", new HelloWorld());
  144. ctx.addServletMapping("/", "myServlet");
  145. tomcat.start();
  146. ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
  147. assertEquals("Hello world", res.toString());
  148. }
  149. public void testSingleWebapp() throws Exception {
  150. Tomcat tomcat = getTomcatInstance();
  151. File appDir = new File(getBuildDirectory(), "webapps/examples");
  152. // app dir is relative to server home
  153. tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
  154. tomcat.start();
  155. ByteChunk res = getUrl("http://localhost:" + getPort() +
  156. "/examples/servlets/servlet/HelloWorldExample");
  157. assertTrue(res.toString().indexOf("<h1>Hello World!</h1>") > 0);
  158. }
  159. public void testJsps() throws Exception {
  160. Tomcat tomcat = getTomcatInstance();
  161. File appDir = new File(getBuildDirectory(), "webapps/examples");
  162. // app dir is relative to server home
  163. tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
  164. tomcat.start();
  165. ByteChunk res = getUrl("http://localhost:" + getPort() +
  166. "/examples/jsp/jsp2/el/basic-arithmetic.jsp");
  167. assertTrue(res.toString().indexOf("<td>${(1==2) ? 3 : 4}</td>") > 0);
  168. }
  169. public void testLaunchTime() throws Exception {
  170. Tomcat tomcat = getTomcatInstance();
  171. long t0 = System.currentTimeMillis();
  172. tomcat.addContext(null, "/", ".");
  173. tomcat.start();
  174. System.err.println("Test time: " +
  175. (System.currentTimeMillis() - t0));
  176. }
  177. /**
  178. * Test for enabling JNDI.
  179. */
  180. public void testEnableNaming() throws Exception {
  181. Tomcat tomcat = getTomcatInstance();
  182. // Must have a real docBase - just use temp
  183. org.apache.catalina.Context ctx =
  184. tomcat.addContext("/", System.getProperty("java.io.tmpdir"));
  185. // You can customise the context by calling its API
  186. // Enable JNDI - it is disabled by default
  187. tomcat.enableNaming();
  188. ContextEnvironment environment = new ContextEnvironment();
  189. environment.setType("java.lang.String");
  190. environment.setName(HelloWorldJndi.JNDI_ENV_NAME);
  191. environment.setValue("Tomcat User");
  192. ctx.getNamingResources().addEnvironment(environment);
  193. Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
  194. ctx.addServletMapping("/", "jndiServlet");
  195. tomcat.start();
  196. ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
  197. assertEquals("Hello, Tomcat User", res.toString());
  198. }
  199. /**
  200. * Test for enabling JNDI and using global resources.
  201. */
  202. public void testEnableNamingGlobal() throws Exception {
  203. Tomcat tomcat = getTomcatInstance();
  204. // Must have a real docBase - just use temp
  205. org.apache.catalina.Context ctx =
  206. tomcat.addContext("/", System.getProperty("java.io.tmpdir"));
  207. // You can customise the context by calling its API
  208. // Enable JNDI - it is disabled by default
  209. tomcat.enableNaming();
  210. ContextEnvironment environment = new ContextEnvironment();
  211. environment.setType("java.lang.String");
  212. environment.setName("globalTest");
  213. environment.setValue("Tomcat User");
  214. tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);
  215. ContextResourceLink link = new ContextResourceLink();
  216. link.setGlobal("globalTest");
  217. link.setName(HelloWorldJndi.JNDI_ENV_NAME);
  218. ctx.getNamingResources().addResourceLink(link);
  219. Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
  220. ctx.addServletMapping("/", "jndiServlet");
  221. tomcat.start();
  222. ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
  223. assertEquals("Hello, Tomcat User", res.toString());
  224. }
  225. /**
  226. * Test for https://issues.apache.org/bugzilla/show_bug.cgi?id=47866
  227. */
  228. public void testGetResource() throws Exception {
  229. Tomcat tomcat = getTomcatInstance();
  230. String contextPath = "/examples";
  231. File appDir = new File(getBuildDirectory(), "webapps" + contextPath);
  232. // app dir is relative to server home
  233. org.apache.catalina.Context ctx =
  234. tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
  235. Tomcat.addServlet(ctx, "testGetResource", new GetResource());
  236. ctx.addServletMapping("/testGetResource", "testGetResource");
  237. tomcat.start();
  238. ByteChunk res = new ByteChunk();
  239. int rc =getUrl("http://localhost:" + getPort() + contextPath +
  240. "/testGetResource", res, null);
  241. assertEquals(HttpServletResponse.SC_OK, rc);
  242. assertTrue(res.toString().contains("<?xml version=\"1.0\" "));
  243. }
  244. }