Comparing InputStream from Download to InputStream from File in FitNesse
<p>I'm trying to write a FitNesse test which compares an InputStream download against a reference XLSX file.</p>

<p>Now, my problem is, that the content I get from the InputStream download is obviously no valid Zip archive (which it should be, like the reference file), apart from the current file contents differing in general (but that's not the issue here).</p>

<p>The testing fixture's method I reference in the FitNesse test contains this code (updated):</p>

<pre><code>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();
</code></pre>

<p>ExcelCompare.workbookEqual() is based on the <a href="http://blog.sodhanalibrary.com/2015/03/compare-excel-sheets-using-java.html#.V6Ri_XrJ2xU" rel="nofollow noreferrer">XLSX workbook comparing solution</a> I applied for this purpose <a href="https://stackoverflow.com/questions/38698734/poi-xssf-xlsx-hashing-indeterminism-with-messagedigest-sha-256/38794756#38794756">as a workaround here</a>. </p>

<p>Basically, when comparing against webFixtureIS, OPCPackage.getParts() throws an Exception, because no PackageParts are availible in the download content (ZipArchive contains no entries):</p>

<pre><code>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)
</code></pre>

<p>When comparing against downloadedAltFile instead (the commented-out code above), ZipPackage.getPartsImpl() -> ZipEntrySource.getEntries() will fail, since the internal ZipArchive is null.</p>

<p>Even so, writing the download InputStream into a file like this will result in a XLSX workbook, which can be opened with Excel and OpenOffice (updated):</p>

<pre><code>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;
}
</code></pre>

<p>writeDownloadedFileAlt() will result in a workbook, which can be opened by Excel without problems, being 0.3kB larger than 16.0kB file writeDownloadedFile() produces, which triggers Excel's repair mechanism (but can be opened still).</p>

<p>When saving the opened file again, Excel creates a file, sized 22.6kB - I still need to try out comparing the reference file against this one.</p>

<p>What am I missing?</p>

<p>Update:
Opening and saving the generated "downloadedAltFile" with Excel will actually result in a comparable file with regular internal ZipArchive structure, but I need to know why or where this information is lost - and how to avoid that, of course. Any ideas?</p>