-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PdfStructureTreeRoot update - allowing to save Object References
- Loading branch information
Showing
2 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
openpdf/src/test/java/com/lowagie/text/pdf/PdfStructureTreeRootTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.lowagie.text.pdf; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PdfStructureTreeRootTest { | ||
|
||
@Test | ||
void shouldCreateNewInstanceSuccessfully() { | ||
PdfWriter writer = mock(PdfWriter.class); | ||
when(writer.getPdfIndirectReference()).thenReturn(mock(PdfIndirectReference.class)); | ||
|
||
PdfStructureTreeRoot root = new PdfStructureTreeRoot(writer); | ||
|
||
assertNotNull(root); | ||
assertEquals(PdfName.STRUCTTREEROOT, root.get(PdfName.TYPE)); | ||
assertNotNull(root.getReference()); | ||
assertSame(writer, root.getWriter()); | ||
} | ||
|
||
@Test | ||
void shouldMapUserTagToStandardTag() { | ||
PdfStructureTreeRoot root = new PdfStructureTreeRoot(mock(PdfWriter.class)); | ||
PdfName userTag = new PdfName("MyTag"); | ||
PdfName standardTag = PdfName.H1; | ||
|
||
root.mapRole(userTag, standardTag); | ||
|
||
PdfDictionary roleMap = (PdfDictionary) root.get(PdfName.ROLEMAP); | ||
assertNotNull(roleMap); | ||
assertEquals(standardTag, roleMap.get(userTag)); | ||
} | ||
|
||
@Test | ||
void getWriterShouldReturnCorrectWriter() { | ||
PdfWriter mockWriter = mock(PdfWriter.class); | ||
PdfStructureTreeRoot treeRoot = new PdfStructureTreeRoot(mockWriter); | ||
|
||
PdfWriter result = treeRoot.getWriter(); | ||
|
||
assertSame(mockWriter, result); | ||
} | ||
|
||
@Test | ||
void addExistingObjectShouldIncreaseParentTreeNextKey() { | ||
PdfWriter mockWriter = mock(PdfWriter.class); | ||
PdfStructureTreeRoot treeRoot = new PdfStructureTreeRoot(mockWriter); | ||
PdfIndirectReference mockRef = mock(PdfIndirectReference.class); | ||
|
||
int firstKey = treeRoot.addExistingObject(mockRef); | ||
int secondKey = treeRoot.addExistingObject(mockRef); | ||
|
||
assertNotEquals(firstKey, secondKey); | ||
assertEquals(firstKey + 1, secondKey); | ||
} | ||
|
||
@Test | ||
void buildTreeShouldGenerateParentTreeWithoutException() throws IOException { | ||
PdfWriter mockWriter = mock(PdfWriter.class); | ||
when(mockWriter.addToBody(any(PdfObject.class))).thenAnswer(invocation -> { | ||
PdfObject arg = invocation.getArgument(0); | ||
return new PdfIndirectObject(0, arg, mockWriter); | ||
}); | ||
|
||
PdfStructureTreeRoot treeRoot = new PdfStructureTreeRoot(mockWriter); | ||
|
||
assertDoesNotThrow(() -> treeRoot.buildTree()); | ||
} | ||
|
||
@Test | ||
void buildTreeShouldHandleIOException() throws IOException { | ||
PdfWriter mockWriter = mock(PdfWriter.class); | ||
doThrow(IOException.class).when(mockWriter).addToBody(any(PdfObject.class)); | ||
|
||
PdfStructureTreeRoot treeRoot = new PdfStructureTreeRoot(mockWriter); | ||
treeRoot.setPageMark(1, mock(PdfIndirectReference.class)); | ||
|
||
assertThrows(IOException.class, () -> treeRoot.buildTree()); | ||
} | ||
} |