[Jsptest-svn-commits] SF.net SVN: jsptest:[269] trunk
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2012-04-11 23:16:30
|
Revision: 269 http://jsptest.svn.sourceforge.net/jsptest/?rev=269&view=rev Author: lkoskela Date: 2012-04-11 23:16:22 +0000 (Wed, 11 Apr 2012) Log Message: ----------- Revert revisions 267 and 268 in preparation to implementing request parameters directly onto Jsp* rather than a parallel JspX* hierarchy of classes. 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 trunk/jsptest-jsp21/pom.xml trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java Removed 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 trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JasperCompiler.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspExecutionImpl.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspImpl.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXCompilerImpl.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXImpl.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/mock/ trunk/jsptest-jsp21/src/main/java/org/ trunk/jsptest-jsp21/src/test/java/net/ trunk/jsptest-jsp21/src/test/resources/ Deleted: 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspX.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,15 +0,0 @@ -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); - -} Deleted: 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspXCompiler.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,21 +0,0 @@ -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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java 2012-04-11 23:16:22 UTC (rev 269) @@ -24,9 +24,6 @@ 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; /** @@ -36,7 +33,6 @@ * * @author Lasse Koskela * @author Meinert Schwartau (scwar32) - * @author Ronaldo Webb */ public abstract class JspTestCase extends TestCase { @@ -44,7 +40,6 @@ private Map requestAttributes; private Map sessionAttributes; private Map substituteTaglibs; - private Map requestParams; private JspExecution execution; public JspTestCase() { @@ -59,7 +54,6 @@ requestAttributes = new HashMap(); sessionAttributes = new HashMap(); substituteTaglibs = new HashMap(); - requestParams = new HashMap(); } /** @@ -95,28 +89,6 @@ } /** - * 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 @@ -149,27 +121,15 @@ 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()); - - 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); - } + 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestJspTestCase.java 2012-04-11 23:16:22 UTC (rev 269) @@ -18,7 +18,6 @@ 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 @@ -103,49 +102,4 @@ 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java 2012-04-11 23:16:22 UTC (rev 269) @@ -31,10 +31,6 @@ public static void appendOutput(String content) { fakedOutput.append(content); } - - public static StringBuffer getOutput() { - return fakedOutput; - } public Jsp compile(String path, Map taglibs) { lastCompiledWebRoot = getWebRoot(); @@ -52,19 +48,11 @@ } }; } - - 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; } Deleted: 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspXCompiler.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,60 +0,0 @@ -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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/test/resources/jsptest.properties 2012-04-11 23:16:22 UTC (rev 269) @@ -1 +1 @@ -jsptest.compiler.implementation=net.sf.jsptest.compiler.dummy.FakeJspXCompiler \ No newline at end of file +jsptest.compiler.implementation=net.sf.jsptest.compiler.dummy.FakeJspCompiler \ 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 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-generic/jsptest-framework/src/test/resources/websrc/index.jsp 2012-04-11 23:16:22 UTC (rev 269) @@ -1,17 +1,3 @@ <%@ page language="Java" %> -<%@ page import="java.util.Enumeration" %> Hello <%= "from" %> Jasper -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 +The time is <%= new java.util.Date().toString() %> \ No newline at end of file Modified: trunk/jsptest-jsp21/pom.xml =================================================================== --- trunk/jsptest-jsp21/pom.xml 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/pom.xml 2012-04-11 23:16:22 UTC (rev 269) @@ -30,15 +30,10 @@ </dependency> <!-- Provides the Jasper JSP compiler --> <dependency> - <groupId>tomcat</groupId> - <artifactId>jasper-compiler</artifactId> - <version>5.5.15</version> + <groupId>org.apache.tomcat</groupId> + <artifactId>jasper</artifactId> + <version>6.0.13</version> </dependency> - <dependency> - <groupId>tomcat</groupId> - <artifactId>jasper-runtime</artifactId> - <version>5.5.15</version> - </dependency> </dependencies> <build> <resources> Deleted: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JasperCompiler.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JasperCompiler.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JasperCompiler.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,375 +0,0 @@ -/* - * Copyright 2007 Lasse Koskela. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package net.sf.jsptest.compiler.jsp21; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.jsp.tagext.TagInfo; -import net.sf.jsptest.compiler.JspCompilationInfo; -import net.sf.jsptest.compiler.java.CommandLineJavac; -import net.sf.jsptest.compiler.java.Java6Compiler; -import net.sf.jsptest.compiler.java.JavaCompiler; -import net.sf.jsptest.compiler.java.SunJavaC; -import net.sf.jsptest.compiler.jsp21.mock.MockOptions; -import net.sf.jsptest.compiler.jsp21.mock.MockServletConfig; -import net.sf.jsptest.compiler.jsp21.mock.MockTagInfo; -import net.sf.jsptest.utils.Path; -import net.sf.jsptest.utils.Strings; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.jasper.EmbeddedServletOptions; -import org.apache.jasper.JasperException; -import org.apache.jasper.JspCompilationContext; -import org.apache.jasper.Options; -import org.apache.jasper.compiler.AntCompiler; -import org.apache.jasper.compiler.Compiler; -import org.apache.jasper.compiler.JspRuntimeContext; -import org.apache.jasper.compiler.ServletWriter; -import org.apache.jasper.servlet.JspCServletContext; -import org.apache.jasper.servlet.JspServletWrapper; - -/** - * The <tt>JasperTestCase</tt> provides a facility for compiling JavaServer Pages outside a real - * Servlet/JSP container. - * <p> - * It makes use of Jakarta Tomcat's Jasper JSP compiler to compile a JSP file into Java source code, - * and then Sun's javac implementation to compile the Java source code into Java bytecode. - * <p> - * The resulting .class file is written under a "WEB-INF/classes" directory under the "web root" - * defined by concrete subclasses through the implementation of <tt>getWebRoot()</tt>. If you - * want the .class files to be generated somewhere else than under the web root, you can also - * override <tt>getClassOutputBaseDir()</tt>, which specifies the root directory for the compiled - * .class files. - * <p> - * The resulting Servlet class gets its package based on the <tt>getJspPackageName()</tt> method - * which can be overridden if necessary. The default is "jsp" which means that, for example, a JSP - * named "front_page.jsp" would eventually be translated into a class file - * "[webroot]/WEB-INF/classes/jsp/front_page_jsp.class" where "jsp/" is the JSP package name and - * "front_page_jsp.class" the normalized class name derived from the source JSP file's name. - * - * @author Lasse Koskela - * @author Meinert Schwartau (scwar32) - */ -public class JasperCompiler { - - private static final Log log = LogFactory.getLog(JasperCompiler.class); - private static JavaCompiler COMPILER = determineJavaCompiler(); - private String webRoot; - private String classOutputBaseDir; - private String jspPackageName; - - public JasperCompiler() { - webRoot = "."; - classOutputBaseDir = "."; - } - - /** - * Sets the "web root", i.e. the root directory of your exploded J2EE web application. In other - * words, this is the directory under which you should have a subdirectory named "WEB-INF". - */ - public void setWebRoot(String webRoot) { - this.webRoot = webRoot; - } - - /** - * Sets the directory where generated .class file(s) should be written.. - */ - public void setClassOutputBaseDir(String directory) { - this.classOutputBaseDir = directory; - } - - /** - * Compile the specified JSP source file into bytecode. - * - * @param path - * The path to the JSP source file to compile, given relative to the web root. - * @param mockTaglibs - * Mapping of tag names to tag handler classes - */ - public JspCompilationInfo compile(String path, Map mockTaglibs) throws Exception { - JspCompilationInfo info = createJspCompilationInfo(path, mockTaglibs); - if (info.jspCompilationRequired()) { - compileJsp(info); - compileJavaToBytecode(info); - } else { - log.debug(" No compilation needed for " + info.getJspSource()); - } - return info; - } - - /** - * Sets the package name for the generated Java classes. The default package name is "jsp". - */ - public void setJspPackageName(String packageName) { - this.jspPackageName = packageName; - } - - /** - * Returns the package name for the generated Java class. - */ - private String getJspPackageName() { - if (jspPackageName != null) { - return jspPackageName; - } else { - return "jsp"; - } - } - - private JspCompilationInfo createJspCompilationInfo(String jsp, Map mockTaglibs) { - JspCompilationInfo info = new JspCompilationInfo(); - info.setJspPath(jsp); - info.setClassOutputDir(classOutputBaseDir); - info.setJspSource(resolveJspSourceFile(jsp)); - info.setWebRoot(getWebRoot()); - info.setTaglibs(mockTaglibs); - resolveJavaSourceFile(info); - resolveClassFileLocation(info); - resolveClassName(info); - return info; - } - - private String getWebRoot() { - File root = new File(webRoot); - if (root.exists() && root.isDirectory()) { - return root.getAbsolutePath(); - } else { - return resolveWebRootFromClassPath(); - } - } - - private String resolveWebRootFromClassPath() { - String path = webRoot; - if (path.startsWith("./")) { - path = path.substring(2); - } - URL url = getClass().getClassLoader().getResource(path); - if (url == null) { - return webRoot; - } - if (!url.toExternalForm().startsWith("file:")) { - log.info("Web root referenced a non-filesystem resource: " + url); - return webRoot; - } - return new File(url.toExternalForm().substring("file:".length())).getAbsolutePath(); - } - - private void compileJsp(JspCompilationInfo info) throws Exception { - assertTrue("Source file " + new File(info.getJspSource()).getAbsolutePath() - + " does not exist", new File(info.getJspSource()).exists()); - PrintWriter logWriter = new PrintWriter(new StringWriter()); - URL baseUrl = new File(info.getWebRoot()).toURL(); - ServletContext sContext = new JspCServletContext(logWriter, baseUrl); - ServletConfig sConfig = new MockServletConfig(sContext); - Options options = createOptions(sContext, sConfig, info); - JspRuntimeContext rtContext = new JspRuntimeContext(sContext, options); - JspServletWrapper sWrapper = makeWrapper(sContext, options, rtContext); - JspCompilationContext cContext = createJspCompilationContext(info, sContext, options, - rtContext, sWrapper, new StringWriter()); - logCompilation(info.getJspSource(), info.getClassOutputDir()); - compileJspToJava(sWrapper, cContext); - File javaFile = new File(info.getJavaSource()); - assertTrue("Failed to generate .java source code to " + javaFile.getAbsolutePath(), - javaFile.exists()); - info.compilationWasSuccessful(); - } - - private void compileJspToJava(JspServletWrapper jspServletWrapper, - JspCompilationContext jspCompilationContext) throws FileNotFoundException, - JasperException, Exception { - Compiler compiler = new AntCompiler(); - compiler.init(jspCompilationContext, jspServletWrapper); - compiler.compile(); - } - - private JspCompilationContext createJspCompilationContext(JspCompilationInfo info, - ServletContext servletContext, Options options, JspRuntimeContext jspRuntimeContext, - JspServletWrapper jspServletWrapper, StringWriter stringWriter) { - boolean isErrorPage = false; - JspCompilationContext cContext = new JspCompilationContext(info.getJspPath(), isErrorPage, - options, servletContext, jspServletWrapper, jspRuntimeContext); - cContext.getOutputDir(); // forces creation of the directory tree - cContext.setServletJavaFileName(info.getJavaSource()); - cContext.setServletPackageName(getJspPackageName()); - cContext.setWriter(new ServletWriter(new PrintWriter(stringWriter))); - createPathToGeneratedJavaSource(info); - return cContext; - } - - private void createPathToGeneratedJavaSource(JspCompilationInfo info) { - new File(info.getJavaSource()).getParentFile().mkdirs(); - } - - private JspServletWrapper makeWrapper(ServletContext servletContext, Options options, - JspRuntimeContext jspRuntimeContext) throws MalformedURLException, JasperException { - TagInfo tagInfo = new MockTagInfo(); - String tagFilePath = "/"; - URL tagFileJarUrl = new File(".").toURL(); - JspServletWrapper wrapper = new JspServletWrapper(servletContext, options, tagFilePath, - tagInfo, jspRuntimeContext, tagFileJarUrl); - return wrapper; - } - - private Options createOptions(ServletContext ctx, ServletConfig cfg, JspCompilationInfo info) { - Options options = new EmbeddedServletOptions(cfg, ctx); - return new MockOptions(options, ctx, info); - } - - private void resolveJavaSourceFile(JspCompilationInfo info) { - File dir = new File(info.getClassOutputDir()); - if (getJspPackageName().length() > 0) { - dir = new File(dir, getJspPackageName().replace('.', '/')); - } - dir.mkdirs(); - String name = resolveJavaSourceFileName(info.getJspPath()); - info.setJavaSource(new File(dir, name).getPath()); - } - - private String resolveJavaSourceFileName(String jspPath) { - String name = encodeSpecialCharacters(jspPath); - if (name.startsWith("/")) { - name = name.substring(1); - } - return name + ".java"; - } - - private String encodeSpecialCharacters(String name) { - StringBuffer result = new StringBuffer(); - char[] chars = name.toCharArray(); - for (int i = 0; i < chars.length; i++) { - if (chars[i] == '-') { - result.append("_002d"); - } else if (chars[i] == '_') { - result.append("_005f"); - } else if (chars[i] == '.') { - result.append("_"); - } else { - result.append(chars[i]); - } - } - return result.toString(); - } - - private String resolveJspSourceFile(String jsp) { - if (jsp.startsWith("/")) { - jsp = jsp.substring(1); - } - return new File(getWebRoot(), jsp).getPath(); - } - - private void resolveClassName(JspCompilationInfo info) { - String baseName = new File(info.getJavaSource()).getName(); - baseName = baseName.substring(0, baseName.indexOf(".")); - String packageName = getPackagePrefix() + getSubDirPackagePrefix(info); - info.setClassName(packageName + baseName); - } - - private String getPackagePrefix() { - String packagePrefix = getJspPackageName(); - if (packagePrefix != null && packagePrefix.length() > 0) { - packagePrefix = packagePrefix + "."; - } - return packagePrefix; - } - - private String getSubDirPackagePrefix(JspCompilationInfo info) { - String dirPrefix = info.getJspPath(); - if (dirPrefix.startsWith("/")) { - dirPrefix = dirPrefix.substring(1); - } - int lastSlashIndex = dirPrefix.lastIndexOf("/"); - if (lastSlashIndex != -1) { - dirPrefix = dirPrefix.substring(0, lastSlashIndex); - dirPrefix = encodeSpecialCharacters(dirPrefix); - dirPrefix = dirPrefix.replace('/', '.') + "."; - } else { - dirPrefix = ""; - } - return dirPrefix; - } - - private void compileJavaToBytecode(JspCompilationInfo info) throws Exception { - File classFile = new File(info.getClassFile()); - classFile.delete(); - logCompilation(info.getJavaSource(), info.getClassOutputDir()); - boolean ok = javac() - .compile(info.getJavaSource(), info.getClassOutputDir(), getClassPath()); - assertTrue("javac failed", ok); - assertTrue("Failed to compile .java file to " + classFile.getAbsolutePath(), classFile - .exists()); - } - - private String[] getClassPath() { - Path cp = new Path(); - cp.addSystemProperty("java.class.path"); - cp.addContainer(javax.servlet.jsp.tagext.JspTag.class); - cp.addContainer(javax.servlet.jsp.jstl.core.LoopTag.class); - cp.addContainer(javax.servlet.http.HttpServlet.class); - cp.addContainer(org.apache.taglibs.standard.Version.class); - cp.addContainer(org.apache.jasper.JspC.class); - cp.addContainer(org.apache.jasper.compiler.Compiler.class); - cp.addContainer(org.apache.jasper.runtime.HttpJspBase.class); - cp.add(new File("target", "test-classes").getAbsolutePath()); - cp.add(new File("target", "classes").getAbsolutePath()); - return cp.toStringArray(); - } - - private void resolveClassFileLocation(JspCompilationInfo info) { - String file = Strings.replace(info.getJavaSource(), ".java", ".class"); - info.setClassFile(file); - } - - private static void assertTrue(String errorMessage, boolean condition) { - if (!condition) { - throw new RuntimeException(errorMessage); - } - } - - private static JavaCompiler determineJavaCompiler() { - List compilers = new ArrayList(); - // this doesn't work because with Maven we need to set the classpath - // explicitly as the "current" classpath does not include our - // dependencies - compilers.add(new Java6Compiler()); - compilers.add(new SunJavaC()); - compilers.add(new CommandLineJavac()); - for (Iterator i = compilers.iterator(); i.hasNext();) { - JavaCompiler compiler = (JavaCompiler) i.next(); - if (compiler.isAvailable()) { - log.debug("Using JavaCompiler: " + compiler.getClass().getName()); - return compiler; - } - } - throw new RuntimeException("No JavaCompiler implementation available on the system"); - } - - private static JavaCompiler javac() { - return COMPILER; - } - - private void logCompilation(String src, String dst) { - log.debug(" Compiling " + src + " to " + dst); - } -} Modified: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,68 +1,22 @@ package net.sf.jsptest.compiler.jsp21; -import java.io.File; import java.util.Map; - -import org.apache.jasper.servlet.JasperLoader; - -import net.sf.jsptest.compiler.JspCompilationInfo; import net.sf.jsptest.compiler.api.Jsp; import net.sf.jsptest.compiler.api.JspCompiler; -import net.sf.jsptest.utils.CustomClassLoader; /** * @author Lasse Koskela * @author Meinert Schwartau (scwar32) - * @auther Ronaldo Webb */ public class JspCompilerImpl implements JspCompiler { - private String outputDirectory = new File(System.getProperty("java.io.tmpdir"), - "jsptest-classes").getAbsolutePath(); - private String webRoot; + public Jsp compile(String path, Map taglibs) { + throw new RuntimeException("Not implemented"); + } public void setOutputDirectory(String directory) { - outputDirectory = directory; } - protected String getOutputDirectory() { - return outputDirectory; - } - public void setWebRoot(String directory) { - this.webRoot = directory; } - - protected String getWebRoot() { - return webRoot; - } - - protected JasperCompiler getCompiler() { - JasperCompiler compiler = new JasperCompiler(); - compiler.setWebRoot(getWebRoot()); - compiler.setClassOutputBaseDir(getOutputDirectory()); - - return compiler; - } - - public Jsp compile(final String jspPath, Map taglibs) { - try { - JasperCompiler compiler = getCompiler(); - JspCompilationInfo info = compiler.compile(jspPath, taglibs); - final Class servletClass = compileToClass(info); - return new JspImpl(servletClass); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - protected Class compileToClass(JspCompilationInfo compilation) throws Exception, - ClassNotFoundException { - return loadJspClass(compilation.getClassName()); - } - - protected Class loadJspClass(String jspClassName) throws ClassNotFoundException { - ClassLoader cl = new CustomClassLoader(getOutputDirectory()); - return cl.loadClass(jspClassName); - } } Deleted: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspExecutionImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspExecutionImpl.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspExecutionImpl.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,19 +0,0 @@ -package net.sf.jsptest.compiler.jsp21; - -import net.sf.jsptest.compiler.api.JspExecution; - -/** - * @author Lasse Koskela - */ -public class JspExecutionImpl implements JspExecution { - - private final String renderedOutput; - - public JspExecutionImpl(String output) { - this.renderedOutput = output; - } - - public String getRenderedResponse() { - return renderedOutput; - } -} Deleted: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspImpl.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspImpl.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,87 +0,0 @@ -package net.sf.jsptest.compiler.jsp21; - -import java.util.Map; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import javax.servlet.jsp.JspFactory; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import net.sf.jsptest.compiler.api.Jsp; -import net.sf.jsptest.compiler.api.JspExecution; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpServletRequest; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpServletResponse; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpSession; -import net.sf.jsptest.compiler.jsp21.mock.MockJspFactory; -import net.sf.jsptest.compiler.jsp21.mock.MockJspWriter; -import net.sf.jsptest.compiler.jsp21.mock.MockPageContext; -import net.sf.jsptest.compiler.jsp21.mock.MockServletConfig; -import net.sf.jsptest.compiler.jsp21.mock.MockServletContext; - -/** - * @author Lasse Koskela - */ -public class JspImpl implements Jsp { - - private static final Log log = LogFactory.getLog(JspImpl.class); - private final Class servletClass; - private MockPageContext pageContext; - - public JspImpl(Class servletClass) { - this.servletClass = servletClass; - } - - public JspExecution request(String httpMethod, Map requestAttributes, Map sessionAttributes) { - ServletContext servletContext = new MockServletContext(); - ServletConfig servletConfig = new MockServletConfig(servletContext); - MockHttpSession httpSession = new MockHttpSession(); - httpSession.setAttributes(sessionAttributes); - MockHttpServletRequest request = new MockHttpServletRequest(); - request.setSession(httpSession); - request.setMethod(httpMethod); - request.setAttributes(requestAttributes); - MockHttpServletResponse response = new MockHttpServletResponse(); - MockJspWriter jspWriter = configureJspFactory(servletContext, request, httpSession); - initializeAndInvokeJsp(servletClass, servletConfig, request, response); - return createExecutionResult(jspWriter.getContents()); - } - - protected MockJspWriter configureJspFactory(ServletContext httpContext, - HttpServletRequest httpRequest, HttpSession httpSession) { - pageContext = new MockPageContext(); - pageContext.setRequest(httpRequest); - pageContext.setServletContext(httpContext); - pageContext.setSession(httpSession); - MockJspWriter jspWriter = new MockJspWriter(); - pageContext.setJspWriter(jspWriter); - JspFactory.setDefaultFactory(new MockJspFactory(pageContext)); - return jspWriter; - } - - protected void initializeAndInvokeJsp(Class jspClass, ServletConfig servletConfig, - HttpServletRequest request, HttpServletResponse response) { - try { - log.debug("Instantiating Servlet: " + jspClass.getName()); - HttpServlet servlet = (HttpServlet) jspClass.newInstance(); - log.debug("Initializing Servlet: " + jspClass.getName()); - servlet.init(servletConfig); - log.debug("Invoking Servlet: " + jspClass.getName()); - servlet.service(request, response); - if (pageContext.getException() != null) { - log.debug("An exception was stored into PageContext. Rethrowing it..."); - throw new RuntimeException(pageContext.getException()); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - protected JspExecution createExecutionResult(String output) { - return new JspExecutionImpl(output); - } -} Deleted: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXCompilerImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXCompilerImpl.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXCompilerImpl.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,33 +0,0 @@ -package net.sf.jsptest.compiler.jsp21; - -import java.util.Map; - -import net.sf.jsptest.compiler.JspCompilationInfo; -import net.sf.jsptest.compiler.api.JspXCompiler; -import net.sf.jsptest.compiler.api.JspX; -import net.sf.jsptest.compiler.jsp21.JasperCompiler; -import net.sf.jsptest.compiler.jsp21.JspCompilerImpl; - -/** - * Add the capability to call the JspXImpl which has the capability to process request parameters. - * - * @author Ronaldo Webb - */ -public class JspXCompilerImpl extends JspCompilerImpl implements JspXCompiler { - - /** - * @see net.sf.jsptest.compiler.api.JspXCompiler#compileX(java.lang.String, java.util.Map) - */ - public JspX compileX(String jspPath, Map taglibs) { - try { - JasperCompiler compiler = getCompiler(); - JspCompilationInfo info = compiler.compile(jspPath, taglibs); - final Class servletClass = compileToClass(info); - return new JspXImpl(servletClass); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - -} Deleted: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXImpl.java 2012-04-06 18:46:12 UTC (rev 268) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspXImpl.java 2012-04-11 23:16:22 UTC (rev 269) @@ -1,76 +0,0 @@ -package net.sf.jsptest.compiler.jsp21; - -import java.util.Iterator; -import java.util.Map; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; - -import net.sf.jsptest.compiler.api.JspExecution; -import net.sf.jsptest.compiler.api.JspX; -import net.sf.jsptest.compiler.jsp21.JspImpl; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpServletRequest; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpServletResponse; -import net.sf.jsptest.compiler.jsp21.mock.MockHttpSession; -import net.sf.jsptest.compiler.jsp21.mock.MockJspWriter; -import net.sf.jsptest.compiler.jsp21.mock.MockServletConfig; -import net.sf.jsptest.compiler.jsp21.mock.MockServletContext; - -/** - * An extension of the JspImpl that can consider request parameters. - * - * @author Ronaldo Webb - * - */ -public class JspXImpl extends JspImpl implements JspX { - - private Class servletClass = null; - - public JspXImpl(Class servletClass) { - super(servletClass); - this.servletClass = servletClass; - } - - /** - * @see net.sf.jsptest.compiler.api.JspX#request(java.lang.String, java.util.Map, java.util.Map, java.util.Map) - */ - public JspExecution request(String httpMethod, Map requestAttributes, - Map sessionAttributes, Map requestParameters) { - ServletContext servletContext = new MockServletContext(); - ServletConfig servletConfig = new MockServletConfig(servletContext); - MockHttpSession httpSession = new MockHttpSession(); - httpSession.setAttributes(sessionAttributes); - MockHttpServletRequest request = new MockHttpServletRequest(); - request.setSession(httpSession); - request.setMethod(httpMethod); - request.setAttributes(requestAttributes); - - if (null != requestParameters && !requestParameters.isEmpty()) { - Iterator keys = requestParameters.keySet().iterator(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - Object value = requestParameters.get(key); - - if (null != value) { - if (value instanceof String[]) { - request.addParameter(key, (String[]) value); - } else { - request.addParameter(key, - new String[] { (String) value }); - } - } - else { - request.addParameter(key, new String[] {null}); - } - } - } - - MockHttpServletResponse response = new MockHttpServletResponse(); - MockJspWriter jspWriter = configureJspFactory(servletContext, request, - httpSession); - initializeAndInvokeJsp(servletClass, servletConfig, request, response); - return createExecutionResult(jspWriter.getContents()); - } - -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |