You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2003/02/07 05:23:50 UTC
cvs commit: jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock MockRequestDispatcher.java MockTester.java MockTestCase.java MockRequest.java MockResponse.java MockContext.java
hlship 2003/02/06 20:23:50
Modified: junit/src/net/sf/tapestry/junit/mock MockTester.java
MockTestCase.java MockRequest.java
MockResponse.java MockContext.java
Added: junit/src/net/sf/tapestry/junit/mock
MockRequestDispatcher.java
Log:
Add a set of unit tests for handling of the RedirectException.
Revision Changes Path
1.14 +31 -9 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockTester.java
Index: MockTester.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockTester.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- MockTester.java 24 Jan 2003 00:49:32 -0000 1.13
+++ MockTester.java 7 Feb 2003 04:23:49 -0000 1.14
@@ -124,6 +124,7 @@
private ApplicationServlet _servlet;
private MockRequest _request;
private MockResponse _response;
+ private int _requestNumber = 0;
private Map _ognlContext;
/**
@@ -182,6 +183,8 @@
{
Element request = (Element) l.get(i);
+ _requestNumber = i + 1;
+
executeRequest(request);
}
}
@@ -439,7 +442,8 @@
if (result)
return;
- throw new AssertionFailedError(name + ": Expression '" + expression + "' was not true.");
+ throw new AssertionFailedError(
+ buildTestName(name) + ": Expression '" + expression + "' was not true.");
}
@@ -597,7 +601,10 @@
System.err.println(text);
throw new AssertionFailedError(
- name + ": Response does not contain regular expression '" + pattern + "'.");
+ buildTestName(name)
+ + ": Response does not contain regular expression '"
+ + pattern
+ + "'.");
}
private void matchSubstring(String name, String text, String substring)
@@ -608,7 +615,7 @@
System.err.println(text);
throw new AssertionFailedError(
- name + ": Response does not contain string '" + substring + "'.");
+ buildTestName(name) + ": Response does not contain string '" + substring + "'.");
}
private void executeOutputMatchesAssertions(Element request) throws DocumentParseException
@@ -657,7 +664,8 @@
if (i >= count)
{
System.err.println(outputString);
- throw new AssertionFailedError(name + ": Too many matches for '" + pattern + "'.");
+ throw new AssertionFailedError(
+ buildTestName(name) + ": Too many matches for '" + pattern + "'.");
}
Element e = (Element) l.get(i);
@@ -668,7 +676,14 @@
{
System.err.println(outputString);
throw new AssertionFailedError(
- name + "[" + i + "]: Expected '" + expected + "' but got '" + actual + "'.");
+ buildTestName(name)
+ + "["
+ + i
+ + "]: Expected '"
+ + expected
+ + "' but got '"
+ + actual
+ + "'.");
}
i++;
@@ -678,7 +693,7 @@
{
System.err.println(outputString);
throw new AssertionFailedError(
- name
+ buildTestName(name)
+ ": Too few matches for '"
+ pattern
+ "' (expected "
@@ -718,7 +733,8 @@
return;
throw new AssertionFailedError(
- "Response cookie '"
+ buildTestName(name)
+ + ": Response cookie '"
+ name
+ "': expected '"
+ value
@@ -727,7 +743,13 @@
+ "'.");
}
- throw new AssertionFailedError("Could not find cookie named '" + name + "' in response.");
+ throw new AssertionFailedError(
+ buildTestName(name) + ": Could not find cookie named '" + name + "' in response.");
+ }
+
+ private String buildTestName(String name)
+ {
+ return "Request #" + _requestNumber + "/" + name;
}
}
1.18 +23 -1 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockTestCase.java
Index: MockTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockTestCase.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- MockTestCase.java 1 Feb 2003 16:47:21 -0000 1.17
+++ MockTestCase.java 7 Feb 2003 04:23:49 -0000 1.18
@@ -95,6 +95,28 @@
// in a static suite() method?
/**
+ * Test the ListEdit component.
+ *
+ **/
+
+ public void testListEdit()
+ throws Exception
+ {
+ attempt("TestListEdit.xml");
+ }
+
+ /**
+ * Test handling of internal and external redirects.
+ *
+ **/
+
+ public void testRedirect()
+ throws Exception
+ {
+ attempt("TestRedirect.xml");
+ }
+
+ /**
* Test ability of the enhancer to create properties for
* connected parameters.
*
1.7 +3 -3 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockRequest.java
Index: MockRequest.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockRequest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MockRequest.java 27 Jan 2003 21:31:38 -0000 1.6
+++ MockRequest.java 7 Feb 2003 04:23:49 -0000 1.7
@@ -357,9 +357,9 @@
return false;
}
- public RequestDispatcher getRequestDispatcher(String arg0)
+ public RequestDispatcher getRequestDispatcher(String path)
{
- return null;
+ return _servletContext.getRequestDispatcher(path);
}
public String getRealPath(String arg0)
1.4 +19 -2 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockResponse.java
Index: MockResponse.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockResponse.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MockResponse.java 17 Jan 2003 17:41:28 -0000 1.3
+++ MockResponse.java 7 Feb 2003 04:23:49 -0000 1.4
@@ -81,6 +81,8 @@
private ServletOutputStream _outputStream;
private List _cookies = new ArrayList();
+ private String _redirectLocation;
+
private class ServletOutputStreamImpl extends ServletOutputStream
{
private ServletOutputStreamImpl()
@@ -177,6 +179,18 @@
if (_commited)
throw new IllegalStateException("sendRedirect() when committed.");
+ if (location.endsWith("/FAIL_IO"))
+ throw new IOException("Forced IOException in MockResponse.sendRedirect().");
+
+ _redirectLocation = location;
+
+ _commited = true;
+
+ }
+
+ public String getRedirectLocation()
+ {
+ return _redirectLocation;
}
public void setDateHeader(String name, long value)
@@ -282,7 +296,10 @@
public void end() throws IOException
{
- _outputStream.close();
+ // For redirects, we may never open an output stream.
+
+ if (_outputStream != null)
+ _outputStream.close();
}
/**
1.6 +37 -18 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockContext.java
Index: MockContext.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockContext.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MockContext.java 17 Jan 2003 17:41:28 -0000 1.5
+++ MockContext.java 7 Feb 2003 04:23:49 -0000 1.6
@@ -80,13 +80,13 @@
public class MockContext extends AttributeHolder implements ServletContext, IInitParameterHolder
{
private MockSession _session;
-
+
/**
* Directory, relative to the current directory (i.e., System property user.dir)
* that is the context root.
*
**/
-
+
private String _rootDirectory = "context";
private String _servletContextName = "test";
private Map _initParameters = new HashMap();
@@ -118,25 +118,25 @@
public URL getResource(String path) throws MalformedURLException
{
- if (path == null || ! path.startsWith("/"))
- throw new MalformedURLException("Not a valid context path.");
-
+ if (path == null || !path.startsWith("/"))
+ throw new MalformedURLException("Not a valid context path.");
+
StringBuffer buffer = new StringBuffer();
-
+
buffer.append(System.getProperty("user.dir"));
- buffer.append("/");
+ buffer.append("/");
buffer.append(_rootDirectory);
-
+
// Path has a leading slash
-
+
buffer.append(path);
-
+
File file = new File(buffer.toString());
-
+
if (file.exists())
return file.toURL();
-
- return null;
+
+ return null;
}
public InputStream getResourceAsStream(String path)
@@ -144,10 +144,10 @@
try
{
URL url = getResource(path);
-
+
if (url == null)
return null;
-
+
return url.openStream();
}
catch (MalformedURLException ex)
@@ -160,9 +160,28 @@
}
}
+ /**
+ * Gets a dispatcher for the given path. Path should be a relative path (relative
+ * to the context). A special case: "NULL" returns null (i.e., when a
+ * dispatcher can't be found).
+ *
+ **/
+
public RequestDispatcher getRequestDispatcher(String path)
{
- return null;
+ if (path.endsWith("/NULL"))
+ return null;
+
+ StringBuffer buffer = new StringBuffer(_rootDirectory);
+ buffer.append(path);
+
+ // Simulate the handling of directories by serving the index.html
+ // in the directory.
+
+ if (path.endsWith("/"))
+ buffer.append("index.html");
+
+ return new MockRequestDispatcher(buffer.toString());
}
public RequestDispatcher getNamedDispatcher(String name)
@@ -240,7 +259,7 @@
return _session;
}
-
+
public MockSession getSession()
{
return _session;
1.1 jakarta-tapestry/junit/src/net/sf/tapestry/junit/mock/MockRequestDispatcher.java
Index: MockRequestDispatcher.java
===================================================================
/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Tapestry" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Tapestry", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package net.sf.tapestry.junit.mock;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* Used to enable mock testing of internal request forwarding.
*
* @author Howard Lewis Ship
* @version $Id: MockRequestDispatcher.java,v 1.1 2003/02/07 04:23:49 hlship Exp $
* @since 2.4
*
**/
public class MockRequestDispatcher implements RequestDispatcher
{
private String _resourcePath;
public MockRequestDispatcher(String resourcePath)
{
_resourcePath = resourcePath;
}
public void forward(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
if (_resourcePath.endsWith("/FAIL_SERVLET"))
throw new ServletException("Test-directed ServletException from RequestDispatcher forward().");
// For testing purposes, assume we only forward to static HTML files.
InputStream in = new FileInputStream(_resourcePath);
response.setContentType("test/html");
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1000];
while (true)
{
int length = in.read(buffer);
if (length < 0)
break;
out.write(buffer, 0, length);
}
in.close();
out.close();
}
public void include(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
throw new ServletException("MockRequestDispatcher.include() not supported.");
}
}