use of org.commonjava.maven.galley.io.checksum.testutil.TestMetadataConsumer in project galley by Commonjava.
the class ChecksummingOutputStreamTest method verifyUsingMd5.
@Test
public void verifyUsingMd5() throws Exception {
final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), "my-path.txt"));
final OutputStream os = new ByteArrayOutputStream();
final byte[] data = "This is a test with a bunch of data and some other stuff, in a big box sealed with chewing gum".getBytes();
ChecksummingOutputStream stream = null;
final TestMetadataConsumer testConsumer = new TestMetadataConsumer();
try {
stream = new ChecksummingOutputStream(new HashSet<AbstractChecksumGeneratorFactory<?>>(Arrays.asList(new Md5GeneratorFactory(), new Sha1GeneratorFactory(), new Sha256GeneratorFactory())), os, txfr, testConsumer, true);
stream.write(data);
} finally {
IOUtils.closeQuietly(stream);
}
final MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
final byte[] digest = md.digest();
final String digestHex = Hex.encodeHexString(digest);
final Transfer md5Txfr = txfr.getSiblingMeta(".md5");
InputStream in = null;
String resultHex = null;
try {
in = md5Txfr.openInputStream();
resultHex = IOUtils.toString(in);
} finally {
IOUtils.closeQuietly(in);
}
assertThat(resultHex, equalTo(digestHex));
TransferMetadata metadata = testConsumer.getMetadata(txfr);
assertThat(metadata, notNullValue());
Map<ContentDigest, String> digests = metadata.getDigests();
assertThat(digests, CoreMatchers.<Map<ContentDigest, String>>notNullValue());
assertThat(digests.get(MD5), equalTo(digestHex));
}
use of org.commonjava.maven.galley.io.checksum.testutil.TestMetadataConsumer in project galley by Commonjava.
the class ChecksummingInputStreamTest method verifyUsingMd5.
@Test
public void verifyUsingMd5() throws Exception {
final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), "my-path.txt"));
final byte[] data = "This is a test with a bunch of data and some other stuff, in a big box sealed with chewing gum".getBytes("UTF-8");
final InputStream is = new ByteArrayInputStream(data);
ChecksummingInputStream stream = null;
final TestMetadataConsumer testConsumer = new TestMetadataConsumer();
try {
stream = new ChecksummingInputStream(new HashSet<AbstractChecksumGeneratorFactory<?>>(Arrays.asList(new Md5GeneratorFactory(), new Sha1GeneratorFactory(), new Sha256GeneratorFactory())), is, txfr, testConsumer, true);
logger.debug("Reading stream with {} bytes", data.length);
byte[] resultData = IOUtils.toByteArray(stream);
logger.debug("Result is {} bytes", resultData.length);
assertThat(Arrays.equals(resultData, data), equalTo(true));
} finally {
IOUtils.closeQuietly(stream);
}
final MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
final byte[] digest = md.digest();
final String digestHex = Hex.encodeHexString(digest);
logger.debug("Verifying .md5 file");
final Transfer md5Txfr = txfr.getSiblingMeta(".md5");
InputStream in = null;
String resultHex = null;
try {
in = md5Txfr.openInputStream();
resultHex = IOUtils.toString(in);
} finally {
IOUtils.closeQuietly(in);
}
assertThat(resultHex, equalTo(digestHex));
logger.debug("Verifying MD5 in metadata consumer");
TransferMetadata metadata = testConsumer.getMetadata(txfr);
assertThat(metadata, notNullValue());
Map<ContentDigest, String> digests = metadata.getDigests();
assertThat(digests, CoreMatchers.<Map<ContentDigest, String>>notNullValue());
assertThat(digests.get(MD5), equalTo(digestHex));
}
Aggregations