我想寫一個FitNesse的測試,一個InputStream下載與參考XLSX文件進行比較。從下載比較的InputStream從文件中的InputStream FitNesse的
現在,我的問題是,從InputStream下載得到的內容顯然沒有有效的Zip存檔(它應該像參考文件一樣),除了當前文件內容不同之外(但這不是這裏的問題)。
的測試夾具的方法我在FitNesse的測試基準包含此代碼(更新):
File downloadedFile = writeDownloadedFile();
File downloadedAltFile = writeDownloadedFileAlt();
File expectedReferenceFile = new File(expectedReferenceFilePath.toUri());
InputStream webFixtureIS = getDialog().getInputStream();
InputStream expectedReferenceFileIS = Files.newInputStream(expectedReferenceFilePath);
byte[] testDownloadedFileBytes = Files.readAllBytes(downloadedFile.toPath());
byte[] testDownloadedAltFileBytes = Files.readAllBytes(downloadedAltFile.toPath());
// expectedReferenceFileIS = Files.newInputStream(expectedReferenceFilePath);
// compareEquals = ExcelCompare.workbookEqual(new XSSFWorkbook(OPCPackage.open(downloadedAltFile)), new XSSFWorkbook(OPCPackage.open(expectedReferenceFileIS)));
// expectedReferenceFileIS.close();
// Direct compare from webFixtureInputStream
webFixtureIS = getDialog().getInputStream();
expectedReferenceFileIS = Files.newInputStream(expectedReferenceFilePath);
compareEquals = ExcelCompare.workbookEqual(new XSSFWorkbook(OPCPackage.open(webFixtureIS)), new XSSFWorkbook(OPCPackage.open(expectedReferenceFileIS)));
webFixtureIS.close();
expectedReferenceFileIS.close();
ExcelCompare.workbookEqual()是基於XLSX workbook comparing solution我用於該目的as a workaround here。
基本上,針對webFixtureIS比較時,OPCPackage.getParts()拋出異常,因爲沒有PackageParts是在下載內容菱(ZipArchive不包含任何條目):
org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:197)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:696)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:280)
當針對downloadedAltFile比較代替(該ZipPackage.getPartsImpl() - > ZipEntrySource.getEntries()將失敗,因爲內部ZipArchive爲null。
即使如此,寫下載的InputStream爲這樣的文件,將導致XLSX工作簿,它可以用Excel和OpenOffice打開(更新):
private File writeDownloadedFileAlt() throws IOException, FileNotFoundException {
InputStream webFixtureIS = getDialog().getInputStream();
Path tmpFilePath = Files.createTempFile("jwebunitTestFileAlt", ".xlsx");
byte[] webFixtureIsBytes = IOUtils.toByteArray(webFixtureIS);
// Files.write(tmpFilePath, webFixtureIsBytes, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
Files.write(tmpFilePath, webFixtureIsBytes, StandardOpenOption.CREATE);
webFixtureIS.close();
return new File(tmpFilePath.toUri());
}
private File writeDownloadedFile() throws IOException, FileNotFoundException {
InputStream webFixtureIS = getDialog().getInputStream();
File tmp = File.createTempFile("jwebunitTestFile.xlsx", null);
int c=0;
// InputStream in = getTestingEngine().getInputStream();
tmp.createNewFile();
tmp.deleteOnExit();
OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp));
while ((c = webFixtureIS.read()) != -1) out.write(c);
webFixtureIS.close();
// out.close();
return tmp;
}
writeDownloadedFileAlt()將導致一個工作簿,它可以通過Excel打開,沒有問題,比writeDownloadedFile()生成的16.0kB文件大0.3kB,這觸發了Excel的修復機制(但可以打開)。
當再次保存打開的文件,Excel創建一個文件,大小22.6kB - 我還需要嘗試引用文件比較針對這一個。
我錯過了什麼?
更新: 打開和保存生成的「downloadedAltFile」與Excel實際上將導致與定期的內部ZipArchive結構類似文件,但我需要知道爲什麼還是在這個信息丟失 - 如何避免這種情況的課程。有任何想法嗎?
保存下載的電子表格到一個文件,然後在其上運行Apache提卡應用在'--detect'模式看看它究竟是什麼? – Gagravarr
再次感謝您回覆,Gagravar。 Tika顯示「application/vnd.openxmlformats-officedocument.spreadsheetml.sheet」,與參考文件類型相同。 – fozzybear