4.2.  Comparing Attachments

Quantity

Compare the number of attachments in two documents:

@Test
public void haveSameNumberOfEmbeddedFiles() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference)
            .haveSameNumberOfEmbeddedFiles()
  ;
}

Name and Content

There is a parameterized test method to compare the attachments by name or by content:

@Test
public void haveSameEmbeddedFiles() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference) 
            .haveSameEmbeddedFiles(COMPARED_BY_NAME)
            .haveSameEmbeddedFiles(COMPARED_BY_CONTENT)  1
  ;
}

1

The attachments are compared byte-by-byte. So, two files of any type can be compared.

The two constants are defined in com.pdfunit.Constants:

// Constants defining the kind comparing embedded files:

com.pdfunit.Constants.COMPARED_BY_CONTENT
com.pdfunit.Constants.COMPARED_BY_NAME

You can use the utility ExtractEmbeddedFiles to extract the attachments. See chapter 9.4: “Extract Attachments”.