/*
 * Copyright 2015 Datalogics, Inc.
 */

package com.datalogics.pdf.samples.util;

import static org.hamcrest.CoreMatchers.equalTo;

import com.adobe.pdfjt.core.exceptions.PDFIOException;
import com.adobe.pdfjt.core.exceptions.PDFInvalidDocumentException;
import com.adobe.pdfjt.core.exceptions.PDFSecurityException;
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImage;

import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * Extended Hamcrest matchers for the sample tests.
 */
public class Matchers {
    private Matchers() {}

    /**
     * Check that a {@link PDFXObjectImage} has a particular SHA-1 checksum.
     *
     * @param checksum the image checksum to check for
     * @return a {@link Matcher}
     */
    public static Matcher<PDFXObjectImage> hasChecksum(final String checksum) {
        // see http://www.planetgeek.ch/2012/03/07/create-your-own-matcher/ for an explanation
        return new FeatureMatcher<PDFXObjectImage, String>(equalTo(checksum), "has checksum", "checksum") {
            private void throwChecksumError(final Throwable exc) {
                throw new IllegalStateException("Getting an image checksum threw " + exc, exc);
            }

            @Override
            protected String featureValueOf(final PDFXObjectImage image) {
                final PDFXObjectImage xObjectImage = image;
                try {
                    return Checksum.getSha1Checksum(xObjectImage.getImageStreamData());
                } catch (final PDFInvalidDocumentException e) {
                    throwChecksumError(e);
                } catch (final PDFIOException e) {
                    throwChecksumError(e);
                } catch (final PDFSecurityException e) {
                    throwChecksumError(e);
                } catch (final NoSuchAlgorithmException e) {
                    throwChecksumError(e);
                } catch (final IOException e) {
                    throwChecksumError(e);
                }
                return null;
            }
        };
    }
}

@@ -464,24 +464,52 @@
 OException;%0A
+import java.io.InputStream;%0A
 import java.
@@ -2144,11 +2144,1194 @@
 %7D;%0A    %7D
+%0A%0A    /**%0A     * Check that an %7B@link InputStream%7D containing image data has a particular SHA-1 checksum.%0A     *%0A     * @param checksum the image checksum to check for%0A     * @return a %7B@link Matcher%7D%0A     */%0A    public static Matcher%3CInputStream%3E inputStreamHasChecksum(final String checksum) %7B%0A        // see http://www.planetgeek.ch/2012/03/07/create-your-own-matcher/ for an explanation%0A        return new FeatureMatcher%3CInputStream, String%3E(equalTo(checksum), %22has checksum%22, %22checksum%22) %7B%0A            private void throwChecksumError(final Throwable exc) %7B%0A                throw new IllegalStateException(%22Getting an image checksum threw %22 + exc, exc);%0A            %7D%0A%0A            @Override%0A            protected String featureValueOf(final InputStream imageStream) %7B%0A                final InputStream stream = imageStream;%0A                try %7B%0A                    return Checksum.getSha1Checksum(stream);%0A                %7D catch (final NoSuchAlgorithmException e) %7B%0A                    throwChecksumError(e);%0A                %7D catch (final IOException e) %7B%0A                    throwChecksumError(e);%0A                %7D%0A                return null;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

Add Hamcrest Matcher for InputStreams