Author: markt
Date: Thu Apr 7 20:24:22 2016
New Revision: 1738188
URL: http://svn.apache.org/viewvc?rev=1738188&view=rev
Log:
Add a unit test based on a discussion on the servlet spec users list.
Modified:
tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java
Modified: tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java?rev=1738188&r1=1738187&r2=1738188&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java
(original)
+++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java Thu
Apr 7 20:24:22 2016
@@ -246,4 +246,81 @@ public class TestApplicationContext exte
}
}
}
+
+
+ @Test
+ public void testCrossContextSetAttribute() throws Exception {
+ // Setup Tomcat instance
+ Tomcat tomcat = getTomcatInstance();
+
+ // No file system docBase required
+ Context ctx2 = tomcat.addContext("/second", null);
+ GetAttributeServlet getAttributeServlet = new GetAttributeServlet();
+ Tomcat.addServlet(ctx2, "getAttributeServlet", getAttributeServlet);
+ ctx2.addServletMapping("/test", "getAttributeServlet");
+
+ // No file system docBase required
+ Context ctx1 = tomcat.addContext("/first", null);
+ ctx1.setCrossContext(true);
+ SetAttributeServlet setAttributeServlet = new
SetAttributeServlet("/test", "/second");
+ Tomcat.addServlet(ctx1, "setAttributeServlet", setAttributeServlet);
+ ctx1.addServletMapping("/test", "setAttributeServlet");
+
+ tomcat.start();
+
+ ByteChunk bc = new ByteChunk();
+ int rc = getUrl("http://localhost:" + getPort() + "/first/test", bc,
null);
+
+ Assert.assertEquals(200, rc);
+ Assert.assertEquals("01-PASS", bc.toString());
+ }
+
+
+ private static class SetAttributeServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+ private static final String ATTRIBUTE_NAME = "setAttributeTest";
+ private static final String ATTRIBUTE_VALUE = "abcde";
+
+ private final String targetContextPath;
+ private final String targetPath;
+
+ public SetAttributeServlet(String targetPath, String
targetContextPath) {
+ this.targetPath = targetPath;
+ this.targetContextPath = targetContextPath;
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ ServletContext sc;
+ if (targetContextPath == null) {
+ sc = req.getServletContext();
+ } else {
+ sc = req.getServletContext().getContext(targetContextPath);
+ }
+ sc.setAttribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
+ sc.getRequestDispatcher(targetPath).forward(req, resp);
+ }
+ }
+
+
+ private static class GetAttributeServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ resp.setContentType("text/plain");
+ PrintWriter pw = resp.getWriter();
+ String value = (String) req.getServletContext().getAttribute(
+ SetAttributeServlet.ATTRIBUTE_NAME);
+ if (SetAttributeServlet.ATTRIBUTE_VALUE.equals(value)) {
+ pw.print("01-PASS");
+ } else {
+ pw.print("01-FAIL");
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]