Skip to content

Commit 420e938

Browse files
committed
Add tests to the setModified method
DEVSIX-3430
1 parent 06cae6c commit 420e938

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfNameTreeTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
5353
import com.itextpdf.test.ExtendedITextTest;
5454
import com.itextpdf.test.annotations.type.IntegrationTest;
55+
56+
import java.util.ArrayList;
5557
import org.junit.Assert;
5658
import org.junit.BeforeClass;
5759
import org.junit.Test;
@@ -60,6 +62,7 @@ This file is part of the iText (R) project.
6062
import java.io.ByteArrayInputStream;
6163
import java.io.ByteArrayOutputStream;
6264
import java.io.IOException;
65+
import java.util.Set;
6366
import java.util.Map;
6467

6568
@Category(IntegrationTest.class)
@@ -147,4 +150,65 @@ public void annotationAppearanceTest() throws IOException {
147150
pdfDocument.close();
148151
Assert.assertEquals(1, objs.size());
149152
}
153+
154+
@Test
155+
public void setModifiedFlagTest() throws IOException {
156+
testSetModified(false);
157+
}
158+
159+
@Test
160+
public void setModifiedFlagAppendModeTest() throws IOException {
161+
testSetModified(true);
162+
}
163+
164+
private static void testSetModified(boolean isAppendMode) throws IOException {
165+
String[] expectedKeys = {
166+
"new_key1",
167+
"new_key2",
168+
"new_key3",
169+
};
170+
171+
ByteArrayOutputStream sourceFile = createDocumentInMemory();
172+
ByteArrayOutputStream modifiedFile = new ByteArrayOutputStream();
173+
PdfReader reader = new PdfReader(new ByteArrayInputStream(sourceFile.toByteArray()));
174+
PdfDocument pdfDoc = isAppendMode
175+
? new PdfDocument(reader, new PdfWriter(modifiedFile), new StampingProperties().useAppendMode())
176+
: new PdfDocument(reader, new PdfWriter(modifiedFile));
177+
PdfNameTree nameTree = pdfDoc.getCatalog().getNameTree(PdfName.Dests);
178+
Map<String, PdfObject> names = nameTree.getNames();
179+
ArrayList<String> keys = new ArrayList<>(names.keySet());
180+
181+
for (int i = 0; i < keys.size(); i++) {
182+
names.put(expectedKeys[i], names.get(keys.get(i)));
183+
names.remove(keys.get(i));
184+
}
185+
186+
nameTree.setModified();
187+
188+
pdfDoc.close();
189+
190+
reader = new PdfReader(new ByteArrayInputStream(modifiedFile.toByteArray()));
191+
pdfDoc = new PdfDocument(reader);
192+
nameTree = pdfDoc.getCatalog().getNameTree(PdfName.Dests);
193+
Set<String> actualKeys = nameTree.getNames().keySet();
194+
195+
Assert.assertArrayEquals(expectedKeys, actualKeys.toArray());
196+
}
197+
198+
private static ByteArrayOutputStream createDocumentInMemory() {
199+
ByteArrayOutputStream boas = new ByteArrayOutputStream();
200+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(boas));
201+
202+
pdfDoc.addNewPage();
203+
pdfDoc.getCatalog().getNameTree(PdfName.Dests).addEntry("key1",
204+
new PdfArray(new float[] {0, 0, 0, 0}));
205+
pdfDoc.getCatalog().getNameTree(PdfName.Dests).addEntry("key2",
206+
new PdfArray(new float[] {1, 1, 1, 1}));
207+
pdfDoc.getCatalog().getNameTree(PdfName.Dests).addEntry("key3",
208+
new PdfArray(new float[] {2, 2, 2, 2}));
209+
210+
pdfDoc.close();
211+
212+
return boas;
213+
}
150214
}

0 commit comments

Comments
 (0)