/*
 * Copyright 2015 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.examples.pdfa;

import junit.framework.TestCase;

import java.io.File;
import org.apache.pdfbox.examples.pdmodel.CreatePDFA;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.preflight.PreflightDocument;
import org.apache.pdfbox.preflight.ValidationResult;
import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
import org.apache.pdfbox.preflight.parser.PreflightParser;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.apache.xmpbox.xml.DomXmpParser;

/**
 *
 * @author Tilman Hausherr
 */
public class CreatePDFATest extends TestCase
{
    private final String outDir = "target/test-output";

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        new File(outDir).mkdirs();
    }

    /**
     * Test of doIt method of class CreatePDFA.
     */
    public void testCreatePDFA() throws Exception
    {
        System.out.println("testCreatePDFA");
        String pdfaFilename = outDir + "/PDFA.pdf";
        String message = "The quick brown fox jumps over the lazy dog äöüÄÖÜß @°^²³ {[]}";
        String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
        String fontfile = dir + "LiberationSans-Regular.ttf";
        CreatePDFA.main(new String[] { pdfaFilename, message, fontfile });
        
        PreflightParser preflightParser = new PreflightParser(new File(pdfaFilename));
        preflightParser.parse();
        try (PreflightDocument preflightDocument = preflightParser.getPreflightDocument())
        {
            preflightDocument.validate();
            ValidationResult result = preflightDocument.getResult();
            for (ValidationError ve : result.getErrorsList())
            {
                System.err.println(ve.getErrorCode() + ": " + ve.getDetails());
            }
            assertTrue("PDF file created with CreatePDFA is not valid PDF/A-1b", result.isValid());
        }
        
        // check the XMP metadata
        PDDocument document = PDDocument.load(new File(pdfaFilename));
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDMetadata meta = catalog.getMetadata();
        DomXmpParser xmpParser = new DomXmpParser();
        XMPMetadata metadata = xmpParser.parse(meta.createInputStream());
        DublinCoreSchema dc = metadata.getDublinCoreSchema();
        assertEquals(pdfaFilename, dc.getTitle());
        document.close();
    }
    
}

@@ -2700,25 +2700,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -2735,32 +2735,37 @@
 etadata%0A        
+try (
 PDDocument docum
@@ -2801,34 +2801,48 @@
 e(pdfaFilename))
-;%0A
+)%0A        %7B%0A    
         PDDocume
@@ -2900,16 +2900,20 @@
         
+    
 PDMetada
@@ -2941,24 +2941,28 @@
 Metadata();%0A
+    
         DomX
@@ -3010,16 +3010,20 @@
         
+    
 XMPMetad
@@ -3088,16 +3088,20 @@
         
+    
 DublinCo
@@ -3150,24 +3150,28 @@
 ();%0A        
+    
 assertEquals
@@ -3209,30 +3209,8 @@
     
-    document.close();%0A
     
@@ -3215,11 +3215,12 @@
   %7D%0A    
+%7D
 %0A%7D%0A

use try-with-resources