vmassol 2003/06/07 02:27:41
Modified: samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit
TestSetURL.java
framework/src/test/share/org/apache/cactus
TestServletURL.java
framework/src/java/share/org/apache/cactus ServletURL.java
framework/src/java/share/org/apache/cactus/server
AbstractHttpServletRequestWrapper.java
Log:
Verify that the context path used in the simulation URL has the correct format.
Revision Changes Path
1.5 +24 -1
jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestSetURL.java
Index: TestSetURL.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestSetURL.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestSetURL.java 26 May 2003 12:15:13 -0000 1.4
+++ TestSetURL.java 7 Jun 2003 09:27:41 -0000 1.5
@@ -241,4 +241,27 @@
assertEquals(request.getParameter("PARAM2"), "");
assertEquals(request.getParameter("PARAM3"), "param3");
}
+
+ //-------------------------------------------------------------------------
+
+ /**
+ * Verify that if the context path is null in <code>setURL()</code> the
+ * real context path will be used.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginSimulatedURLNullContextPath(WebRequest theRequest)
+ {
+ theRequest.setURL(null, null, null, null, null);
+ }
+
+ /**
+ * Verify that if the context path is null in <code>setURL()</code> the
+ * real context path will be used.
+ */
+ public void testSimulatedURLNullContextPath()
+ {
+ assertNotNull(request.getContextPath());
+ }
}
1.10 +46 -1
jakarta-cactus/framework/src/test/share/org/apache/cactus/TestServletURL.java
Index: TestServletURL.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/TestServletURL.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TestServletURL.java 26 May 2003 11:46:39 -0000 1.9
+++ TestServletURL.java 7 Jun 2003 09:27:41 -0000 1.10
@@ -87,6 +87,51 @@
//-------------------------------------------------------------------------
/**
+ * Verify that if the context path is not empty or null it must start with
+ * a "/" character.
+ */
+ public void testSetContextPathFirstCharacterNotForwardSlash()
+ {
+ try
+ {
+ new ServletURL(null, "invalidcontextpath", null, null, null);
+ fail("The context path must start with a \"/\" character");
+ }
+ catch (IllegalArgumentException expected)
+ {
+ assertEquals("The Context Path must start with a \"/\" character.",
+ expected.getMessage());
+ }
+ }
+
+ /**
+ * Verify that if the context path is not empty or null it must not end
+ * with the "/" character.
+ */
+ public void testSetContextPathLastCharacterNotForwardSlash()
+ {
+ try
+ {
+ new ServletURL(null, "/invalidcontextpath/", null, null, null);
+ fail("The context path must not end with a \"/\" character");
+ }
+ catch (IllegalArgumentException expected)
+ {
+ assertEquals("The Context Path must not end with a \"/\""
+ + " character.", expected.getMessage());
+ }
+ }
+
+ /**
+ * Verify that the context path can be an empty string.
+ */
+ public void testSetContextPathEmptyString()
+ {
+ ServletURL servletURL = new ServletURL(null, "", null, null, null);
+ assertEquals("", servletURL.getContextPath());
+ }
+
+ /**
* Verify that the <code>getHost()</code> method is returning the correct
* host when a port is specified and that the <code>getPort()</code>
* method returns the specified port.
1.12 +39 -11
jakarta-cactus/framework/src/java/share/org/apache/cactus/ServletURL.java
Index: ServletURL.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/ServletURL.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ServletURL.java 20 Feb 2003 13:50:16 -0000 1.11
+++ ServletURL.java 7 Jun 2003 09:27:41 -0000 1.12
@@ -72,13 +72,26 @@
* <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the
* ServletContext that this servlet is a part of. If this context is the
* default context rooted at the base of the web server's URL namespace, this
- * path will be an empty string. Otherwise, this path starts with a character
- * but does not end with a character.</li>
- * <li><b>Servlet Path</b>: The path section that directly corresponds to the
- * mapping which activated this request. This path starts with a
+ * path will be an empty string. Otherwise, this path starts with a "/"
+ * character but does not end with a "/" character.</li>
+ * <li><b>Servlet Path</b>: The path section that directly corresponds to the
+ * mapping which activated this request. This path starts with a "/"
* character.</li>
- * <li><b>PathInfo</b>: The part of the request path that is not part of the
+ * <li><b>PathInfo</b>: The part of the request path that is not part of the
* Context Path or the Servlet Path.</li></ul></pre></code>
+ * From the Servlet 2.3 specification :<br>
+ * <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the
+ * ServletContext that this servlet is a part of. If this context is the
+ * default context rooted at the base of the web server's URL namespace, this
+ * path will be an empty string. Otherwise, this path starts with a "/"
+ * character but does not end with a "/" character.</li>
+ * <li><b>Servlet Path</b>: The path section that directly corresponds to the
+ * mapping which activated this request. This path starts with a "/"
+ * character <b>except in the case where the request is matched with the
+ * "/*" pattern, in which case it is the empty string</b>.</li>
+ * <li><b>PathInfo</b>: The part of the request path that is not part of the
+ * Context Path or the Servlet Path. <b>It is either null if there is no
+ * extra path, or is a string with a leading "/"</b>.</li></ul></pre></code>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
@@ -211,8 +224,8 @@
* <code>HttpServletRequest.getContextPath()</code>.
* Can be null. If null, then the context from the
* Servlet Redirector will be returned.
- * Format: "/" + name or an empty string
- * for the default context.
+ * Format: "/" + name or an empty string for the
+ * default context. Must not end with a "/" character.
* @param theServletPath the servlet path in the URL to simulate,
* i.e. this is the name that will be returned by the
* <code>HttpServletRequest.getServletPath()</code>.
@@ -252,8 +265,8 @@
* <code>HttpServletRequest.getContextPath()</code>.
* Can be null. If null, then the context from the
* Servlet Redirector will be returned.
- * Format: "/" + name or an empty string
- * for the default context.
+ * Format: "/" + name or an empty string for the
+ * default context. Must not end with a "/" character.
* @param theServletPath the servlet path in the URL to simulate,
* i.e. this is the name that will be returned by the
* <code>HttpServletRequest.getServletPath()</code>.
@@ -420,12 +433,27 @@
* name that will be returned by the
* <code>HttpServletRequest.getContextPath()</code>. If not set, the
* context from the Servlet Redirector will be returned. Format: "/" +
- * name or an empty string for the default context.
+ * name or an empty string for the default context. If not an empty
+ * string the last character must not be "/".
*
* @param theContextPath the context path to simulate
*/
public void setContextPath(String theContextPath)
{
+ if ((theContextPath != null) && (theContextPath.length() > 0))
+ {
+ if (!theContextPath.startsWith("/"))
+ {
+ throw new IllegalArgumentException("The Context Path must"
+ + " start with a \"/\" character.");
+ }
+ if (theContextPath.endsWith("/"))
+ {
+ throw new IllegalArgumentException("The Context Path must not"
+ + " end with a \"/\" character.");
+ }
+ }
+
this.contextPath = theContextPath;
}
1.13 +6 -8
jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractHttpServletRequestWrapper.java
Index: AbstractHttpServletRequestWrapper.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractHttpServletRequestWrapper.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- AbstractHttpServletRequestWrapper.java 26 May 2003 11:45:22 -0000 1.12
+++ AbstractHttpServletRequestWrapper.java 7 Jun 2003 09:27:41 -0000 1.13
@@ -166,20 +166,18 @@
/**
* @return the context path from the simulated URL or the real context path
- * if a simulation URL has not been defined.
+ * if a simulation URL has not been defined. The real context path
+ * will be returned if the simulated URL as defined a null value
+ * for the context path.
*/
public String getContextPath()
{
- String result;
+ String result = this.request.getContextPath();
- if (this.url != null)
+ if ((this.url != null) && (this.url.getContextPath() != null))
{
result = this.url.getContextPath();
LOGGER.debug("Using simulated context : [" + result + "]");
- }
- else
- {
- result = this.request.getContextPath();
}
return result;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]