Date values in PDF documents are normally not a matter to be tested. But when you want to test them anyway, it is not easy because date values are formatted in many different ways.
Tests in the following listings only handle the creation date because tests for the modification date have exactly the same syntax.
<!-- Tags to test date values: --> <hasCreationDate /> <hasCreationDateAfter /> <hasCreationDateBefore /> <hasModificationDate /> <hasModificationDateAfter /> <hasModificationDateBefore /> <hasNoCreationDate /> <hasNoModificationDate />
The first test checks that a creation date exists.
<testcase name="hasCreationDate"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasCreationDate /> </assertThat> </testcase>
You can verify that your PDF document has no creation date like this:
<testcase name="hasCreationDate_NoDateInPDF"> <assertThat testDocument="documentInfo/documentInfo_noDateFields.pdf"> <hasNoCreationDate /> </assertThat> </testcase>
You have to use the attribute resolution
to declare which
parts of a date should be used for the test.
The attribute can have the values resolution="DATE"
or
resolution="DATETIME"
. Using the constant DATE
date values were compared using year, month and day. And using
DATETIME
hour, minute and second are alos considered.
Here an example:
<testcase name="hasCreationDate_DateResolution"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasCreationDate withDate="2013-05-05" resolution="DATE" /> </assertThat> </testcase>
<testcase name="hasCreationDate_DateTimeResolution"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasCreationDate withDate="2013-05-05T09:33:47" resolution="DATETIME" /> </assertThat> </testcase>
The format of the expected date is defined by XML Schema (xs:date
).
But the PDF internal date format may be different, so a suitable format string
for the internal date has to be declared in the file “config.properties”.
When you omit the attribute resolution=".."
the default resolution="DATE"
is used.
Date formats in PDF documents vary widely range depending on the PDF generating tool.
That is why you can declare it in the file config.properties
:
################################################################### # Declaring the default format for dates in PDF documents. # Use the format strings according to java.util.SimpleDateFormat. ################################################################### # Using date only: #dateformat = 'D:'yyyyMMdd # Using date and time: dateformat = 'D:'yyyyMMddHHmmss
Beware: When you define the format dateformat = 'D:'yyyy
then
“January 1” is assumed for date and month.
That is probably not what you intended.
You can only define one date format in the config file.
But if you want to check a PDF document with another date format, you
can test it as a property using the tag <hasProperty />
:
<testcase name="hasProperty_CreationDate"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasProperty name="CreationDate"> <matchingComplete>D:20131027172417+01'00'</matchingComplete> </hasProperty> <hasProperty name="CreationDate"> <startingWith>D:20131027</startingWith> </hasProperty> </assertThat> </testcase>
You can check that a PDF document's creation date is later or earlier than a given date:
<testcase name="hasCreationDate_Before"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasCreationDateBefore withDate="2099-01-01" resolution="DATE" /> </assertThat> </testcase>
<testcase name="hasCreationDate_After"> <assertThat testDocument="documentInfo/documentInfo_allInfo.pdf"> <hasCreationDateAfter withDate="1999-01-01" resolution="DATE" /> </assertThat> </testcase>
The lower- or upper-limits are not included in the expected date range.
Beside creation and modification date PDF documents contain the date their certificates were issued. Chapter 3.23: “Signatures and Certificates” covers tests for that.