銆銆Ant鏄疛ava騫沖彴涓嬮潪甯告鐨勬壒澶勭悊鍛戒護鎵ц紼嬪簭錛岃兘闈炲父鏂逛究鍦拌嚜鍔ㄥ畬鎴愮紪璇戯紝嫻嬭瘯錛屾墦鍖咃紝閮ㄧ講絳夌瓑涓緋誨垪浠誨姟錛屽ぇ澶ф彁楂樺紑鍙戞晥鐜囥傚鏋滀綘鐜板湪榪樻病鏈夊紑濮嬩嬌鐢ˋnt錛岄偅灝辮璧跺揩寮濮嬪涔犱嬌鐢紝浣胯嚜宸辯殑寮鍙戞按騫充笂涓涓柊鍙伴樁銆?/p>
銆銆Eclipse 涓凡緇忛泦鎴愪簡Ant錛屾垜浠彲浠ョ洿鎺ュ湪Eclipse涓繍琛孉nt銆?/p>
銆銆浠ュ墠闈㈠緩绔嬬殑Hello宸ョ▼涓轟緥錛屽垱寤轟互涓嬬洰褰曠粨鏋勶細
銆銆鏂板緩涓涓猙uild.xml錛屾斁鍦ㄥ伐紼嬫牴鐩綍涓嬨俠uild.xml瀹氫箟浜咥nt瑕佹墽琛岀殑鎵瑰鐞嗗懡浠ゃ傝櫧鐒禔nt涔熷彲浠ヤ嬌鐢ㄥ叾瀹冩枃浠跺悕錛屼絾鏄伒寰爣鍑嗚兘鏇翠嬌寮鍙戞洿瑙勮寖錛屽悓鏃舵槗浜庝笌鍒漢浜ゆ祦銆?/p>
銆銆閫氬父錛宻rc瀛樻斁Java婧愭枃浠訛紝classes瀛樻斁緙栬瘧鍚庣殑class鏂囦歡錛宭ib瀛樻斁緙栬瘧鍜岃繍琛岀敤鍒扮殑鎵鏈塲ar鏂囦歡錛寃eb瀛樻斁JSP絳墂eb鏂囦歡錛宒ist瀛樻斁鎵撳寘鍚庣殑jar鏂囦歡錛宒oc瀛樻斁API鏂囨。銆?/p>
銆銆鐒跺悗鍦ㄦ牴鐩綍涓嬪垱寤篵uild.xml鏂囦歡錛岃緭鍏ヤ互涓嬪唴瀹癸細
<xml version="1.0"?>
<project name="Hello world" default="doc">
<-- properies -->
<property name="src.dir" value="src" />
<property name="report.dir" value="report" />
<property name="classes.dir" value="classes" />
<property name="lib.dir" value="lib" />
<property name="dist.dir" value="dist" />
<property name="doc.dir" value="doc"/>
<-- 瀹氫箟classpath -->
<path id="master-classpath">
<fileset file="${lib.dir}/*.jar" />
<pathelement path="${classes.dir}"/>
<path>
<-- 鍒濆鍖栦換鍔?-->
<target name="init">
<target>
<-- 緙栬瘧 -->
<target name="compile" depends="init" description="compile the source files">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
<classpath refid="master-classpath"/>
<javac>
<target>
<-- 嫻嬭瘯 -->
<target name="test" depends="compile" description="run junit test">
<mkdir dir="${report.dir}"/>
<junit printsummary="on"
haltonfailure="false"
failureproperty="tests.failed"
showoutput="true">
<classpath refid="master-classpath" />
<ormatter type="plain"/>
<batchtest todir="${report.dir}">
<fileset dir="${classes.dir}">
<include name="**/*Test.*"/>
<fileset>
<batchtest>
<junit>
<fail if="tests.failed">
***********************************************************
**** One or more tests failed! Check the output ... ****
***********************************************************
<fail/>
<target/>
<-- 鎵撳寘鎴恓ar -->
<target name="pack" depends="test" description="make .jar file">
<mkdir dir="${dist.dir}" />
<jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
<xclude name="**/*Test.*" />
<xclude name="**/Test*.*" />
<jar/>
<targe/t>
<-- 杈撳嚭api鏂囨。 -->
<target name="doc" depends="pack" description="create api doc">
<mkdir dir="${doc.dir}" />
<javadoc destdir="${doc.dir}"
author="true"
version="true"
use="true"
windowtitle="Test API">
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="example/**" />
<packageset/>
<doctitle>[CDATA[<1>ello, test<h1>]>doctitle>
<dottom>[CDATA[<>ll Rights Reserved.<i>]>bottom>
<tag name="todo" scope="all" description="To do:" />
<javadoc/>
<target/>
<project/>
銆銆浠ヤ笂xml渚濇瀹氫箟浜唅nit錛堝垵濮嬪寲錛夛紝compile錛堢紪璇戯級錛宼est錛堟祴璇曪級錛宒oc錛堢敓鎴愭枃妗o級錛宲ack錛堟墦鍖咃級浠誨姟錛屽彲浠ヤ綔涓烘ā鏉褲?/p>
銆銆閫変腑Hello宸ョ▼錛岀劧鍚庨夋嫨鈥淧roject鈥濓紝鈥淧roperties鈥濓紝鈥淏uilders鈥濓紝鈥淣ew鈥︹濓紝閫夋嫨鈥淎nt Build鈥濓細
銆銆濉叆Name錛欰nt_Builder錛汢uildfile錛歜uild.xml錛汢ase Directory錛?{workspace_loc:/Hello}錛堟寜鈥淏rowse Workspace鈥濋夋嫨宸ョ▼鏍圭洰褰曪級錛岀敱浜庣敤鍒頒簡junit.jar鍖咃紝鎼滅儲Eclipse鐩綍錛屾壘鍒癹unit.jar錛屾妸瀹冨鍒跺埌Hello/lib鐩綍涓嬶紝騫舵坊鍔犲埌Ant鐨凜lasspath涓細
銆銆鐒跺悗鍦˙uilder闈㈡澘涓挬涓夾nt_Build錛屽幓鎺塉ava Builder錛?/p>
銆銆鍐嶆緙栬瘧錛屽嵆鍙湪鎺у埗鍙扮湅鍒癆nt鐨勮緭鍑猴細
Buildfile: F:\eclipse-projects\Hello\build.xml
init:
compile:
[mkdir] Created dir: F:\eclipse-projects\Hello\classes
[javac] Compiling 2 source files to F:\eclipse-projects\Hello\classes
test:
[mkdir] Created dir: F:\eclipse-projects\Hello\report
[junit] Running example.HelloTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec
pack:
[mkdir] Created dir: F:\eclipse-projects\Hello\dist
[jar] Building jar: F:\eclipse-projects\Hello\dist\hello.jar
doc:
[mkdir] Created dir: F:\eclipse-projects\Hello\doc
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package example...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.4.2_04
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
[javadoc] Generating F:\eclipse-projects\Hello\doc\stylesheet.css...
[javadoc] Note: Custom tags that could override future standard tags: @todo. To avoid potential overrides, use at least one period character (.) in custom tag names.
[javadoc] Note: Custom tags that were not seen: @todo
BUILD SUCCESSFUL
Total time: 11 seconds
銆銆Ant渚濇鎵ц鍒濆鍖栵紝緙栬瘧錛屾祴璇曪紝鎵撳寘錛岀敓鎴怉PI鏂囨。涓緋誨垪浠誨姟錛屾瀬澶у湴鎻愰珮浜嗗紑鍙戞晥鐜囥傚皢鏉ュ紑鍙慗2EE欏圭洰鏃訛紝榪樺彲鍔犲叆閮ㄧ講絳変換鍔°傚茍涓旓紝鍗充嬌鑴辯浜咵clipse鐜錛屽彧瑕佹紜畨瑁呬簡Ant錛岄厤緗ソ鐜鍙橀噺ANT_HOME=<nt瑙e帇鐩綍>Path=鈥?%ANT_HOME%\bin錛屽湪鍛戒護琛屾彁紺虹涓嬪垏鎹㈠埌Hello鐩綍錛岀畝鍗曞湴閿叆ant鍗沖彲銆?/p>
cactus.sysproperties=cactus.contextURL
#cactus-sample-servlet-cactified灝辨槸浣犵殑嫻嬭瘯搴旂敤鎵鍦ㄨ礬寰勶紝8080鏄鍙e彿
cactus.contextURL = http://localhost:8080/cactus-sample-servlet-cactified
cactus.servletRedirectorName = ServletRedirector
cactus.jspRedirectorName = JspRedirector
cactus.filterRedirectorName = FilterRedirector
<path id="project.classpath">
聽聽聽聽聽聽聽聽<fileset dir="${lib.dir}">
聽聽聽聽聽聽聽聽聽聽 <include name="*.jar"/>
聽聽聽聽聽聽聽聽</fileset>
聽聽聽聽聽聽聽聽<!-- cactus.properties鏂囦歡灝遍渶瑕佹斁鍦╨ib.dir鎵瀵瑰簲鐨勮礬寰勪腑 -->
聽聽聽聽聽聽聽聽<pathelement location="${lib.dir}"/>
聽聽聽聽聽聽聽聽<pathelement location="${tomcat.home}/common/lib/jsp-api.jar"/>
聽聽聽聽聽聽聽聽<pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>
聽聽聽聽</path>
<taskdef resource="cactus.tasks" classpathref="project.classpath"/>
聽聽 <taskdef name="runservertests" classname="org.apache.cactus.integration.ant.RunServerTestsTask">
聽聽聽聽聽聽聽聽聽聽聽聽<classpath>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<path refid="project.classpath"/>
聽聽聽聽聽聽聽聽聽聽聽聽</classpath>
聽聽聽聽聽聽聽聽</taskdef>
<target name="war" depends="compile.java"
聽聽聽聽聽聽聽聽聽聽聽聽description="Generate the runtime war">
聽聽聽聽聽聽聽聽<war warfile="${target.dir}/${project.name}.war"
聽聽聽聽聽聽聽聽聽聽聽聽 webxml="${src.webapp.dir}/WEB-INF/web.xml">
聽聽聽聽聽聽聽聽聽聽聽聽<fileset dir="${src.webapp.dir}">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<exclude name="cactus-report.xsl"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<exclude name="WEB-INF/cactus-web.xml"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<exclude name="WEB-INF/web.xml"/>
聽聽聽聽聽聽聽聽聽聽聽聽</fileset>
聽聽聽聽聽聽聽聽聽聽聽聽<classes dir="${target.classes.java.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽<!-- 鍒繕浜嗘墦鍖呮祴璇曠被 -->
聽聽聽聽聽聽聽聽聽聽聽聽<classes dir="${target.classes.test.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽<!-- 鍒繕浜嗘墦鍖呭悇縐嶇浉鍏崇殑jar鏂囦歡 -->
聽聽聽聽聽聽聽聽聽聽聽聽< lib dir="project.classpath"/>
聽聽聽聽聽聽聽聽</war>
聽聽聽聽</target>
<target name="test.prepare"
聽聽聽聽聽聽聽聽聽聽聽聽depends="war, compile.cactus, test.prepare.logging">
聽聽聽聽聽聽聽聽<!-- Cactify the web-app archive -->
聽聽聽聽聽聽聽聽<cactifywar srcfile="${target.dir}/${project.name}.war"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽destfile="${tomcat.home}/webapps/${project.name}-cactified.war"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽>
聽聽聽聽聽聽聽聽聽聽聽聽<classes dir="${target.classes.java.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽<classes dir="${target.classes.test.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽<lib dir="project.classpath"/>
聽聽聽聽聽聽 </cactifywar>
</target>
<target name="test" depends="test.prepare"
聽聽聽聽聽聽聽聽聽聽聽聽 description="Run tests on Tomcat ">
聽聽聽聽聽聽聽聽<!-- Start the servlet engine, wait for it to be started, run the
聽聽聽聽聽聽聽聽聽聽聽聽 unit tests, stop the servlet engine, wait for it to be stopped.
聽聽聽聽聽聽聽聽聽聽聽聽 The servlet engine is stopped if the tests fail for any reason -->
聽聽聽聽聽聽聽聽<!-- 8080鏄湇鍔″櫒鐨勭鍙e彿錛?{project.name}-cactified鏄」鐩殑璺緞錛屽拰涓婁竴姝ョ殑cactifywar 鐨刣estfile鐩稿搴?-->
聽聽聽聽聽聽聽聽<runservertests
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽testURL="http://localhost:8080/${project.name}-cactified/ServletRedirector?Cactus_Service=RUN_TEST"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽startTarget="_StartTomcat"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽stopTarget="_StopTomcat"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽testTarget="_Test"/>
聽聽聽聽</target>
<!-- _Test灝辨槸涓涓櫘閫氱殑junit浠誨姟 -->
聽聽聽聽<target name="_Test">
聽聽聽聽聽聽聽聽<junit printsummary="yes" fork="yes">
聽聽聽聽聽聽聽聽聽聽聽聽<classpath>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<path refid="project.classpath"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<pathelement location="${target.classes.java.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<pathelement location="${target.classes.test.dir}"/>
聽聽聽聽聽聽聽聽聽聽聽聽</classpath>
聽聽聽聽聽聽聽聽聽聽聽聽<formatter type="brief" usefile="false"/>
聽聽聽聽聽聽聽聽聽聽聽聽<formatter type="xml"/>
聽聽聽聽聽聽聽聽聽聽聽聽<batchtest>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<fileset dir="${src.test.dir}">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<!-- Due to some Cactus synchronization bug, the 'unit' tests need
聽聽聽聽聽聽聽聽聽聽聽聽聽聽to run before the 'sample' tests -->
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<include name="**/Test*.java"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<exclude name="**/Test*All.java"/>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</fileset>
聽聽聽聽聽聽聽聽聽聽聽聽</batchtest>
聽聽聽聽聽聽聽聽</junit>
聽聽聽聽</target>
Cactus綆浠?/p>
錛庛綆浠?
Cactus瀹炵幇浜嗗JUnit嫻嬭瘯妗嗘灦鐨勬棤緙濇墿灞曪紝鍙互鏂逛究鍦版祴璇曟湇鍔$搴旂敤紼嬪簭銆侰actus鍙互鍦ㄤ笅闈㈠嚑縐嶆儏鍐典笅浣跨敤錛?/p>
Cactus鐨勪嬌鐢ㄤ篃鏄潪甯哥畝鍗曠殑錛屼綘鍐欑殑嫻嬭瘯綾誨彧闇緇ф壙ServletTestCase鎴栬匤spTestCase銆丗ilterTestCase錛堝畠浠兘緇ф壙浜咼Unit鐨凾estCase錛夈傚啓濂芥祴璇曚唬鐮佸悗闇瑕佸惎鍔╳eb瀹瑰櫒錛岀劧鍚庢墽琛屾祴璇曚唬鐮併傚湪涓嬮潰鐨勭珷鑺備腑鎴戜滑灝嗛氳繃渚嬪瓙鍚戜綘璇︾粏璁茶В銆?/p>
Cactus欏圭洰Apache Jakarta Commons鐨勪竴涓瓙欏圭洰錛岀綉鍧鏄細http://jakarta.apache.org/commons/cactus/銆?/p>
錛庛TestCase妗嗘灦
鍦–actus涓嬶紝鎴戜滑鍐欑殑TestCase涓嶫Unit鏈夋墍涓嶅悓錛屽厛鐪嬩竴孌典唬鐮侊紝濡備笅錛?/p>銆銆銆銆銆銆銆public class TestSample extendsServletTestCase/JspTestCase/FilterTestCase {
銆銆銆銆銆銆銆public TestSample (String testName) {
銆銆銆銆銆銆銆super(testName);
銆銆銆銆銆銆銆}
銆銆銆銆銆銆銆public void setUp() {
銆銆銆銆銆銆銆}
銆銆銆銆銆銆銆public void tearDown() {
銆銆銆銆銆銆銆}
銆銆銆銆銆銆銆public void beginXXX(WebRequest theRequest) {
銆銆銆銆銆銆銆}
銆銆銆銆銆銆銆public void testXXX() {
銆銆銆銆銆銆銆}
銆銆銆銆銆銆銆public void endXXX(WebResponse theResponse) {
銆銆銆銆銆銆銆}
涓婇潰鏄竴涓狢actus嫻嬭瘯綾葷殑瀹屾暣浠g爜妗嗘灦錛屽叾涓殑extends閮ㄥ垎闇瑕佹寜浣犳墍嫻嬭瘯鐨勪笉鍚岀洰鏍囨潵緇ф壙涓嶅悓鐨勭被錛堢畝浠嬩腑鏈夋墍鎻忚堪錛夈?/p>
鍙﹀鎴戜滑娉ㄦ剰鍒頒袱涓柊鐨勬柟娉昩eginXXX鍜宔ndXXX鐨勶紝榪欎袱涓柟娉曞垎鍒細鍦╰estXXX鎵ц鍓嶅拰鎵ц鍚庢墽琛岋紝瀹冧滑鍜宻etUp銆乼earDown涓嶅悓鐨勬槸beginXXX鍜宔ndXXX浼氬湪鐩稿簲鐨則estXXX鍓嶆墽琛岋紝鑰宻etUp鍜宼earDown鍒欏湪姣忎釜testXXX鏂規硶鍓嶉兘浼氭墽琛屻傚彟澶朾eginXXX鍜宔ndXXX鏄鎴風浠g爜錛屾墍浠ュ湪榪欎袱涓柟娉曢噷鏄棤娉曚嬌鐢╮equest榪欐牱鐨勬湇鍔$瀵硅薄鐨勩?/p>
瀵逛簬endXXX鏂規硶闇瑕佸彟鍔犺鏄庣殑鏄紝鍦–actus v1.1鍓嶏紙鍖呮嫭v1.1錛夛紝瀹冪殑褰㈠紡鏄繖鏍風殑public void endXXX(HttpURLConnection theConnection)錛岃屽湪Cactus v1.2寮濮嬪畠鐨勫艦寮忔湁涓ょ鍙兘錛?/p>
鍙互鐪嬪埌鍖哄埆鍦ㄤ簬寮曠敤鐨勫寘涓嶅悓錛屼負浠涔堜細榪欐牱鐨勫憿錛熷洜涓哄湪v1.2寮濮婥actus闆嗘垚浜咹ttpUnit榪欎釜緇勪歡銆傚鏋滀綘鐔熸倝HttpUnit榪欎釜緇勪歡錛屾垜鎯沖簲璇ユ槑鐧戒負浠涔堣闆嗘垚HttpUnit銆備笅闈㈡垜浠潵鐪嬩竴孌典唬鐮佸紑姣旇緝涓涓嬩袱鑰呯殑鍖哄埆錛?/p>
public void endXXX(org.apache.cactus.WebResponse theResponse) {
String content = theResponse.getText();
assertEquals(content, "<html><body><h1>Hello world!</h1></body></html>");
}
public void endXXX(com.meterware.httpunit.WebResponse theResponse) {
WebTable table = theResponse.getTables()[0];
assertEquals("rows", 4, table.getRowCount());
assertEquals("columns", 3, table.getColumnCount());
assertEquals("links", 1, table.getTableCell(0, 2).getLinks().length);
}
褰撶劧錛屽湪瀹為檯搴旂敤涓綘闇瑕佹牴鎹笉鍚岀殑闇瑕佹潵閫夋嫨涓嶅悓鐨別ndXXX銆備袱涓猈ebResponse鐨勫樊鍒彲浠ュ弬瑙佷袱鑰呭悇鑷殑API Doc錛岃繖閲屽氨涓嶅啀澶氳浜嗐?/p>
濡備綍鍦–actus閲屽啓嫻嬭瘯
錛庛鍐欐祴璇曚唬鐮?/strong>
棣栧厛錛屾垜浠粰鍑鴻嫻嬬被鐨勪唬鐮侊紝鏄竴涓猄ervlet錛?/p>
public class SampleServlet extends HttpServlet {
public void doGet(HttpServletRequest theRequest,
HttpServletResponse theResponse) throws IOException {
PrintWriter pw = theResponse.getWriter();
theResponse.setContentType("text/html");
pw.print("<html><head/><body>");
pw.print("A GET request");
pw.print("</body></html>");
}
public String checkMethod(HttpServletRequest theRequest) {
return theRequest.getMethod();
}
}
Cactus涓殑嫻嬭瘯綾繪鏋跺凡緇忓湪涓婇潰緇欏嚭銆備笅闈㈡潵鐪嬩竴涓嬩緥瀛愶紝渚嬪瓙鏄粠涓瑿actus鑷甫鐨勫疄渚嬩腑鎶藉彇鐨勪竴閮ㄥ垎錛屽涓嬶細
public class TestSampleServlet extends ServletTestCase {
public void testReadServletOutputStream() throws IOException {
SampleServlet servlet = new SampleServlet();
servlet.doGet(request, response);
}
public void endReadServletOutputStream(WebResponse theResponse)
throws IOException {
String expected = "<html><head/><body>A GET request</body></html>";
String result = theResponse.getText();
assertEquals(expected, result);
}
public void beginPostMethod(WebRequest theRequest) {
theRequest.addParameter("param", "value", WebRequest.POST_METHOD);
}
public void testPostMethod() {
SampleServlet servlet = new SampleServlet();
assertEquals("POST", servlet.checkMethod(request));
assertEquals("value", request.getParameter("param"));
}
}
絎竴涓柟娉晅estReadServletOutputStream錛岃皟鐢╠oGet錛岀浉褰撲簬鍦ㄥ鎴風鎻愪氦璇鋒眰錛岀劧鍚庡湪Servlet澶勭悊鍚庝細浜х敓涓涓洖棣堬紝鎵浠ワ紝鍦╡ndReadServletOutputStream鏂規硶閲岋紝鎴戜滑閫氳繃璋冪敤response鐨勭浉搴旀柟娉曞垽鏂洖棣堟槸鍚︾鍚堥鏈熺粨鏋溿?/p>
絎簩涓柟娉晅estPostMethod錛屽湪榪欎箣鍓嶆湁涓涓猙eginPostMethod錛屽湪榪欎釜鏂規硶閲屾垜浠互POST鏂瑰紡寰request閲屽鍔犱竴涓〃鍗曟暟鎹畃aram錛屽間負鈥漹alue鈥濄備笅闈㈠湪testPostMethod鎴戜滑灝辮楠岃瘉琛ㄥ崟鏁版嵁鏄惁浠OST鏂瑰紡鎻愪氦鍒頒簡鏈嶅姟绔殑Servlet閲岋紝鎵浠ワ紝鎴戜滑鐪嬪埌浜嗕袱涓猘ssertEquals錛屽垎鍒繘琛屼簡鍒ゆ柇銆傚湪榪欓噷鎴戜滑瑕佹敞鎰忓埌beginPostMethod鏂規硶涓殑theRequest鍜宼estPostMethod涓殑request鐨勫尯鍒紝鍦ㄥ墠闈㈡垜浠凡緇忔彁鍒拌繃錛宐eginPostMethod鏄湪瀹㈡埛绔墽琛岀殑錛屾墍浠ュ畠鏂規硶鍐呯殑鎵鏈夋搷浣滀簨瀹炰笂鏄ā鎷熼〉闈㈡搷浣滅殑錛屾瘮濡備笂闈㈢殑璁劇疆琛ㄥ崟鏁版嵁錛岃宼estPostMethod鏄湇鍔$鎵ц鐨勶紝鍏朵腑鐨剅equest涔熸槸鏈嶅姟绔殑銆?/p>閰嶇疆cactus.properties鍜寃eb.xml
cactus.properties
榪欎釜灞炴ф槸蹇呴』鐨勶紝瀹冩寚瀹氫簡web搴旂敤鐨勮闂湴鍧
渚嬶細cactus.contextURL = http://localhost:8080/test
鍙夛紝褰撴祴璇曠被緇ф壙ServletTestCase鏃剁敤浜庢寚瀹欳actus Servlet Redirector鐨勬槧灝勫悕縐般傞粯璁わ細ServletRedirector
渚嬶細cactus.servletRedirectorName = ServletRedirector
鍙夛紝褰撴祴璇曠被緇ф壙ServletTestCase鏃剁敤浜庢寚瀹欳actus Jsp Redirector鐨勬槧灝勫悕縐般傞粯璁わ細ServletRedirector
渚嬶細cactus.jspRedirectorName = JspRedirector
鍙夛紝褰撴祴璇曠被緇ф壙ServletTestCase鏃剁敤浜庢寚瀹欳actus Filter Redirector鐨勬槧灝勫悕縐般傞粯璁わ細ServletRedirector
渚嬶細cactus.filterRedirectorName = FilterRedirector
Cactus.properties浣犲彲浠ユ斁緗湪WEB-INF/classes/涓嬨?/p>
web.xml
鍦╳eb.xml閲岃涓虹浉搴旂殑嫻嬭瘯綾繪寚瀹氱浉搴旂殑Cactus Redirector銆?/p>
ServletTestCase瀵瑰簲org.apache.cactus.server.ServletTestRedirector
JspTestCase瀵瑰簲/jspRedirector.jsp
FilterTestCase瀵瑰簲org.apache.cactus.server.FilterTestRedirector
<web-app>
<filter>
<filter-name>FilterRedirector</filter-name>
<filter-class>org.apache.cactus.server.FilterTestRedirector</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterRedirector</filter-name>
<url-pattern>/FilterRedirector</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
</servlet>
<servlet>
<servlet-name>JspRedirector</servlet-name>
<jsp-file>/jspRedirector.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JspRedirector</servlet-name>
<url-pattern>/JspRedirector</url-pattern>
</servlet-mapping>
</web-app>
濡傛灉浣犵殑嫻嬭瘯綾葷戶鎵夸簡JspTestCase鍒欓渶瑕佸皢jspRedirector.jsp鏂囦歡鏀劇疆鍒頒綘鍦╳eb.xml涓寚瀹氱殑璺緞閲屻?/p>
瀹夎璇存槑
濡備笅錛?/p>
junit.jar
servlet.jar
cactus.jar
httpclient.jar
commons-logging.jar
httpunit.jar錛孴idy.jar錛寈erces.jar錛堝彲閫夛紝濡傛灉浣犻泦鎴愪簡httpunit鐨勮瘽灝遍渶瑕侊紝涔熷氨鏄湪endXXX涓嬌鐢ㄤ簡httpunit錛?/p>
cactus.jar
junit.jar
aspectjrt.jar
commons-logging.jar
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl涓烘暟鎹簱鐨凷ID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2://localhost:5000/sample"; //sample涓轟綘鐨勬暟鎹簱鍚? String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb涓烘暟鎹簱 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.sybase.jdbc.SybDriver").newInstance(); String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB涓轟綘鐨勬暟鎹簱鍚? Properties sysProps = System.getProperties(); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps); |
Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //myDB涓烘暟鎹簱鍚? Connection conn= DriverManager.getConnection(url); |
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB涓烘暟鎹簱鍚? Connection conn= DriverManager.getConnection(url); |
Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB涓烘暟鎹簱鍚? String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); Connection conn = DriverManager.getConnection(url,"",""); Statement stmtNew=conn.createStatement() ; |
try{ 銆Class.forName(com.mysql.jdbc.Driver); 銆System.out.println(Success loading Mysql Driver!); }catch(Exception e) { 銆System.out.println(Error loading Mysql Driver!); 銆e.printStackTrace(); } |
jdbc錛歮ysql錛?/localhost/databasename[?pa=va][錛唒a=va] |
PreparedStatement pstmt3D null; try { 銆((OraclePreparedStatement)pstmt).setExecuteBatch(30); 銆... 銆pstmt.executeUpdate(); } |