私は、InputStreamのダウンロードを参照XLSXファイルと比較するFitNesseテストを作成しようとしています。ダウンロードからのInputStreamとFitNesseのFileからのInputStreamの比較
私の問題は、InputStreamのダウンロードから得られるコンテンツは、明らかに現在のファイルの内容とは別に、一般的なものとは別に、有効なZipアーカイブ(これは参照ファイルと同じ)であることです。ここの問題)。
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()私は、この目的のために適用as a workaround hereXLSX workbook comparing solutionに基づいている:私はFitNesseの試験で参照
試験治具の方法は、(更新)このコードを含んでいます。 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を書くことはExcelとOpenOfficeの(更新)で開くことができXLSXワークブック、になります:
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は()ワークブックになりますこれは問題なくエクセルで開くことができ、16kバイトのファイルwriteDownloadedFile()より0.3kB大きく、Excelの修復メカニズムを起動します(ただし、まだ開くことができます)。
開かれたファイルを再度保存すると、22.6kBのサイズのファイルがExcelによって作成されます。このファイルは、参照ファイルと比較して試してみる必要があります。
私には何が欠けていますか?
更新: 生成された「downloadedAltFile」をExcelで開いて保存すると、実際には通常の内部ZipArchive構造と同等のファイルが作成されますが、この情報が失われた理由や場所を知る必要があります。コース。何か案は?
ダウンロードしたスプレッドシートをファイルに保存し、 '--detect'モードでApache Tikaアプリケーションを実行しますそれが本当に何かを見るために? – Gagravarr
Gagravarに返信してくれてありがとう。 Tikaは、参照ファイルと同じタイプの "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"を公開しています。 – fozzybear