作成したファイルをEclipseのsrc
フォルダのresourcesフォルダにあるベンチマークファイルと比較するJUnitTest
テストを作成しています。Java - 2つの同一ファイルのInputStreamを比較します
コード
public class CompareFileTest
{
private static final String TEST_FILENAME = "/resources/CompareFile_Test_Output.xls";
@Test
public void testCompare()
{
InputStream outputFileInputStream = null;
BufferedInputStream bufferedInputStream = null;
File excelOne = new File(StandingsCreationHelper.directoryPath + "CompareFile_Test_Input1.xls");
File excelTwo = new File(StandingsCreationHelper.directoryPath + "CompareFile_Test_Input1.xls");
File excelThree = new File(StandingsCreationHelper.directoryPath + "CompareFile_Test_Output.xls");
CompareFile compareFile = new CompareFile(excelOne, excelTwo, excelThree);
// The result of the comparison is stored in the excelThree file
compareFile.compare();
try
{
outputFileInputStream = new FileInputStream(excelThree);
bufferedInputStream = new BufferedInputStream(outputFileInputStream);
assertTrue(IOUtils.contentEquals(CompareFileTest.class.getResourceAsStream(TEST_FILENAME), bufferedInputStream));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
しかし、私は、任意の詳細がなくて、アサーションエラーメッセージが表示されます。私はちょうど比較ファイル操作からベンチマークファイルを作成して以来、両方のファイルが同一でなければなりません。
ありがとうございます!
編集:スリムのコメントの後、私はファイル差分ツールを使用して、両方のファイルが異なっていることがわかりましたが、コピーされているので、どうなったのかわかりません。たぶん、タイムスタンプなどがありますか?
'CompareFileTest.class.getResourceAsStream(TEST_FILENAME)'を変数に抽出し、nullでないことを確認します。 – roby
@roby残念ながらヌルではありません。 –
次に、両方のファイルをStringに変換してassertEqualsを試すことができます。 'IOUtils.toString(InputStream、Charset)'を使うことができます。 – roby