Search in sources :

Example 1 with TestMetadataConsumer

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));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) TestMetadataConsumer(org.commonjava.maven.galley.io.checksum.testutil.TestMetadataConsumer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MessageDigest(java.security.MessageDigest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with TestMetadataConsumer

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) TestMetadataConsumer(org.commonjava.maven.galley.io.checksum.testutil.TestMetadataConsumer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MessageDigest(java.security.MessageDigest) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)2 MessageDigest (java.security.MessageDigest)2 HashSet (java.util.HashSet)2 TestMetadataConsumer (org.commonjava.maven.galley.io.checksum.testutil.TestMetadataConsumer)2 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)2 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1