use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.
the class TestValidFiles method validate.
@Test()
public void validate() throws Exception {
if (path == null) {
logger.warn("This is an empty test");
return;
}
PreflightDocument document = null;
try {
PreflightParser parser = new PreflightParser(path);
parser.parse();
document = parser.getPreflightDocument();
document.validate();
ValidationResult result = document.getResult();
Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")", result.isValid());
Assert.assertTrue(path + " : Should find at least one error", result.getErrorsList().size() > 0);
// could contain more than one error
if (result.getErrorsList().size() > 0) {
Assert.fail("File expected valid : " + path.getAbsolutePath());
}
} catch (ValidationException e) {
throw new Exception(path + " :" + e.getMessage(), e);
} finally {
if (document != null) {
document.close();
}
}
}
use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.
the class TestMetadataFiles method checkPDF.
private boolean checkPDF(File pdf) {
PreflightDocument document = null;
boolean testResult = false;
if (pdf.exists()) {
ValidationResult result = null;
try {
PreflightParser parser = new PreflightParser(pdf);
parser.parse();
document = (PreflightDocument) parser.getPDDocument();
document.validate();
result = document.getResult();
} catch (SyntaxValidationException e) {
result = e.getResult();
} catch (IOException e) {
fail("An exception occured while parsing the PDF " + pdf + ": " + e);
}
if (result != null) {
testResult = result.isValid();
}
if (document != null) {
try {
document.close();
} catch (IOException e) {
// shouldn't happen;
}
}
} else {
fail("Can't find the given file " + pdf);
}
return testResult;
}
use of org.apache.pdfbox.preflight.parser.PreflightParser 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());
}
use of org.apache.pdfbox.preflight.parser.PreflightParser 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());
}
use of org.apache.pdfbox.preflight.parser.PreflightParser 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();
}
}
}
Aggregations