2014-06-12 50 views
1

我使用SoapUI Pro爲一家郵政公司測試一組Web服務。在SoapUI中使用groovy編輯目錄Pro

作爲我的測試用例的一部分,我以pdf格式生成標籤和清單文檔。我將這些pdf存儲在測試用例開始時由groovy腳本創建的文件夾中。有時當我運行測試時,沒有生成標籤或pdf,因爲我正在測試錯誤代碼,即使用不創建貨件的測試輸入,因此不生成標籤。

我在測試用例的末尾有一個腳本,它將刪除我創建的文件夾,如果它是空的。我想修改它以壓縮整個文件夾,如果它不是空的,即在其中有標籤pdf。但不知道如何做到這一點。

以下是我的腳本刪除空文件夾。對不起,我沒有嘗試任何代碼來執行壓縮。

import groovy.xml.NamespaceBuilder 
import org.apache.tools.antBuilder.* 
//This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs are generated as part of test 
// i.e test does not produce valid shipments. 
def pdfSubFolderPath = context.expand('${#TestCase#pdfSubFolderPath}') 
def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize() 

if(pdfSubFolderSize == 0){ 
    new File(pdfSubFolderPath).deleteDir() 
    log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs" 
} 
else 
{ 
//zip folder and contents 
} 

回答

1

我修改代碼以壓縮在您的pdfSubFolderPath所有文件:

import groovy.xml.NamespaceBuilder 
import org.apache.tools.antBuilder.* 
import java.util.zip.ZipOutputStream 
import java.util.zip.ZipEntry 
import java.nio.channels.FileChannel 

//This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs are generated as part of test 
// i.e test does not produce valid shipments. 
def pdfSubFolderPath = context.expand('${#TestCase#pdfSubFolderPath}') 
def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize() 

if(pdfSubFolderSize == 0){ 
    new File(pdfSubFolderPath).deleteDir() 
    log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs" 
} 
else 
{ 
    //zip folder and contents 
    def zipFileName = "C:/temp/file.zip" // output zip file name 
    def inputDir = pdfSubFolderPath; // dir to be zipped 

    // create the output zip file 
    def zipFile = new ZipOutputStream(new FileOutputStream(zipFileName)) 

    // for each file in the directori 
    new File(inputDir).eachFile() { file -> 
     zipFile.putNextEntry(new ZipEntry(file.getName())) 
     file.eachByte(1024) { buffer, len -> zipFile.write(buffer, 0, len) } 
     zipFile.closeEntry() 
    } 
    zipFile.close() 
} 

希望這有助於

+1

如果該文件是> 1024個字節長會發生什麼事?更好的辦法是使用:'file.eachByte(1024){buffer,len - > zipFile.write(buffer,0,len)}' –

+0

你是對的+1!我糾正了答案。謝謝:) – albciff

+0

感謝你們倆。解決方案很好。擔。 – chucknor