Skip to content

Commit

Permalink
Fix #318 Small unit test checking that a single paragraph should crea…
Browse files Browse the repository at this point in the history
…te a single page.

fixes #318
  • Loading branch information
asturio committed Feb 4, 2021
1 parent 7155362 commit aeb1eb2
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.librepdf.openpdf.independent;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class NumberOfPagesTest {

@Test
void whenWritingHelloWorld_thenOnlyOnePageShouldBeCreated() throws IOException {
// GIVEN
// buffer for reading the document afterwards
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
// step 1: create a writer that listens to the document and writes to outputBuffer
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputBuffer);
// step 2: open the document
document.open();
// step 3: add a paragraph to the document
document.add(new Paragraph("Hello World"));
// step 4: we close the document
int pagesWritten = writer.getCurrentPageNumber(); // get page "count" just before the closed
document.close();
// WHEN
// step 5 Read it back and count pages
PdfReader reader = new PdfReader(outputBuffer.toByteArray());
final int pagesRead = reader.getNumberOfPages();
// THEN
Assertions.assertThat(pagesWritten)
.isEqualTo(1)
.isEqualTo(pagesRead);
}
}

0 comments on commit aeb1eb2

Please sign in to comment.