Build 自動驗證程序按圖 2 所示步驟循環執行。
圖 2. Build 自動驗證程序

驗證結果發布服務器
Build 驗證結果需要被持久化存儲,所以我們引入驗證結果發布服務器,開發人員和測試人員可以通過瀏覽器查看所有 Build 的驗證結果。圖 3 展示了驗證結果發布服務器的具體實現,構建(Build)驗證服務器和發布服務器通過數據庫交互,并通過一個 Web 程序進行結果發布;圖 4 則是一個典型的驗證結果發布的實例。
圖 3. 驗證結果發布服務器的具體實現

圖 4. 構建(Build)驗證結果發布

驗證結果發布程序的實現相對簡單,我們對此不作展開。 Build 自動驗證程序則是整個解決方案的核心,下面對它的所有步驟逐一說明。
輪詢下載 Build
Build 的下載一般通過 FTP 協議完成,雖然 Windows 和 Unix 的命令腳本都提供相信的 FTP 支持,但用命令腳本不易實現各種錯誤(例如網絡錯誤或者 Build 服務器不可訪問)的處理邏輯,因此腳本運行的容錯性和穩定性相對較差。而 Build 自動驗證程序要求在無人干預的情況下 7 × 24 小時工作,所以我們選擇 Java 實現 Build 輪詢下載。圖 5 是 Build 輪詢下載的基本流程,BuildDownload.java 則是該流程的具體實現。
圖 5. Build 輪詢下載流程

清單 1. BuildDownload.java
public class BuildDownload { private String server; //Build Server's IP or host name private int port; //Build Server's FTP port private String user; //User ID used to log in the Build Server private String pwd; //Password private String remoteBuildDir; //The remote build directory on the Build Server private String localBuildDir; //The local directory to store the downloaded build private int interval; //The interval minutes to search the new build public static void main(String[] args) { //1. Check the input parameter: the full path of the configuration file if (args.length != 1) { System.out.println("Error - Invalid parameters."); System.exit(1); } BuildDownload dameon = new BuildDownload(args[0]); //2. Search and download the newest build boolean isSuccess = false; do { isSuccess = dameon.download(); } while(!isSuccess); } public BuildDownload(String configFile) { /*Load the specified property file and initialize the configurations*/ } public boolean download() { FtpClient client = new FtpClient(); InputStream input = null; BufferedInputStream buffer = null; BufferedReader bufferReader = null; FileOutputStream outfile = null; try { System.out.println("Info - Wait " + this.interval + " minutes."); Thread.sleep(this.interval*60000); //1. Connect to FTP server client.openServer(this.server, this.port); client.login(this.user, this.pwd); //2. Search the newest build on the Build Server System.out.println("Info - Search the newest build on the Build Server."); client.cd(this.remoteBuildDir); input = client.list(); bufferReader = new BufferedReader(new InputStreamReader(input)); String newest = "00000000000000.zip"; //The format of build name: yyyyMMddHHmmss.zip while ((temp=bufferReader.readLine())!=null) { String temp = temp.trim(); int start = temp.lastIndexOf(" "); temp = temp.substring(start+1); if ( temp.compareTo(newest) > 0 ) newest = temp; } bufferReader.close(); input.close(); System.out.println("Info - The newest build on the Build Server is " + newest); //3. Compare the remote newest build with the latest verified build String latestVerifiedBuild = getLatestVerifiedBuildID(); if (latestVerifiedBuild.compareTo(newest) >= 0) { client.closeServer(); System.out.println("Info - The build " + newest + " has been verified."); return false; } System.out.println("Info - A new build " + newest + " is ready."); //4. Download the newest build clearLocalDirectory(); System.out.println("Info - Start to download the build " + newest); client.binary(); input = (InputStream)client.get(this.remoteBuildDir + "/" + newest); buffer = new BufferedInputStream(input); outfile = new FileOutputStream(this.localBuildDir + "/" + newest, true); int content; while((content=buffer.read()) != -1) { outfile.write(content); } outfile.flush(); outfile.close(); buffer.close(); input.close(); client.closeServer(); System.out.println("Info - Finish to download the build " + newest); return true; } catch (Exception e) { e.printStackTrace(); System.out.println("Error - Unexpected exception happened."); return false; } } public String getLatestVerifiedBuildID() { /*Get the latest verified build from the database in the publish server*/ } public void clearLocalDirectory() { /*Clear the local directory in which the downloaded build was stored*/ }} |
文章來源于領測軟件測試網 http://www.kjueaiud.com/