### Question:
ChecksumHelper { protected byte[] validateAndStripChecksum(byte[] source) { int lastIndexOf = Bytes.lastIndexOf(source, (byte) SEPARATOR); if (lastIndexOf < 0) { throw new RuntimeException("no checksum was found on source: " + new String(source)); } long checkSum = Long.parseLong(new String(Arrays.copyOfRange(source, lastIndexOf + 1, source.length))); byte[] data = Arrays.copyOfRange(source, 0, lastIndexOf); CRC32 crc32 = new CRC32(); crc32.update(data); long calculatedCheckSum = crc32.getValue(); if (Long.compare(checkSum, calculatedCheckSum) != 0) { throw new RuntimeException("checksums do not match! calculated: " + calculatedCheckSum + ", provided: " + checkSum); } return data; }    }### Answer:
@Test public void testValidateChecksum() { ChecksumHelper checksumHelper = new ChecksumHelper(); byte[] withChecksum = "foobarbaz#444082090".getBytes(); log.debug("withChecksum: " + new String(withChecksum)); assertNotNull(withChecksum); byte[] stripped = checksumHelper.validateAndStripChecksum(withChecksum); log.debug("stripped : " + new String(stripped)); assertEquals("foobarbaz", new String(stripped)); }
@Test(expected = RuntimeException.class) public void testValidateChecksumFailsOnMissingChecksum() { ChecksumHelper checksumHelper = new ChecksumHelper(); byte[] withChecksum = "foobarbaz".getBytes(); log.debug("withChecksum: " + new String(withChecksum)); assertNotNull(withChecksum); checksumHelper.validateAndStripChecksum(withChecksum); fail("should have thrown an exception by now"); }
@Test(expected = RuntimeException.class) public void testValidateChecksumFailsOnFalseChecksum() { ChecksumHelper checksumHelper = new ChecksumHelper(); byte[] withChecksum = "foobarbaz#444082092".getBytes(); log.debug("withChecksum: " + new String(withChecksum)); assertNotNull(withChecksum); checksumHelper.validateAndStripChecksum(withChecksum); fail("should have thrown an exception by now"); }
@Test(expected = RuntimeException.class) public void testValidateChecksumFailsOnFalseChecksum2() { ChecksumHelper checksumHelper = new ChecksumHelper(); byte[] withChecksum = "foobarbal#444082090".getBytes(); log.debug("withChecksum: " + new String(withChecksum)); assertNotNull(withChecksum); checksumHelper.validateAndStripChecksum(withChecksum); fail("should have thrown an exception by now"); }