Prev |  | Next

## 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)
      ;
    }

| 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”.

Prev | Up | Next
Chapter 4. Comparing a Test PDF with a Reference | Home | 4.3. Comparing Bookmarks