/*
 * Copyright 2015 Datalogics, Inc.
 */

package com.datalogics.pdf.samples.extraction;

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

import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.RandomAccessFileByteWriter;
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.core.types.ASRectangle;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.graphics.PDFRectangle;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.pdf.page.PDFPageTree;

import com.datalogics.pdf.samples.SampleTest;
import com.datalogics.pdf.samples.util.LogRecordListCollector;

import org.junit.Test;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Tests the TextExtract sample.
 */
public class TextExtractTest extends SampleTest {

    private static final String OUTPUT_FILE_PATH = "TextExtractTest.txt";
    private static final String INPUT_PDF_PATH = "/com/datalogics/pdf/samples/pdfjavatoolkit-ds.pdf";
    private static final String EXTRACTED_DOCUMENT_NAME = "TextExtractTest-ReadingOrder.txt";
    private static final String EMPTY_PDF_FILE_PATH = "Empty.pdf";
    private static final String EMPTY_TEXT_FILE_PATH = "Empty.txt";

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

        final URL inputUrl = TextExtract.class.getResource(INPUT_PDF_PATH);
        final URL outputUrl = file.toURI().toURL();

        TextExtract.extractTextReadingOrder(inputUrl, outputUrl);;
        assertTrue(file.getPath() + " must exist after run", file.exists());

        final String extractedText = contentsOfTextFile(file);
        assertEquals(contentsOfResource(EXTRACTED_DOCUMENT_NAME), extractedText);
    }

    @Test
    public void testDocumentWithNoText() throws Exception {
        // Create a new document with a single empty page
        final PDFDocument document = createEmptyPdf();
        final File emptyPdf = newOutputFileWithDelete(EMPTY_PDF_FILE_PATH);
        final RandomAccessFile outputPdfFile = new RandomAccessFile(emptyPdf, "rw");
        final ByteWriter outputWriter = new RandomAccessFileByteWriter(outputPdfFile);
        document.saveAndClose(outputWriter, PDFSaveFullOptions.newInstance());

        final File emptyTxt = newOutputFileWithDelete(EMPTY_TEXT_FILE_PATH);

        final URL inputUrl = emptyPdf.toURI().toURL();
        final URL outputUrl = emptyTxt.toURI().toURL();

        final ArrayList<LogRecord> logRecords = new ArrayList<LogRecord>();
        final Logger logger = Logger.getLogger(TextExtract.class.getName());
        try (LogRecordListCollector collector = new LogRecordListCollector(logger, logRecords)) {
            TextExtract.extractTextReadingOrder(inputUrl, outputUrl);
        }

        // Verify that we got the expected log message
        assertEquals("Must have one log record", 1, logRecords.size());
        final LogRecord logRecord = logRecords.get(0);
        assertEquals(inputUrl.getFile() + " did not have any text to extract.", logRecord.getMessage());
        assertEquals(Level.INFO, logRecord.getLevel());

        // Verify that the output file was not created
        final Path path = Paths.get(outputUrl.getPath());
        assertTrue(outputUrl.getFile() + " should not exist.", Files.notExists(path, LinkOption.NOFOLLOW_LINKS));
    }

    /*
     * Create a PDF with a single blank page, for testing.
     */
    private static PDFDocument createEmptyPdf() throws PDFInvalidDocumentException, PDFIOException,
                    PDFSecurityException {
        final PDFDocument document = PDFDocument.newInstance(PDFOpenOptions.newInstance());
        final PDFPage page = PDFPage.newInstance(document, PDFRectangle.newInstance(document, ASRectangle.US_LETTER));
        PDFPageTree.newInstance(document, page);
        return document;
    }
}

@@ -177,106 +177,8 @@
 e;%0A%0A
-import com.adobe.internal.io.ByteWriter;%0Aimport com.adobe.internal.io.RandomAccessFileByteWriter;%0A
 impo
@@ -220,32 +220,32 @@
 PDFIOException;%0A
+
 import com.adobe
@@ -509,64 +509,8 @@
 ns;%0A
-import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;%0A
 impo
@@ -638,24 +638,75 @@
 FPageTree;%0A%0A
+import com.datalogics.pdf.document.DocumentHelper;%0A
 import com.d
@@ -827,16 +827,16 @@
 .Test;%0A%0A
+
 import j
@@ -852,41 +852,8 @@
 le;%0A
-import java.io.RandomAccessFile;%0A
 impo
@@ -2525,246 +2525,75 @@
     
-final RandomAccessFile outputPdfFile = new RandomAccessFile(emptyPdf, %22rw%22);%0A        final ByteWriter outputWriter = new RandomAccessFileByteWriter(outputPdfFile);%0A        document.saveAndClose(outputWriter, PDFSaveFullOptions.newInstance
+DocumentHelper.saveFullAndClose(document, emptyPdf.getCanonicalPath
 ());

Use the Talkeetna DocumentHelper in the test