Chapter 3. Test Scopes

3.1. Overview

An Introduction to the Syntax

Each test for a single PDF document begins with the method AssertThat.document(..). The file to be tested is passed as a parameter. Then each following function opens a different test scope, e.g. content, fonts, layouts, etc.:

// Instantiation of PDFUnit for a single document:

AssertThat.document(filename)          13.1: “Instantiation of PDF Documents” 
          ...                  
// Switch to one of many test scopes. 

// Compare one PDF with a reference:
AssertThat.document(filename)         1
          .and(..)                     4.1: “Overview”  
          ...

1

The PDF document can be passed to the function as a String, File, InputStream, URL or byte[].

It is possible to write test for a given set of PDF documents. Such tests start with the method AssertThat.eachDocument(..):

// Instantiation of PDFUnit for multiple documents:
...
File[] files = {file1, file2, file3};
AssertThat.eachDocument(filename)   1  5: “Folders and Multiple Documents” 
          .hasText(..)    
          .containing(..)    
;

1

The PDF documents can be passed to the function as a String[], File[], InputStream[], or URL[].

A test can also cover all PDF documents in a folder. Such tests start with the method AssertThat.eachDocument().inFolder(..):

// Instantiation of PDFUnit for a folder:
...
File folderToCheck = new File(..);
AssertThat.eachDocument()
          .inFolder(folderToCheck) 1  5.3: “Validate all documents in a folder” 
          .hasText(..)    
          .containing(..)    
;

1

All PDF documents in this folder will be checked. PDF documents in subfolders will not be checked.

Exception

Tests that expect an exception have to catch the class PDFUnitValidationException.

Here is an example for a test which expects an exception:

@Test(expected=PDFUnitValidationException.class)
public void isSigned_DocumentNotSigned() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)
            .isSigned()
  ;
}

Test Scopes

The following list gives a complete overview over the test scopes of PDFUnit. The link behind each method points to the chapter where the test scope is described:

// Every one of the following methods opens a new test scope:
  
.asRenderedPage()                3.20: “Layout - Entire PDF Pages” 

.containsImage(..)               3.16: “Images in PDF Documents” 
.containsOneImageOf(..)          3.16: “Images in PDF Documents” 

.hasAuthor()                     3.9: “Document Properties” 
.hasBookmark()                   3.5: “Bookmarks and Named Destinations” 
.hasBookmarks()                  3.5: “Bookmarks and Named Destinations” 
.hasCreationDate()               3.7: “Dates” 
.hasCreator()                    3.7: “Dates” 
.hasEmbeddedFile(..)             3.3: “Attachments” 
.hasEncryptionLength(..)         3.24: “Passwords” 
.hasField(..)                    3.13: “Form Fields” 
.hasFields(..)                   3.13: “Form Fields” 
.hasFont()                       3.12: “Fonts” 
.hasFonts()                      3.12: “Fonts” 
.hasFormat(..)                   3.15: “Format” 
.hasImage(..)                    3.16: “Images in PDF Documents” 
.hasJavaScript()                 3.17: “JavaScript” 
.hasJavaScriptAction()           3.2: “Actions” 
.hasKeywords()                   3.9: “Document Properties” 
.hasLanguageInfo(..)             3.18: “Language” 
.hasLayer()                      3.19: “Layers” 
.hasLayers()                     3.19: “Layers” 
.hasLessPagesThan()              3.23: “Page Numbers as Objectives” 
.hasLocalGotoAction()            3.2: “Actions” 
.hasModificationDate()           3.7: “Dates” 
.hasMorePagesThan()              3.23: “Page Numbers as Objectives” 
.hasNamedDestination()           3.5: “Bookmarks and Named Destinations” 

.hasNoAuthor()                   3.9: “Document Properties” 
.hasNoCreationDate()             3.7: “Dates” 
.hasNoCreator()                  3.9: “Document Properties” 
.hasNoImage()                    3.16: “Images in PDF Documents” 
.hasNoKeywords()                 3.9: “Document Properties” 
.hasNoLanguageInfo()             3.18: “Language” 
.hasNoModificationDate()         3.7: “Dates” 
.hasNoProducer()                 3.9: “Document Properties” 
.hasNoProperty(..)               3.9: “Document Properties” 
.hasNoSubject()                  3.9: “Document Properties” 
.hasNoText()                     3.30: “Text” 
.hasNoTitle()                    3.9: “Document Properties” 
.hasNoXFAData()                  3.37: “XFA Data” 
.hasNoXMPData()                  3.38: “XMP Data” 

.hasNumberOf...()                3.22: “Number of PDF Elements” 

.hasOCG()                        3.19: “Layers” 
.hasOCGs()                       3.19: “Layers” 
.hasOwnerPassword(..)            3.24: “Passwords” 
.hasPermission()                 3.26: “Permissions” 
.hasProducer()                   3.9: “Document Properties” 
.hasProperty(..)                 3.9: “Document Properties” 
.hasSignatureField(..)           3.28: “Signed PDF” 
.hasSignatureFields()            3.28: “Signed PDF” 
.hasSubject()                    3.9: “Document Properties” 
.hasText()                       3.30: “Text” 
.hasTitle()                      3.9: “Document Properties” 
.hasUserPassword(..)             3.24: “Passwords” 
.hasVersion()                    3.36: “Version Info” 
.hasXFAData()                    3.37: “XFA Data” 
.hasXMPData()                    3.38: “XMP Data” 
.hasZugferdData()                3.39: “ZUGFeRD” 

.haveSame...()                   4.1: “Overview” 

.isCertified()                   3.6: “Certified PDF” 
.isCertifiedFor(..)              3.6: “Certified PDF” 
.isLinearizedForFastWebView()    3.11: “Fast Web View” 
.isSigned()                      3.28: “Signed PDF” 
.isSignedBy(..)                  3.28: “Signed PDF” 
.isTagged()                      3.29: “Tagged Documents” 

.restrictedTo(..)                13.2: “Page Selection” 

Sometimes, two methods are needed to enter a test scope:

// Validation of bar code and QR code:
.hasImage().withBarcode()                 3.4: “Bar Code” 
.hasImage().withQRcode()                  3.27: “QR Code” 

// Validation based on Excel files:
.compliesWith().constraints(excelRules)   3.10: “Excel Files for Validation Constraints” 

// Validation of DIN5008 constraints:
.compliesWith().din5008FormA()            3.8: “DIN 5008” 
.compliesWith().din5008FormB()            3.8: “DIN 5008” 
.compliesWith().pdfStandard()             3.25: “PDF/A” 

// Validation around ZUGFeRD:
.compliesWith().zugferdSpecification(..)  3.39: “ZUGFeRD” 

PDFUnit is continuously being improved and the manual kept up to date. Wishes and requests for new functions are appreciated. Please, send them to info[at]pdfunit.com.