5.3.  Validate all documents in a folder

All tests on file folders start with the following methods:

File folder = new File(folderName);
AssertThat.eachDocument()                  1 
          .inFolder(folder)                2 
          .passedFilter(filter)            3
          ...
;

1 2

These two methods are the entry into any test which uses all files in the given folder that have the '.pdf' file type extension. The extension comparison is case-insensitive.

3

An optional filter can be used to reduce the number of documents, and multiple filters can be concatenated. Without a filter, all PDF documents in the folder will be checked.

Once one document in the folder fails a test, the test ends for all documents in that folder.

If the used filters filter out every document, an error message is thrown. An error message is also thrown if a folder does not contain any PDF documents.

Example - Validate all Documents in a Folder

The following example checks whether all PDF documents in the given folder comply with the ZUGFeRD specification version 1.0:

@Test
public void filesInFolderCompliesZugferdSpecification() throws Exception {
  String folderName = PATH + "zugferd10";
  File folder = new File(folderName);
  AssertThat.eachDocument()
            .inFolder(folder)
            .compliesWith()
            .zugferdSpecification(VERSION10)
  ;
}

Example - Filter Out Documents by Name

In the next example all PDF documents with 'pdfunit-perl' in their names are selected. The title of these documents is then validated:

@Test
public void validateFilteredFilesInFolder() throws Exception {
  File folder = new File(PATH);
  FilenameFilter allPdfunitFiles = new FilenameMatchingFilter(".*pdfunit-perl.*");
  AssertThat.eachDocument()
            .inFolder(folder)
            .passedFilter(allPdfunitFiles) 
            .hasProperty("Title").equalsTo("PDFUnit - Automated PDF Tests")
  ;
}

There are two kinds of filters. The filter FilenameContainingFilter checks whether a file name (including the path) contains a substring, and the filter FilenameMatchingFilter evaluates whether it matches a regular expression. The regular expression should always begin with '.*'.