[Jsptest-svn-commits] SF.net SVN: jsptest:[267] trunk/jsptest-generic
Status: Alpha
Brought to you by:
lkoskela
From: <rc...@us...> - 2012-04-06 17:59:29
|
Revision: 267 http://jsptest.svn.sourceforge.net/jsptest/?rev=267&view=rev Author: rcw3bb Date: 2012-04-06 17:59:22 +0000 (Fri, 06 Apr 2012) Log Message: ----------- Implemented Request Parameters. Modified Paths: -------------- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestJspTestCase.java trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java trunk/jsptest-generic/jsptest-framework/src/test/resources/jsptest.properties trunk/jsptest-generic/jsptest-framework/src/test/resources/websrc/index.jsp Added Paths: ----------- trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspX.java trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspXCompiler.java trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspXCompiler.java Added: trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspX.java =================================================================== --- trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspX.java (rev 0) +++ trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspX.java 2012-04-06 17:59:22 UTC (rev 267) @@ -0,0 +1,15 @@ +package net.sf.jsptest.compiler.api; + +import java.util.Map; + +/** + * This is a Jsp Extensions however it is not a subclass of Jsp. + * This is implemented like this to avoid breaking previous codes and maintains just one method to implement. + * + * @author Ronaldo Webb + */ +public interface JspX { + + JspExecution request(String httpMethod, Map requestAttributes, Map sessionAttributes, Map requestParameters); + +} Added: trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspXCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspXCompiler.java (rev 0) +++ trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspXCompiler.java 2012-04-06 17:59:22 UTC (rev 267) @@ -0,0 +1,21 @@ +package net.sf.jsptest.compiler.api; + +import java.util.Map; + +/** + * An extension of the JspCompiler that can return JspX instance.if + * Implement this interface if you want the capability of using request parameters. + * + * @author Ronaldo Webb + * + */ +public interface JspXCompiler extends JspCompiler { + + /** + * The the same compile method as the JspCompiler but returns JspX instance. + * @param path The path to compile. + * @param taglibs Maps of taglibs. + * @return An instance of JspX. + */ + JspX compileX(String path, Map taglibs); +} Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java 2010-06-13 20:28:53 UTC (rev 266) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java 2012-04-06 17:59:22 UTC (rev 267) @@ -24,6 +24,9 @@ import net.sf.jsptest.compiler.api.JspCompiler; import net.sf.jsptest.compiler.api.JspCompilerFactory; import net.sf.jsptest.compiler.api.JspExecution; +import net.sf.jsptest.compiler.api.JspX; +import net.sf.jsptest.compiler.api.JspXCompiler; + import org.apache.log4j.Logger; /** @@ -33,6 +36,7 @@ * * @author Lasse Koskela * @author Meinert Schwartau (scwar32) + * @author Ronaldo Webb */ public abstract class JspTestCase extends TestCase { @@ -40,6 +44,7 @@ private Map requestAttributes; private Map sessionAttributes; private Map substituteTaglibs; + private Map requestParams; private JspExecution execution; public JspTestCase() { @@ -54,6 +59,7 @@ requestAttributes = new HashMap(); sessionAttributes = new HashMap(); substituteTaglibs = new HashMap(); + requestParams = new HashMap(); } /** @@ -89,6 +95,28 @@ } /** + * Sets a request string parameter with single value. + * @param param + * Name of the parameter + * @param value + * Value of the parameter. + */ + protected void setRequestParameter(String param, String value) { + requestParams.put(param, value); + } + + /** + * Sets a request string parameter with multiple values. + * @param param + * Name of the parameter + * @param values + * Value of the parameter. + */ + protected void setRequestParameter(String param, String[] values) { + requestParams.put(param, values); + } + + /** * Simulate a HTTP GET request to the specified JSP file. * * @param path @@ -121,15 +149,27 @@ protected void request(String path, String httpMethod) throws Exception { validatePath(path); JspCompiler compiler = JspCompilerFactory.newInstance(); + log.debug("Using compiler " + compiler.getClass().getName() + " and webroot " + new File(getWebRoot()).getAbsolutePath()); + compiler.setWebRoot(getWebRoot()); compiler.setOutputDirectory(getOutputDirectory()); - Jsp jsp = compiler.compile(path, substituteTaglibs); - log.debug("Simulating a request to " + path); - execution = jsp.request(httpMethod, requestAttributes, sessionAttributes); + + if (compiler instanceof JspXCompiler) { + log.debug("Using JspXCompiler"); + JspX jsp = ((JspXCompiler) compiler).compileX(path, substituteTaglibs); + log.debug("Simulating a request to " + path); + execution = jsp.request(httpMethod, requestAttributes, sessionAttributes, requestParams); + } + else { + log.debug("Using JspCompiler"); + Jsp jsp = compiler.compile(path, substituteTaglibs); + log.debug("Simulating a request to " + path); + execution = jsp.request(httpMethod, requestAttributes, sessionAttributes); + } } - + private void validatePath(String path) { if (!path.startsWith("/")) { throw new IllegalArgumentException("The JSP path must start with a \"/\""); Modified: trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestJspTestCase.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestJspTestCase.java 2010-06-13 20:28:53 UTC (rev 266) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestJspTestCase.java 2012-04-06 17:59:22 UTC (rev 267) @@ -18,6 +18,7 @@ import junit.framework.AssertionFailedError; import junit.framework.TestCase; import net.sf.jsptest.compiler.dummy.FakeJspCompiler; +import net.sf.jsptest.compiler.dummy.FakeJspXCompiler; /** * @author Lasse Koskela @@ -102,4 +103,49 @@ assertEquals(IllegalArgumentException.class, e.getClass()); } } + + public void testJspWithNoParam() throws Exception { + testcase.get("/index"); + testcase.output().shouldNotContain("Params"); + } + + public void testJspWithMultipleParamValues() throws Exception { + testcase.setUp(); + testcase.setRequestParameter("Name", new String[] {"Value1", "Value2", "Value3"}); + testcase.get("/index"); + testcase.output().shouldContain("Params"); + testcase.output().shouldContain("Name"); + testcase.output().shouldContain("Value1"); + testcase.output().shouldContain("Value2"); + testcase.output().shouldContain("Value3"); + } + + public void testJspWithOneParam() throws Exception { + testcase.setUp(); + testcase.setRequestParameter("Name", "Value"); + testcase.get("/index"); + testcase.output().shouldContain("Params"); + testcase.output().shouldContain("Name"); + testcase.output().shouldContain("Value"); + } + + public void testJspWithMultipleParamNullValues() throws Exception { + testcase.setUp(); + testcase.setRequestParameter("Name", new String[] {null, "Value1", "Value2"}); + testcase.get("/index"); + testcase.output().shouldContain("Params"); + testcase.output().shouldContain("Name"); + testcase.output().shouldContain("null"); + testcase.output().shouldContain("Value1"); + testcase.output().shouldContain("Value2"); + } + + public void testJspWithOneParamNullValue() throws Exception { + testcase.setUp(); + testcase.setRequestParameter("Name", (String) null); + testcase.get("/index"); + testcase.output().shouldContain("Params"); + testcase.output().shouldContain("Name"); + testcase.output().shouldContain("null"); + } } Modified: trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java 2010-06-13 20:28:53 UTC (rev 266) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java 2012-04-06 17:59:22 UTC (rev 267) @@ -31,6 +31,10 @@ public static void appendOutput(String content) { fakedOutput.append(content); } + + public static StringBuffer getOutput() { + return fakedOutput; + } public Jsp compile(String path, Map taglibs) { lastCompiledWebRoot = getWebRoot(); @@ -48,11 +52,19 @@ } }; } - + + public static void setLastCompiledPath(String path) { + lastCompiledPath = path; + } + public static String lastCompiledPath() { return lastCompiledPath; } + public static void setLastCompiledWebRoot(String webRoot) { + lastCompiledWebRoot = webRoot; + } + public static String lastCompiledWebRoot() { return lastCompiledWebRoot; } Added: trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspXCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspXCompiler.java (rev 0) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspXCompiler.java 2012-04-06 17:59:22 UTC (rev 267) @@ -0,0 +1,60 @@ +package net.sf.jsptest.compiler.dummy; + +import java.util.Iterator; +import java.util.Map; + +import net.sf.jsptest.compiler.api.JspExecution; +import net.sf.jsptest.compiler.api.JspX; +import net.sf.jsptest.compiler.api.JspXCompiler; + +public class FakeJspXCompiler extends FakeJspCompiler implements JspXCompiler { + + public JspX compileX(final String path, Map taglibs) { + return new JspX() { + { + FakeJspCompiler.setLastCompiledWebRoot(getWebRoot()); + FakeJspCompiler.setLastCompiledPath(path); + } + + public JspExecution request(String httpMethod, + Map requestAttributes, Map sessionAttributes, + final Map requestParameters) { + return new JspExecution() { + public String getRenderedResponse() { + { + if (null != requestParameters && !requestParameters.isEmpty()) { + Iterator keys = requestParameters.keySet().iterator(); + FakeJspCompiler.cleanOutput(); + FakeJspCompiler.appendOutput("Params"); + + while (keys.hasNext()) { + String key = (String) keys.next(); + Object value = requestParameters.get(key); + + if (null != value) { + if (value instanceof String[]) { + FakeJspCompiler.appendOutput(key); + String[] values=(String[]) value; + for (int i = 0; i < values.length; i++) { + FakeJspCompiler.appendOutput(values[i]); + } + } else { + FakeJspCompiler.appendOutput(key); + FakeJspCompiler.appendOutput((String) value); + } + } + else { + FakeJspCompiler.appendOutput(key); + FakeJspCompiler.appendOutput((String) null); + } + } + } + + } + return FakeJspCompiler.getOutput().toString(); + } + }; + } + }; + } +} Modified: trunk/jsptest-generic/jsptest-framework/src/test/resources/jsptest.properties =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/resources/jsptest.properties 2010-06-13 20:28:53 UTC (rev 266) +++ trunk/jsptest-generic/jsptest-framework/src/test/resources/jsptest.properties 2012-04-06 17:59:22 UTC (rev 267) @@ -1 +1 @@ -jsptest.compiler.implementation=net.sf.jsptest.compiler.dummy.FakeJspCompiler \ No newline at end of file +jsptest.compiler.implementation=net.sf.jsptest.compiler.dummy.FakeJspXCompiler \ No newline at end of file Modified: trunk/jsptest-generic/jsptest-framework/src/test/resources/websrc/index.jsp =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/resources/websrc/index.jsp 2010-06-13 20:28:53 UTC (rev 266) +++ trunk/jsptest-generic/jsptest-framework/src/test/resources/websrc/index.jsp 2012-04-06 17:59:22 UTC (rev 267) @@ -1,3 +1,17 @@ <%@ page language="Java" %> +<%@ page import="java.util.Enumeration" %> Hello <%= "from" %> Jasper -The time is <%= new java.util.Date().toString() %> \ No newline at end of file +The time is <%= new java.util.Date().toString() %> +<% + boolean display=true; + Enumeration paramNames=request.getParameterNames(); + while (paramNames.hasMoreElements()) { + if (display) { + out.println("Params<br/>"); + display=false; + } + String paramName = (String) paramNames.nextElement(); + String paramValue = request.getParameter(paramName); + out.println(paramName + "=" + paramValue + "<br/>"); + } +%> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |