我想写一个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