技术开发 频道

容器外的JSP页面测试技术

【IT168技术文档】由于上面给出例 复杂 。一般人很难理解。我也是 ^_^
但仔细看我还是自己写出一个比较简单的,望大家一起讨论。
被测试jsp
<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld" %> <%@ taglib prefix="c" uri="/WEB-INF/c-1_0-rt.tld" %> <html:html> <c:if test="${name != pass}"> ${name} <br> ${pass} <br> <!-- <html:text property="in" ></html:text> --> </c:if> </html:html>
ant直接把他放在 eclipes 工程的根目录下 build.xml
但有有3个参数要设置 tomcat.home Tomcat 的地址
webapp.path 工程中的根目录,下面有WEB-INF
src 原代码 (到时候jsp会翻译成.java
到这个目录的 org.apache.jsp.jsp 下)
<project name="Webapp Precompilation" default="all" basedir="."> <!-- tomcat dir --> <property name="tomcat.home" value="D:\Tomcat 5.0"/> <!-- this=..//WEB-INF (in eclipes) --> <property name="webapp.path" value=".\WebRoot"/> <!-- src (in eclipes) --> <property name="src" value="./src"/> <target name="jspc"> <taskdef classname="org.apache.jasper.JspC" name="jasper2" > <classpath id="jspc.classpath"> <pathelement location="${java.home}/../lib/tools.jar"/> <fileset dir="${tomcat.home}/bin"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/server/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar"/> </fileset> </classpath> </taskdef> <jasper2 validateXml="false" uriroot="${webapp.path}" webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml" outputDir="${src}" /> </target> <target name="compile"> <mkdir dir="${webapp.path}/WEB-INF/classes"/> <mkdir dir="${webapp.path}/WEB-INF/lib"/> <javac destdir="${webapp.path}/WEB-INF/classes" optimize="off" debug="on" failonerror="false" srcdir="${src}" excludes="**/*.smap"> <classpath> <pathelement location="${webapp.path}/WEB-INF/classes"/> <fileset dir="${webapp.path}/WEB-INF/lib"> <include name="*.jar"/> </fileset> <pathelement location="${tomcat.home}/common/classes"/> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar"/> </fileset> <pathelement location="${tomcat.home}/shared/classes"/> <fileset dir="${tomcat.home}/shared/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/bin"> <include name="*.jar"/> </fileset> </classpath> <include name="**" /> <exclude name="tags/**" /> </javac> </target> <target name="all" depends="jspc,compile"> </target> <target name="cleanup"> <delete> <fileset dir="${webapp.path}/WEB-INF/src"/> <fileset dir="${webapp.path}/WEB-INF/classes/org/apache/jsp"/> </delete> </target> </project>
TEST
package jetty.test.supper; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.jsp.jsp.MyJsp_jsp; import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; import com.meterware.servletunit.InvocationContext; import com.meterware.servletunit.ServletRunner; import com.meterware.servletunit.ServletUnitClient; import junit.framework.TestCase; public class JSPCTest extends TestCase{ private InvocationContext ic = null ; protected void setUp() throws Exception { ServletRunner sr = new ServletRunner(); // 向环境中注册 jsp sr.registerServlet("HelloWorld", MyJsp_jsp.class.getName()); ServletUnitClient sc = sr.newClient(); WebRequest request = new GetMethodWebRequest("http://localhost/HelloWorld"); ic = sc.newInvocation(request); } public void testJspC() throws Exception{ HttpServletRequest re = ic .getRequest(); HttpServletResponse rq = ic.getResponse(); re.setAttribute("name","liukaiyi"); re.setAttribute("pass","123456"); MyJsp_jsp is = (MyJsp_jsp) ic.getServlet(); is._jspService(re,rq); WebResponse response = ic.getServletResponse(); // 输出 System.out.println( response.getText() ); } }
结果是
<html> liukaiyi <br> 123456 <br> </html>

后面用 HttpUnit 和 HtmlUnit 测试就不用我说了吧

原文地址

0
相关文章