From 5e602e9b24342aff36e3d08fe8e1b28e0214d0f9 Mon Sep 17 00:00:00 2001 From: James Welch Date: Mon, 1 Jul 2024 12:49:49 +0100 Subject: [PATCH] Update test and use PdfBox to provide additional validation --- dependencies.gradle | 1 + .../dita/processor/DitaProcessorSpec.groovy | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 8966f61..f7bd2a0 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -94,6 +94,7 @@ dependencies { // runtimeOnly group: 'ant-contrib', name: 'ant-contrib', version: '0.6' + testImplementation 'org.apache.pdfbox:pdfbox:2.0.27' // For the html helper functions api group: 'net.sf.jtidy', name: 'jtidy', version: 'r938' diff --git a/src/test/groovy/uk/ac/ox/softeng/dita/processor/DitaProcessorSpec.groovy b/src/test/groovy/uk/ac/ox/softeng/dita/processor/DitaProcessorSpec.groovy index 181e970..8e94017 100644 --- a/src/test/groovy/uk/ac/ox/softeng/dita/processor/DitaProcessorSpec.groovy +++ b/src/test/groovy/uk/ac/ox/softeng/dita/processor/DitaProcessorSpec.groovy @@ -21,11 +21,16 @@ import uk.ac.ox.softeng.maurodatamapper.dita.DitaProject import uk.ac.ox.softeng.maurodatamapper.dita.elements.langref.base.DitaMap import uk.ac.ox.softeng.maurodatamapper.dita.elements.langref.base.Topic import uk.ac.ox.softeng.maurodatamapper.dita.processor.DitaProcessor + +import groovy.util.logging.Slf4j +import org.apache.pdfbox.pdmodel.PDDocument +import org.apache.pdfbox.text.PDFTextStripper import spock.lang.Specification import java.nio.file.Files import java.nio.file.Paths +@Slf4j class DitaProcessorSpec extends Specification{ DitaProcessor ditaProcessor @@ -61,11 +66,24 @@ class DitaProcessorSpec extends Specification{ } byte[] fileContents = ditaProcessor.generatePdf(ditaProject) + log.debug("File contents size: ${fileContents.size()}") + then: + fileContents.size() > 7700 // The number of bytes of the generated pdf file + fileContents.size() < 7800 + + + + when: // Use PdfBox to validate the PDF + Files.write(Paths.get('build/tmp/pdftest.pdf'), fileContents) + PDDocument pdDocument = PDDocument.load(new File('build/tmp/pdftest.pdf')) + PDFTextStripper pdfStripper = new PDFTextStripper() + String text = pdfStripper.getText(pdDocument) + log.debug(text) - System.err.println("File contents size: ${fileContents.size()}") then: - fileContents.size() == 7771 // The number of bytes of the generated pdf file + text.contains("My first topic") + text.contains("Hello, World!") }