軟體行業發展迅猛,開發模式不斷演變。XP(Extreme Programming,極限編程)提供全新開發模式。本文介紹基於免費工具的 XP 增量開發環境建立:Ant + JUnit + Log4j + CVS。
| 工具 | 特點 | 下載地址 |
|---|---|---|
| Ant | 基於 Java 的跨平台建構工具,配置檔為 XML 任務樹,不需撰寫 shell 命令 | jakarta.apache.org/builds/jakarta-ant/release/ |
| JUnit | Java 單元測試框架標準,配合 Refactoring 與 XP 技術廣泛採用 | www.junit.org |
| Log4j | Apache 開源日誌框架,靈活控制輸出目的地、格式、級別,透過配置檔不修改代碼即可調整 | jakarta.apache.org/log4j/jakarta-log4j-1.2.8.zip |
| JDOM | Java + XML = JDOM,比 DOM/SAX 更方便的 XML 處理接口,樹操作風格 | www.jdom.org/downloads/index.html |
JAVA_HOME、ANT_HOMEant,出現 Buildfile: build.xml does not exist! Build failed 即成功JUnit.jar 複製到 ANT_HOME/lib 目錄以支援 JUnit 任務package test;
import xml.ReadXML;
import junit.framework.*;
public class ReadXMLCase extends TestCase {
public ReadXMLCase() { super(); }
public ReadXMLCase(String arg0) { super(arg0); }
public void testPrintXML() {
ReadXML rx = new ReadXML("E:/workspace/graduationDesigner/web.xml");
rx.printXML();
}
public static void main(String args[]) {
junit.textui.TestRunner.run(new TestSuite(ReadXMLCase.class));
}
}
package xml;
import org.apache.log4j.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
public class ReadXML {
static Logger log = Logger.getLogger(ReadXML.class.getName());
private String fileName;
private SAXBuilder builder;
public ReadXML(String fileName) {
super();
BasicConfigurator.configure();
this.fileName = fileName;
builder = new SAXBuilder();
}
public void printXML() {
try {
log.info("開始創建Document");
Document doc = builder.build(new File(fileName));
XMLOutputter outputter = new XMLOutputter();
log.info("開始輸出XML內容:");
outputter.output(doc, System.out);
log.info("輸出結束!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<project name="acaiDesigner" default="junit" basedir=".">
<property name="appname" value="acaiDesigner"/>
<property name="src" value="${basedir}/src"/>
<property name="build.classesdir" value="${basedir}/bin"/>
<path id="classpath">
<pathelement path="${classpath}:${build.classesdir}"/>
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile">
<javac srcdir="${src}" destdir="${build.classesdir}">
<classpath refid="classpath"/>
</javac>
</target>
<target name="junit" depends="compile">
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<classpath refid="classpath"/>
<formatter type="plain"/>
<test name="test.ReadXMLCase"/>
</junit>
</target>
</project>
DOS 下執行 ant 即可自動編譯與單元測試。編譯錯誤會列印對應資訊。測試結果生成 TEST-test.ReadXMLCase.txt,記錄測試過程與 Log4j 輸出。
XP 強調測試代碼多於源代碼,是建立強大系統根基。此方式讓開發者輕易排錯,有信心說「這是經過測試的」。歡迎交流:webmaster@justhis.com | https://ejb.cn