It does not make sense to compare the entire XFA data of two PDF documents, because often they contain a creation date and a modification data. For this reason PDFUnit provides an XPath based test method for XFA data.
Only one powerful method is provided:
// Method for tests with XMP data
.haveSameXFAData().matchingXPath(XPathExpression)
If you are in doubt about the XFA data, you can extract it with the utility
ExtractXFAData
into an XML file. For more information see chapter
9.11: “Extract XFA Data to XML”.
In the following example, two nodes of the XFA data defined by XPath expressions will be compared. The result of the XPath expression has to be the same for both PDF documents.
@Test public void haveSameXFAData_MultipleDefaultNamespaces() throws Exception { String filenameTest = "documentUnderTest.pdf"; String filenameReference = "reference.pdf"; String nsStringXFATemplate = "http://www.xfa.org/schema/xfa-template/2.6/"; String nsStringXFALocale = "http://www.xfa.org/schema/xfa-locale-set/2.7/"; DefaultNamespace nsXFATemplate = new DefaultNamespace(nsStringXFATemplate); DefaultNamespace nsXFALocale = new DefaultNamespace(nsStringXFALocale); String xpathSubform = "//default:subform/@name[.='movie']"; String xpathLocale = "//default:locale/@name[.='nl_BE']"; XPathExpression exprXFATemplate = new XPathExpression(xpathSubform, nsXFATemplate); XPathExpression exprXFALocale = new XPathExpression(xpathLocale, nsXFALocale); AssertThat.document(filenameTest) .and(filenameReference) .haveSameXFAData() .matchingXPath(exprXFATemplate) .matchingXPath(exprXFALocale) ; }
Be careful using the default namespace. It has to be declared in the attribute
<haveSameXFAData />
. Other namespaces will be
detected automatically.
You can use the method matchingXPath(..)
more than once in a
test. But it would be better to split the test.
PDFUnit throws an exception if two documents without any XFA data are compared. It makes no sense to compare things that don't exist. A detailed description of how to work with XPath in PDFUnit can be found in chapter 13.11: “Using XPath”.
The XPath-expressions may contain XPath functions:
@Test public void haveSameXFAData() throws Exception { String filenameTest = "documentUnderTest.pdf"; String filenameReference = "reference.pdf"; DefaultNamespace defaultNS = new DefaultNamespace("http://www.xfa.org/schema/xfa-template/2.6/"); XPathExpression expression = new XPathExpression("count(//default:field) = 3", defaultNS); AssertThat.document(filenameTest) .and(filenameReference) .haveSameXFAData() .matchingXPath(expression) ; }