3.7.  Dates

Overview

It's unusual to have to test a PDF document's creation or modification date. But when you do it, it's not easy because dates can be formatted in many different ways. PDFUnit tries to hide the complexity and provides a wide range of functions:

// Date existence tests:
.hasCreationDate()
.hasNoCreationDate()
.hasModificationDate()
.hasNoModificationDate()

// Date value tests:
.hasCreationDate().after(..) 
.hasCreationDate().before(..) 
.hasCreationDate().equalsTo(..)
.hasModificationDate().after(..) 
.hasModificationDate().before(..) 
.hasModificationDate().equalsTo(..)

The following listings only show tests for the creation date because tests for the modification date have exactly the same syntax.

Existence of a Date

The first test checks that a creation date exists:

@Test
public void hasCreationDate() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasCreationDate()
  ;
}

You can verify that your PDF document has no creation date like this:

@Test
public void hasCreationDate_NoDateInPDF() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasNoCreationDate()
  ;
}

The next chapter compares a date value with an expected date.

Date Resolution

The expected date has to be an instance of java.util.Calendar. Additionally, the date resolution has to be declared, saying which parts of an existing date are parts to be compared with the expected date.

Using the enumeration DateResolution.DATE day, month and year are used for comparison. When using the DateResolution.DATETIME hours, minutes and seconds are also compared. Both enumerations exist as constants:

// Constants for date resolution:

com.pdfunit.Constants.AS_DATE
com.pdfunit.Constants.DateResolution AS_DATETIME

A test looks like this:

@Test
public void hasCreationDate_WithValue() throws Exception {
  String filename = "documentUnderTest.pdf";
  Calendar expectedCreationDate = 
           DateHelper.getCalendar("20131027_17:24:17", "yyyyMMdd_HH:mm:ss"); 1
  
  AssertThat.document(filename)
            .hasCreationDate()
            .equalsTo(expectedCreationDate, AS_DATE)   2
  ;
}

1

The utility com.pdfunit.util.DateHelper can be used to create an instance of java.util.Calender for the expected date.

2

The constants ard defined in the enumeration com.pdfunit.DateResolution.

Formatted dates are generally difficult to handle by programs. That's why PDFUnit uses an instance of java.util.Calendar. That makes the date processing independent from formats. If PDFUnit has problems converting the PDF-internal date value into an instance of the class Calendar, you can check the date as follows:

@Test
public void hasCreationDate() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasProperty("CreationDate").startingWith("D:20131027")
            .hasProperty("CreationDate").equalsTo("D:20131027172417+01'00'")
  ;
}

You can find the date format of an existing PDF document when you open it with a simple text editor. Search for the string "CreationDate".

Date Tests with Upper and Lower Limit

You can check that a PDF document's creation date is later or earlier than a given date:

@Test
public void hasCreationDate_Before() throws Exception {
  String filename = "documentUnderTest.pdf";
  Calendar creationDateUpperLimit = DateHelper.getCalendar("20991231", "yyyyMMdd"); 
  
  AssertThat.document(filename)
            .hasCreationDate()
            .before(creationDateUpperLimit, AS_DATE) 1
  ;
}
@Test
public void hasCreationDate_After() throws Exception {
  String filename = "documentUnderTest.pdf";
  Calendar creationDateLowerLimit = DateHelper.getCalendar("19990101", "yyyyMMdd"); 
  
  AssertThat.document(filename)
            .hasCreationDate()
            .after(creationDateLowerLimit, AS_DATE) 1
  ;
}

1 1

The lower- or upper-limits are not included in the expected date range.

Creation Date of a Signature

When a PDF document is signed, the date of the signature can be tested. Chapter 3.28: “Signed PDF” covers tests for that.