Skip to content

Commit

Permalink
Update test and use PdfBox to provide additional validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrwelch committed Jul 1, 2024
1 parent 4d4f028 commit 5e602e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!")

}

Expand Down

0 comments on commit 5e602e9

Please sign in to comment.