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());
}
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());
}
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();
}
}
}
Aggregations