ChecksumUtil { public static long createCombinedChecksum(final List<Path> paths) throws IOException { final CRC32 checksumGenerator = new CRC32(); final List<Long> chunkChecksum = new ArrayList<>(); for (final var path : paths) { checksumGenerator.update(Files.readAllBytes(path)); chunkChecksum.add(checksumGenerator.getValue()); checksumGenerator.reset(); } chunkChecksum.forEach( c -> checksumGenerator.update(ByteBuffer.allocate(Long.BYTES).putLong(0, c))); return checksumGenerator.getValue(); } private  ChecksumUtil(); static long createCombinedChecksum(final List<Path> paths);  }
@Test public void shouldGenerateTheSameChecksumForOneFile() throws Exception { final var expectedChecksum = ChecksumUtil.createCombinedChecksum(List.of(exampleFile)); final var actual = ChecksumUtil.createCombinedChecksum(List.of(exampleFile)); assertThat(actual).isEqualTo(expectedChecksum); }
@Test public void shouldGenerateTheSameChecksumForMultipleFiles() throws Exception { final var expectedChecksum = ChecksumUtil.createCombinedChecksum( List.of(exampleFile, otherExampleFile, thirdExampleFile)); final var actual = ChecksumUtil.createCombinedChecksum( List.of(exampleFile, otherExampleFile, thirdExampleFile)); assertThat(actual).isEqualTo(expectedChecksum); }
@Test public void shouldGenerateDifferentChecksumForDifferentFiles() throws Exception { final var expectedChecksum = ChecksumUtil.createCombinedChecksum(List.of(exampleFile)); final var actual = ChecksumUtil.createCombinedChecksum(List.of(thirdExampleFile)); assertThat(actual).isNotEqualTo(expectedChecksum); }
@Test public void shouldGenerateDifferentChecksumForDifferentCombination() throws Exception { final var expectedChecksum = ChecksumUtil.createCombinedChecksum(List.of(exampleFile, otherExampleFile)); final var actual = ChecksumUtil.createCombinedChecksum(List.of(exampleFile, thirdExampleFile)); assertThat(actual).isNotEqualTo(expectedChecksum); }
@Test public void shouldGenerateDifferentChecksumOnDifferentOrder() throws Exception { final var expectedChecksum = ChecksumUtil.createCombinedChecksum( List.of(exampleFile, otherExampleFile, thirdExampleFile)); final var actual = ChecksumUtil.createCombinedChecksum( List.of(thirdExampleFile, otherExampleFile, exampleFile)); assertThat(actual).isNotEqualTo(expectedChecksum); }