/*
 * Copyright 2015 Datalogics, Inc.
 */

package com.datalogics.pdf.samples.creation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.adobe.pdfjt.core.types.ASName;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFResources;
import com.adobe.pdfjt.pdf.graphics.font.PDFFont;

import com.datalogics.pdf.samples.SampleTest;

import org.junit.Test;

import java.io.File;
import java.nio.file.Files;

/**
 * Tests the MakeWhiteFangBook sample.
 */
public class MakeWhiteFangBookTest extends SampleTest {
    static final String FILE_NAME = "WhiteFang.pdf";

    @Test
    public void testMain() throws Exception {
        final File file = newOutputFile(FILE_NAME);
        if (file.exists()) {
            Files.delete(file.toPath());
        }
        MakeWhiteFangBook.main(file.getCanonicalPath());
        assertTrue(file.getPath() + " must exist after run", file.exists());

        final PDFDocument doc = openPdfDocument(file.getCanonicalPath());

        try {
            // Verify the resources
            final PDFResources resources = pageResources(doc, 1);

            final PDFFont f0 = resources.getFont(ASName.create("F0"));
            assertEquals(ASName.k_Times_Bold, f0.getBaseFont());
            assertNiceSimpleFont(f0);

            final PDFFont f1 = resources.getFont(ASName.create("F1"));
            assertEquals(ASName.k_Times_Roman, f1.getBaseFont());
            assertNiceSimpleFont(f1);

            // Verify the contents
            for (int i = 1; i < 7; i++) {
                final String contentsAsString = pageContentsAsString(doc, i);
                final String resourceName = String.format("WhiteFang.pdf.page%d.txt", i);

                assertEquals(contentsOfResource(resourceName), contentsAsString);
            }

        } finally {
            doc.close();
        }
    }
}

@@ -100,37 +100,198 @@
 tic 
-org.junit.Assert.assertEquals
+com.datalogics.pdf.samples.util.Matchers.hasChecksum;%0A%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertThat
 ;%0Aim
@@ -522,16 +522,233 @@
 PDFFont;
+%0Aimport com.adobe.pdfjt.pdf.graphics.xobject.PDFXObject;%0Aimport com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImage;%0Aimport com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectMap;%0Aimport com.adobe.pdfjt.pdf.page.PDFPage;
 %0A%0Aimport
@@ -1838,32 +1838,32 @@
 getBaseFont());%0A
-
             asse
@@ -1881,24 +1881,817 @@
 eFont(f1);%0A%0A
+            // Verify the checksum of the image%0A            final PDFPage page = doc.requirePages().getPage(0);%0A            final PDFXObjectMap objMap = page.getResources().getXObjectMap();%0A            int numImages = 0;%0A%0A            for (final ASName name : objMap.keySet()) %7B%0A                final PDFXObject o = objMap.get(name);%0A                if (o instanceof PDFXObjectImage) %7B%0A                    assertThat(%22there should only be one image on the first page of the test document%22, numImages++,%0A                               equalTo(0));%0A                    final PDFXObjectImage image = (PDFXObjectImage) o;%0A                    assertThat(image, hasChecksum(%22ea7454d6be178a1db357fda17f0d130cad314995%22));%0A                %7D%0A            %7D%0A            assertThat(numImages, equalTo(1));%0A%0A
             

Test front cover image.