/*
 * Copyright 2015 Datalogics, Inc.
 */

package com.datalogics.pdf.samples.manipulation;

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

import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationList;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.pdf.page.PDFPageTree;

import com.datalogics.pdf.samples.SampleTest;

import org.junit.Test;

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

/**
 * Tests the FlattenPdf sample.
 */
public class FlattenPdfTest extends SampleTest {

    private static final String OUTPUT_FLATTENED_FORM_PDF_PATH = "FlattenedForm.pdf";
    private static final String OUTPUT_FLATTENED_ANNOTATION_PDF_PATH = "FlattenedAnnotation.pdf";
    private static final String INPUT_FORM_PDF_PATH = "Acroform.pdf";
    private static final String INPUT_ANNOTATION_PDF_PATH = "annotations.pdf";

    @Test
    public void testFlattenForm() throws Exception {
        final File file = newOutputFile(OUTPUT_FLATTENED_FORM_PDF_PATH);
        if (file.exists()) {
            Files.delete(file.toPath());
        }

        FlattenPdf.main(INPUT_FORM_PDF_PATH, file.getCanonicalPath());
        assertTrue(file.getPath() + " must exist after run", file.exists());

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

        assertEquals("There should not be an Acroform dictionary in a flattened form document",
                     document.getInteractiveForm(), null);
    }

    @Test
    public void testFlattenAnnotations() throws Exception {
        final File file = newOutputFile(OUTPUT_FLATTENED_ANNOTATION_PDF_PATH);
        if (file.exists()) {
            Files.delete(file.toPath());
        }

        FlattenPdf.main(INPUT_ANNOTATION_PDF_PATH, file.getCanonicalPath());
        assertTrue(file.getPath() + " must exist after run", file.exists());

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

        final PDFPageTree pgTree = document.requirePages();
        final PDFPage currentPage = pgTree.getPage(0);

        final PDFAnnotationList list = currentPage.getAnnotationList();

        for (int i = 0; i < list.size(); i++) {
            assertEquals("Flattened annotations should not have content", list.get(i).getContents(), null);
        }

    }
}

@@ -475,36 +475,8 @@
 ile;
-%0Aimport java.nio.file.Files;
 %0A%0A/*
@@ -978,32 +978,43 @@
 nal File file = 
+SampleTest.
 newOutputFile(OU
@@ -1002,32 +1002,42 @@
 st.newOutputFile
+WithDelete
 (OUTPUT_FLATTENE
@@ -1057,88 +1057,8 @@
 TH);
-%0A        if (file.exists()) %7B%0A            Files.delete(file.toPath());%0A        %7D
 %0A%0A  
@@ -1542,16 +1542,27 @@
  file = 
+SampleTest.
 newOutpu
@@ -1566,16 +1566,26 @@
 tputFile
+WithDelete
 (OUTPUT_
@@ -1590,34 +1590,28 @@
 T_FLATTENED_
-ANNOTATION
+FORM
 _PDF_PATH);%0A
@@ -1613,88 +1613,8 @@
 TH);
-%0A        if (file.exists()) %7B%0A            Files.delete(file.toPath());%0A        %7D
 %0A%0A  
@@ -2039,60 +2039,8 @@
 );%0A%0A
-        for (int i = 0; i %3C list.size(); i++) %7B%0A    
     
@@ -2057,17 +2057,26 @@
 Equals(%22
-F
+Properly f
 lattened
@@ -2103,66 +2103,56 @@
 not 
-have content%22, list.get(i).getContents(), null);%0A        %7D
+appear in the annotation list%22, list.size(), 0);
 %0A%0A  

update flatten sample tests