Search in sources :

Example 6 with SyntaxValidationException

use of org.apache.pdfbox.preflight.exception.SyntaxValidationException in project pdfbox by apache.

the class TestValidDirectory method validate.

@Test
public void validate() throws Exception {
    PreflightDocument document = null;
    System.out.println(target);
    ValidationResult result = null;
    try {
        PreflightParser parser = new PreflightParser(target);
        parser.parse();
        document = (PreflightDocument) parser.getPDDocument();
        document.validate();
        result = document.getResult();
    } catch (SyntaxValidationException e) {
        result = e.getResult();
    } finally {
        if (document != null) {
            document.close();
        }
    }
    Assert.assertTrue("Validation of " + target, result.isValid());
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) Test(org.junit.Test)

Example 7 with SyntaxValidationException

use of org.apache.pdfbox.preflight.exception.SyntaxValidationException in project pdfbox by apache.

the class TestInvalidDirectory method validate.

@Test
public void validate() throws Exception {
    PreflightDocument document = null;
    System.out.println(target);
    ValidationResult result = null;
    try {
        PreflightParser parser = new PreflightParser(target);
        parser.parse();
        document = (PreflightDocument) parser.getPDDocument();
        document.validate();
        result = document.getResult();
    } catch (SyntaxValidationException e) {
        result = e.getResult();
    } finally {
        if (document != null) {
            document.close();
        }
    }
    Assert.assertFalse("Test of " + target, result.isValid());
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) Test(org.junit.Test)

Example 8 with SyntaxValidationException

use of org.apache.pdfbox.preflight.exception.SyntaxValidationException in project pdfbox by apache.

the class TestIsartorBavaria method validate.

@Test()
public void validate() throws Exception {
    PreflightDocument document = null;
    try {
        ValidationResult result;
        try {
            PreflightParser parser = new PreflightParser(file);
            parser.parse();
            document = (PreflightDocument) parser.getPDDocument();
            // to speeds up tests, skip validation of page count is over the limit
            if (document.getNumberOfPages() < 8191) {
                document.validate();
            }
            result = document.getResult();
        } catch (SyntaxValidationException e) {
            result = e.getResult();
        }
        if (this.expectedErrorSet.isEmpty()) {
            Set<String> errorSet = new HashSet<>();
            for (ValidationError error : result.getErrorsList()) {
                errorSet.add(error.getErrorCode());
            }
            StringBuilder message = new StringBuilder();
            message.append(file.getName());
            message.append(" : PDF/A file should be valid, but has error");
            if (errorSet.size() > 1) {
                message.append('s');
            }
            message.append(':');
            for (String errMsg : errorSet) {
                message.append(' ');
                message.append(errMsg);
            }
            assertTrue(message.toString(), result.isValid());
            assertTrue(message.toString(), result.getErrorsList().isEmpty());
        } else {
            assertFalse(file.getName() + " : PDF/A file should be invalid (expected " + this.expectedErrorSet + ")", // TODO
            result.isValid());
            assertTrue(file.getName() + " : Should find at least one error", result.getErrorsList().size() > 0);
            // each expected error should occur
            boolean logged = false;
            boolean allFound = true;
            for (String expectedError : this.expectedErrorSet) {
                boolean oneFound = false;
                for (ValidationError error : result.getErrorsList()) {
                    if (error.getErrorCode().equals(expectedError)) {
                        oneFound = true;
                    }
                    if (isartorResultFile != null && !logged) {
                        String log = file.getName().replace(".pdf", "") + "#" + error.getErrorCode() + "#" + error.getDetails() + "\n";
                        isartorResultFile.write(log.getBytes());
                    }
                }
                if (!oneFound) {
                    allFound = false;
                    break;
                }
                // log only in the first inner loop
                logged = true;
            }
            if (!allFound) {
                Set<String> errorSet = new HashSet<>();
                for (ValidationError error : result.getErrorsList()) {
                    errorSet.add(error.getErrorCode());
                }
                StringBuilder message = new StringBuilder();
                for (String errMsg : errorSet) {
                    if (message.length() > 0) {
                        message.append(", ");
                    }
                    message.append(errMsg);
                }
                fail(String.format("%s : Invalid error code returned. Expected %s, found [%s]", file.getName(), expectedErrorSet, message.toString().trim()));
            }
        // if each of the expected errors are found, we consider test valid
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SyntaxValidationException (org.apache.pdfbox.preflight.exception.SyntaxValidationException)8 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 IOException (java.io.IOException)4 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)4 Test (org.junit.Test)3 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)2 ValidationResult (org.apache.pdfbox.preflight.ValidationResult)2 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSObject (org.apache.pdfbox.cos.COSObject)1 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)1 COSStream (org.apache.pdfbox.cos.COSStream)1 COSString (org.apache.pdfbox.cos.COSString)1 PDFObjectStreamParser (org.apache.pdfbox.pdfparser.PDFObjectStreamParser)1 Format (org.apache.pdfbox.preflight.Format)1 Element (org.w3c.dom.Element)1