[Jsptest-svn-commits] SF.net SVN: jsptest: [176] branches/multispecsupport.1.2/jsptest-jsp12
Status: Alpha
Brought to you by:
lkoskela
From: <gdi...@us...> - 2007-10-05 18:34:07
|
Revision: 176 http://jsptest.svn.sourceforge.net/jsptest/?rev=176&view=rev Author: gdinwiddie Date: 2007-10-05 11:34:06 -0700 (Fri, 05 Oct 2007) Log Message: ----------- initial work toward getting jsptest to work for JSP 1.2 spec. So far the strategy has been to duplicate things for JSP 2.0 and hack them until the tests pass. The thought is that the duplication can be removed afterwards. Modified Paths: -------------- branches/multispecsupport.1.2/jsptest-jsp12/pom.xml branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java Added Paths: ----------- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JasperCompiler.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspExecutionImpl.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspImpl.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/JspTestServletOutputStream.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockEmbeddedServletOptions.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletRequest.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletResponse.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpSession.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspFactory.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspWriter.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockPageContext.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockServletConfig.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockServletContext.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockServletInputStream.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockTagInfo.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockTldLocationsCache.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/utils/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/utils/StringUtils.java branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/apache/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/apache/jasper/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/apache/jasper/compiler/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/apache/jasper/servlet/ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/org/apache/jasper/servlet/JspServletWrapperWrapper.java branches/multispecsupport.1.2/jsptest-jsp12/src/test/java/net/ branches/multispecsupport.1.2/jsptest-jsp12/src/test/java/net/sf/ branches/multispecsupport.1.2/jsptest-jsp12/src/test/java/net/sf/jsptest/ branches/multispecsupport.1.2/jsptest-jsp12/src/test/java/net/sf/jsptest/utils/ branches/multispecsupport.1.2/jsptest-jsp12/src/test/java/net/sf/jsptest/utils/StringUtilsTest.java Modified: branches/multispecsupport.1.2/jsptest-jsp12/pom.xml =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/pom.xml 2007-10-05 18:33:38 UTC (rev 175) +++ branches/multispecsupport.1.2/jsptest-jsp12/pom.xml 2007-10-05 18:34:06 UTC (rev 176) @@ -37,5 +37,10 @@ <artifactId>jasper-runtime</artifactId> <version>4.1.30</version> </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.3</version> + </dependency> </dependencies> </project> Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JasperCompiler.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JasperCompiler.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JasperCompiler.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,432 @@ +/* + * 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.jsp12; + +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.JavaCompiler; +import net.sf.jsptest.compiler.jsp12.mock.MockEmbeddedServletOptions; +import net.sf.jsptest.compiler.jsp12.mock.MockServletConfig; +import net.sf.jsptest.compiler.jsp12.mock.MockTagInfo; +import net.sf.jsptest.utils.Path; +import net.sf.jsptest.utils.StringUtils; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jasper.EmbededServletOptions; +import org.apache.jasper.JasperException; +import org.apache.jasper.JspCompilationContext; +import org.apache.jasper.Options; +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; +import org.apache.jasper.servlet.JspServletWrapperWrapper; + +/** + * 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 + */ +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 mockTaglibs + * Mapping of tagnames to tag handler classes + * @param jspPath + * The path to the JSP source file to compile, given relative + * to the web root. + */ + 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, sConfig, baseUrl.toString()); + 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 Compiler(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()); + String packageName = getJspPackageName(); + String subPackageName = getSubDirPackagePrefix(info); + if (subPackageName.length() > 0) { + packageName = packageName+"."+subPackageName; + } + if (packageName.endsWith(".")) { + packageName = packageName.substring(0, packageName.length()-1); + } + cContext.setServletPackageName(packageName); + 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, ServletConfig servletConfig, String jspUri) + throws MalformedURLException, JasperException { + TagInfo tagInfo = new MockTagInfo(); + String tagFilePath = "/"; + URL tagFileJarUrl = new File(".").toURL(); + // FIXME + boolean isErrorPage = false; + JspServletWrapper wrapper = new JspServletWrapperWrapper( + servletConfig, options, jspUri, isErrorPage, jspRuntimeContext); + /* + servletContext, options, tagFilePath, tagInfo, + jspRuntimeContext, tagFileJarUrl); + */ + return wrapper; + } + + private Options createOptions(ServletContext ctx, + ServletConfig cfg, JspCompilationInfo info) { + Options options = new EmbededServletOptions(cfg, ctx); + return new MockEmbeddedServletOptions(options, ctx, info + .getTaglibs()); + } + + private void resolveJavaSourceFile(JspCompilationInfo info) { + File dir = new File(info.getClassOutputDir()); + if (getJspPackageName().length() > 0) { + dir = new File(dir, convertPackageToPath(getJspPackageName())); + } + File subdir = new File(dir.getAbsolutePath()); + String subPackage = getSubDirPackagePrefix(info); + if (subPackage.length() > 0) { + subdir = new File(subdir, convertPackageToPath(subPackage)); + } + subdir.mkdirs(); + String name = resolveJavaSourceFileName(info.getJspPath()); + info.setJavaSource(new File(dir, name).getPath()); + } + + private String convertPackageToPath(String packageName) { + return packageName.replace('.', '/'); + } + + private String resolveJavaSourceFileName(String jspPath) { + String name = encodeSpecialCharacters(jspPath); + name = replace(name, ".jsp", "_jsp.java"); + if (name.startsWith("/")) { + name = name.substring(1); + } + return name; + } + + private String replace(String string, String target, String replacement) { +// string = string.replace(target, replacement); + return StringUtils.replace(string, target, replacement); + } + + private String encodeSpecialCharacters(String name) { + return name; + } + + 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(".")); + info.setClassName(getPackagePrefix() + + getSubDirPackagePrefix(info) + 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); + } + if (dirPrefix.lastIndexOf("/") != -1) { + dirPrefix = dirPrefix.substring(0, dirPrefix + .lastIndexOf("/")); + dirPrefix = convertPathToPackage(dirPrefix) + "."; + } else { + dirPrefix = ""; + } + return dirPrefix; + } + + private String convertPathToPackage(String path) { + return path.replace('/', '.'); + } + + 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"); +//FIXME cp.addContainer(javax.servlet.jsp.tagext.JspTag.class); + cp.addContainer(javax.servlet.jsp.jstl.core.LoopTag.class); + cp.addContainer(javax.servlet.http.HttpServlet.class); +//FIXME 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")); + cp.add(new File("target", "classes").getAbsolutePath()); + return cp.toStringArray(); + } + + private void resolveClassFileLocation(JspCompilationInfo info) { + String file = StringUtils.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 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: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java 2007-10-05 18:33:38 UTC (rev 175) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java 2007-10-05 18:34:06 UTC (rev 176) @@ -1,19 +1,59 @@ package net.sf.jsptest.compiler.jsp12; +import java.io.File; import java.util.Map; +import net.sf.jsptest.compiler.JspCompilationInfo; import net.sf.jsptest.compiler.api.Jsp; import net.sf.jsptest.compiler.api.JspCompiler; +import net.sf.jsptest.compiler.jsp12.JasperCompiler; +import net.sf.jsptest.compiler.jsp12.JspImpl; +import net.sf.jsptest.utils.CustomClassLoader; 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; } - public void setOutputDirectory(String directory) { + protected String getOutputDirectory() { + return outputDirectory; } public void setWebRoot(String directory) { + this.webRoot = directory; } + + protected String getWebRoot() { + return webRoot; + } + + public Jsp compile(String jspPath, Map taglibs) { + try { + JasperCompiler compiler = new JasperCompiler(); + compiler.setWebRoot(getWebRoot()); + compiler.setClassOutputBaseDir(getOutputDirectory()); + JspCompilationInfo info = compiler.compile(jspPath, + taglibs); + final Class servletClass = compileToClass(info); + return new JspImpl(servletClass); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private Class compileToClass(JspCompilationInfo info) + throws Exception, ClassNotFoundException { + return loadJspClass(info.getClassName()); + } + + private Class loadJspClass(String jspClassName) + throws ClassNotFoundException { + ClassLoader cl = new CustomClassLoader(getOutputDirectory()); + return cl.loadClass(jspClassName); + } } Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspExecutionImpl.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspExecutionImpl.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspExecutionImpl.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,18 @@ +package net.sf.jsptest.compiler.jsp12; + +import java.io.File; + +import net.sf.jsptest.compiler.api.JspExecution; + +public class JspExecutionImpl implements JspExecution { + + private final File renderedOutput; + + public JspExecutionImpl(File renderedOutput) { + this.renderedOutput = renderedOutput; + } + + public File getRenderedResponse() { + return renderedOutput; + } +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspImpl.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspImpl.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspImpl.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,161 @@ +package net.sf.jsptest.compiler.jsp12; + +import java.io.File; +import java.io.IOException; +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.jsp12.mock.MockHttpServletRequest; +import net.sf.jsptest.compiler.jsp12.mock.MockHttpServletResponse; +import net.sf.jsptest.compiler.jsp12.mock.MockHttpSession; +import net.sf.jsptest.compiler.jsp12.mock.MockJspFactory; +import net.sf.jsptest.compiler.jsp12.mock.MockJspWriter; +import net.sf.jsptest.compiler.jsp12.mock.MockPageContext; +import net.sf.jsptest.compiler.jsp12.mock.MockServletConfig; +import net.sf.jsptest.compiler.jsp12.mock.MockServletContext; +import net.sf.jsptest.utils.IO; + +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 writeOutputToTempFile(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 writeOutputToTempFile(String output) { + try { + File temp = File.createTempFile("generated_html_", + ".html"); + temp.createNewFile(); + log.debug("Writing " + output.length() + " bytes to " + + temp.getAbsolutePath()); + IO.write(output, temp); + return new JspExecutionImpl(temp); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + // public JspExecution request(String httpMethod, + // Map requestAttributes, Map sessionAttributes) { + // try { + // final File responseFile = File.createTempFile( + // "jsptest-response", ".txt"); + // HttpServlet servlet = (HttpServlet) servletClass + // .newInstance(); + // servlet.init(new MockServletConfig( + // new MockServletContext())); + // + // ServletRequest request = createServletRequest(httpMethod, + // requestAttributes, sessionAttributes); + // ServletResponse response = createServletResponse(responseFile); + // + // servlet.service(request, response); + // + // response.flushBuffer(); + // + // return createJspExecution(responseFile); + // } catch (Exception e) { + // throw new RuntimeException(e); + // } + // } + // + // protected JspExecution createJspExecution(final File responseFile) { + // return new JspExecution() { + // public File getRenderedResponse() { + // return responseFile; + // } + // }; + // } + // + // protected ServletResponse createServletResponse(File output) + // throws FileNotFoundException { + // return new MockHttpServletResponse(output); + // } + // + // private ServletRequest createServletRequest(String httpMethod, + // Map requestAttributes, Map sessionAttributes) { + // MockHttpSession session = new MockHttpSession(); + // session.setAttributes(sessionAttributes); + // MockHttpServletRequest request = new MockHttpServletRequest(); + // request.setMethod(httpMethod); + // request.setAttributes(requestAttributes); + // request.setSession(session); + // return request; + // } +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/JspTestServletOutputStream.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/JspTestServletOutputStream.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/JspTestServletOutputStream.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,19 @@ +package net.sf.jsptest.compiler.jsp12.mock; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletOutputStream; + +public class JspTestServletOutputStream extends ServletOutputStream { + + private final OutputStream stream; + + public JspTestServletOutputStream(OutputStream stream) { + this.stream = stream; + } + + public void write(int b) throws IOException { + stream.write(b); + } +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockEmbeddedServletOptions.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockEmbeddedServletOptions.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockEmbeddedServletOptions.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,113 @@ +/* + * 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.jsp12.mock; + +import java.io.File; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.jasper.Options; +//import org.apache.jasper.compiler.JspConfig; +//import org.apache.jasper.compiler.MockTagPluginManager; +//import org.apache.jasper.compiler.TagPluginManager; +import org.apache.jasper.compiler.TldLocationsCache; + +/** + * @author Lasse Koskela + */ +public class MockEmbeddedServletOptions implements Options { + + private Options options; + + private ServletContext servletContext; + + private Map taglibs; + + public MockEmbeddedServletOptions(Options options, + ServletContext context, Map taglibs) { + this.options = options; + this.servletContext = context; + this.taglibs = taglibs; + } + + public int getCheckInterval() { + return options.getCheckInterval(); + } + + public boolean getClassDebugInfo() { + return options.getClassDebugInfo(); + } + + public String getClassPath() { + return options.getClassPath(); + } + + public String getCompiler() { + return options.getCompiler(); + } + + public boolean getDevelopment() { + return options.getDevelopment(); + } + + public boolean getFork() { + return options.getFork(); + } + + public String getIeClassId() { + return options.getIeClassId(); + } + + public String getJavaEncoding() { + return options.getJavaEncoding(); + } + + public boolean getKeepGenerated() { + return options.getKeepGenerated(); + } + + public boolean getLargeFile() { + return options.getLargeFile(); + } + + public boolean getMappedFile() { + return options.getMappedFile(); + } + + public boolean getReloading() { + return options.getReloading(); + } + + public File getScratchDir() { + return options.getScratchDir(); + } + + public boolean getSendErrorToClient() { + return options.getSendErrorToClient(); + } + + public TldLocationsCache getTldLocationsCache() { + TldLocationsCache realCache = options.getTldLocationsCache(); + return new MockTldLocationsCache(realCache, servletContext); + } + + public boolean isPoolingEnabled() { + return options.isPoolingEnabled(); + } + +} \ No newline at end of file Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletRequest.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletRequest.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletRequest.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,354 @@ +/* + * 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.jsp12.mock; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.security.Principal; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Vector; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +public class MockHttpServletRequest implements HttpServletRequest { + + private Map headers = new HashMap(); + + private Map parameters = new HashMap(); + + private Map attributes = new HashMap(); + + private MockHttpSession httpSession = null; + + private byte[] body; + + private String httpMethod; + + private String characterEncoding; + + public MockHttpServletRequest() { + this((MockHttpSession) null); + } + + public MockHttpServletRequest(MockHttpSession httpSession) { + setSession(httpSession); + } + + public MockHttpServletRequest(byte[] body) { + this.body = body; + characterEncoding = "UTF-8"; + this.headers.put("Content-type", + "multipart/form-data; boundary=XXXBOUNDARYXXX"); + } + + public String getAuthType() { + return null; + } + + public Cookie[] getCookies() { + return new Cookie[0]; + } + + public long getDateHeader(String s) { + try { + // TODO: use the HTTP date format pattern + return new SimpleDateFormat().parse( + (String) headers.get(s)).getTime(); + } catch (ParseException e) { + return 0; + } + } + + public String getHeader(String reference) { + return (String) headers.get(reference); + } + + public Enumeration getHeaders(String s) { + return new Vector().elements(); + } + + public Enumeration getHeaderNames() { + return new Vector(headers.keySet()).elements(); + } + + public int getIntHeader(String s) { + return Integer.parseInt(getHeader(s)); + } + + public void setMethod(String httpMethod) { + this.httpMethod = httpMethod; + } + + public String getMethod() { + return httpMethod; + } + + public String getPathInfo() { + return "/"; + } + + public String getPathTranslated() { + return "/"; + } + + public String getContextPath() { + return "/"; + } + + public String getQueryString() { + return null; + } + + public String getRemoteUser() { + return null; + } + + public boolean isUserInRole(String s) { + return false; + } + + public Principal getUserPrincipal() { + return null; + } + + public String getRequestedSessionId() { + return "JSPTESTSESSIONID"; + } + + public String getRequestURI() { + return "/"; + } + + public StringBuffer getRequestURL() { + return new StringBuffer("/"); + } + + public String getServletPath() { + return "/"; + } + + public HttpSession getSession(boolean flag) { + if (flag && httpSession == null) { + httpSession = new MockHttpSession(); + } + return httpSession; + } + + public HttpSession getSession() { + return httpSession; + } + + public boolean isRequestedSessionIdValid() { + return true; + } + + public boolean isRequestedSessionIdFromCookie() { + return true; + } + + public boolean isRequestedSessionIdFromURL() { + return false; + } + + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + public Object getAttribute(String name) { + return attributes.get(name); + } + + public Enumeration getAttributeNames() { + return new Vector(attributes.keySet()).elements(); + } + + public String getCharacterEncoding() { + return characterEncoding; + } + + public void setCharacterEncoding(String env) { + characterEncoding = env; + } + + public int getContentLength() { + return body.length; + } + + public String getContentType() { + return "multipart/form-data; boundary=xxx"; + } + + public ServletInputStream getInputStream() throws IOException { + return new MockServletInputStream(body); + } + + public String getParameter(String name) { + String values[] = (String[]) parameters.get(name); + if (values != null) { + return (values[0]); + } else { + return (null); + } + } + + public Map getParameterMap() { + return parameters; + } + + public String getProtocol() { + return "http"; + } + + public String getScheme() { + return "http://"; + } + + public String getServerName() { + return "servername"; + } + + public int getServerPort() { + return 8080; + } + + public BufferedReader getReader() throws IOException { + return null; + } + + public String getRemoteAddr() { + return null; + } + + public String getRemoteHost() { + return null; + } + + public void setAttributes(Map attributes) { + this.attributes.putAll(attributes); + } + + public void setAttribute(String name, Object o) { + attributes.put(name, o); + } + + public void removeAttribute(String name) { + attributes.remove(name); + } + + public Locale getLocale() { + return Locale.getDefault(); + } + + public Enumeration getLocales() { + return new Vector(Arrays.asList(Locale.getAvailableLocales())) + .elements(); + } + + public boolean isSecure() { + return false; + } + + public RequestDispatcher getRequestDispatcher(String s) { + return new RequestDispatcher() { + + public void forward(ServletRequest request, + ServletResponse response) + throws ServletException, IOException { + } + + public void include(ServletRequest request, + ServletResponse response) + throws ServletException, IOException { + } + }; + } + + public String getRealPath(String s) { + File tmpDir = new File(System.getProperty("java.io.tmpdir")); + File jsptestTmpDir = new File(tmpDir, "jsptest"); + return new File(jsptestTmpDir, s).getAbsolutePath(); + } + + public int getRemotePort() { + return 11111; + } + + public String getLocalName() { + return null; + } + + public String getLocalAddr() { + return null; + } + + public int getLocalPort() { + return 22222; + } + + public void addParameter(String name, String values[]) { + parameters.put(name, values); + } + + public String toString() { + StringBuffer buffer = new StringBuffer(); + buffer.append(getClass().getName()).append("{"); + Enumeration enumeration = getParameterNames(); + while (enumeration.hasMoreElements()) { + String key = (String) enumeration.nextElement(); + String[] values = getParameterValues(key); + buffer.append("parameter:"); + buffer.append(key); + buffer.append("=["); + for (int i = 0; i < values.length; i++) { + String value = values[i]; + buffer.append(value); + if (i < values.length - 1) { + buffer.append(","); + } + } + buffer.append("]\n"); + } + buffer.append("}"); + return buffer.toString(); + } + + public Enumeration getParameterNames() { + return new Vector(parameters.keySet()).elements(); + } + + public String[] getParameterValues(String name) { + return (String[]) parameters.get(name); + } + + public void setSession(MockHttpSession session) { + this.httpSession = session; + } +} \ No newline at end of file Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletResponse.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletResponse.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpServletResponse.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,178 @@ +package net.sf.jsptest.compiler.jsp12.mock; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.Locale; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +public class MockHttpServletResponse implements HttpServletResponse { + + private ServletOutputStream servletOutputStream; + + private String description; + + private PrintWriter writer; + + public MockHttpServletResponse() { + this(new ByteArrayOutputStream()); + description = "(in-memory)"; + } + + public MockHttpServletResponse(File output) + throws FileNotFoundException { + this(new FileOutputStream(output)); + description = "(output file: " + output.getAbsolutePath() + + ")"; + } + + private MockHttpServletResponse(final OutputStream output) { + this.servletOutputStream = new JspTestServletOutputStream( + output); + writer = new PrintWriter(servletOutputStream) { + }; + } + + public String toString() { + return super.toString() + " " + description; + } + + public void addCookie(Cookie cookie) { + } + + public void addDateHeader(String s, long l) { + } + + public void addHeader(String s, String s1) { + } + + public void addIntHeader(String s, int i) { + } + + public boolean containsHeader(String s) { + return false; + } + + public String encodeRedirectURL(String s) { + return null; + } + + /** @deprecated */ + public String encodeRedirectUrl(String s) { + return null; + } + + public String encodeURL(String s) { + return s; + } + + /** @deprecated */ + public String encodeUrl(String s) { + return s; + } + + public void flushBuffer() throws IOException { + getOutputStream().flush(); + } + + public int getBufferSize() { + return 0; + } + + public String getCharacterEncoding() { + return null; + } + + public Locale getLocale() { + return null; + } + + public ServletOutputStream getOutputStream() throws IOException { + return servletOutputStream; + } + + public PrintWriter getWriter() throws IOException { + return writer; + } + + public boolean isCommitted() { + return false; + } + + public void reset() { + } + + public void resetBuffer() { + } + + public void sendError(int i) throws IOException { + throw new RuntimeException("Not implemented"); + } + + public void sendError(int i, String s) throws IOException { + throw new RuntimeException("Not implemented"); + } + + public void sendRedirect(String s) throws IOException { + throw new RuntimeException("Not implemented"); + } + + public void setBufferSize(int i) { + } + + public void setContentLength(int i) { + } + + public void setContentType(String s) { + } + + public void setDateHeader(String s, long l) { + } + + public void setHeader(String s, String s1) { + } + + public void setIntHeader(String s, int i) { + } + + public void setLocale(Locale locale) { + } + + /** @deprecated */ + public void setStatus(int i) { + } + + public void setStatus(int i, String s) { + } + + public String getContentType() { + return null; + } + + public void setCharacterEncoding(String reference) { + } +} \ No newline at end of file Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpSession.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpSession.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockHttpSession.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,106 @@ +/* + * 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.jsp12.mock; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionContext; + +/** + * @author Lasse Koskela + */ +public class MockHttpSession implements HttpSession { + + private Map attributes = new HashMap(); + + public Object getAttribute(String s) { + return attributes.get(s); + } + + public Enumeration getAttributeNames() { + return new Vector(attributes.keySet()).elements(); + } + + public long getCreationTime() { + return 0; + } + + public String getId() { + return "JSPTESTSESSIONID"; + } + + public long getLastAccessedTime() { + return 0; + } + + public int getMaxInactiveInterval() { + return 0; + } + + public HttpSessionContext getSessionContext() { + return null; + } + + public Object getValue(String s) { + return null; + } + + public String[] getValueNames() { + return new String[0]; + } + + public void invalidate() { + } + + public boolean isNew() { + return false; + } + + public void putValue(String s, Object o) { + } + + public void removeAttribute(String s) { + attributes.remove(s); + } + + public void removeValue(String s) { + } + + public void setAttribute(String s, Object o) { + attributes.put(s, o); + } + + public void setMaxInactiveInterval(int i) { + } + + public ServletContext getServletContext() { + return null; + } + + /** + * For the purposes of jsptest. + */ + public void setAttributes(Map attributes) { + this.attributes.putAll(attributes); + } + +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspFactory.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspFactory.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspFactory.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,54 @@ +/* + * 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.jsp12.mock; + +import javax.servlet.Servlet; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.jsp.JspEngineInfo; +import javax.servlet.jsp.JspFactory; +import javax.servlet.jsp.PageContext; + +/** + * @author Lasse Koskela + */ +public class MockJspFactory extends JspFactory { + + private PageContext pageContext; + + public MockJspFactory(PageContext pageContext) { + this.pageContext = pageContext; + } + + public PageContext getPageContext(Servlet servlet, ServletRequest request, + ServletResponse response, String errorPageURL, + boolean needsSession, int buffer, boolean autoflush) { + return pageContext; + } + + public void releasePageContext(PageContext pageContext) { + } + + public JspEngineInfo getEngineInfo() { + return new JspEngineInfo() { + + public String getSpecificationVersion() { + return "2.0"; + } + }; + } +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspWriter.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspWriter.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockJspWriter.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,159 @@ +/* + * 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.jsp12.mock; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.servlet.jsp.JspWriter; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * @author Lasse Koskela + */ +public class MockJspWriter extends JspWriter { + + private Log log; + + private PrintWriter writer; + + private StringWriter stringWriter; + + public String getContents() { + return stringWriter.toString(); + } + + public MockJspWriter() { + super(1024, true); + log = LogFactory.getLog(getClass()); + stringWriter = new StringWriter(); + writer = new PrintWriter(stringWriter, true); + } + + public void newLine() throws IOException { + writer.println(); + } + + public void print(boolean x) throws IOException { + writer.print(x); + } + + public void print(char x) throws IOException { + writer.print(x); + } + + public void print(int x) throws IOException { + writer.print(x); + } + + public void print(long x) throws IOException { + writer.print(x); + } + + public void print(float x) throws IOException { + writer.print(x); + } + + public void print(double x) throws IOException { + writer.print(x); + } + + public void print(char[] x) throws IOException { + writer.print(x); + } + + public void print(String x) throws IOException { + writer.print(x); + } + + public void print(Object x) throws IOException { + writer.print(x); + } + + public void println() throws IOException { + writer.println(); + } + + public void println(boolean x) throws IOException { + writer.println(x); + } + + public void println(char x) throws IOException { + writer.println(x); + } + + public void println(int x) throws IOException { + writer.println(x); + } + + public void println(long x) throws IOException { + writer.println(x); + } + + public void println(float x) throws IOException { + writer.println(x); + } + + public void println(double x) throws IOException { + writer.println(x); + } + + public void println(char[] x) throws IOException { + writer.println(x); + } + + public void println(String x) throws IOException { + writer.println(x); + } + + public void println(Object x) throws IOException { + writer.println(x); + } + + public void clear() throws IOException { + notImplemented("clear()"); + } + + + public void clearBuffer() throws IOException { + notImplemented("clearBuffer()"); + } + + public void flush() throws IOException { + writer.flush(); + } + + public void close() throws IOException { + writer.close(); + } + + public int getRemaining() { + notImplemented("getRemaining()"); + return 0; + } + + public void write(char[] x, int start, int length) throws IOException { + writer.write(x, start, length); + } + + private void notImplemented(String methodSignature) { + log.error(getClass().getName() + "#" + methodSignature + " not implemented"); + } +} Added: branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockPageContext.java =================================================================== --- branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockPageContext.java (rev 0) +++ branches/multispecsupport.1.2/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/mock/MockPageContext.java 2007-10-05 18:34:06 UTC (rev 176) @@ -0,0 +1,286 @@ +package net.sf.jsptest.compiler.jsp12.mock; + +import java.io.IOException; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +import javax.servlet.Servlet; +import javax.servlet... [truncated message content] |