Skip to content

Commit 3ef8a39

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add new PdfStreamTest class, new PdfStructTreeRootTest#severalSameElementsInStructTreeRootTest
DEVSIX-3518 Autoported commit. Original commit hash: [f7a0ca816]
1 parent 7ede824 commit 3ef8a39

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfStreamTest.cs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ source product.
4141
4242
*/
4343
using System;
44+
using iText.IO.Util;
4445
using iText.Kernel.Pdf.Xobject;
4546
using iText.Kernel.Utils;
4647
using iText.Test;
@@ -53,8 +54,8 @@ public class PdfStreamTest : ExtendedITextTest {
5354
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
5455
+ "/test/itext/kernel/pdf/PdfStreamTest/";
5556

56-
[NUnit.Framework.SetUp]
57-
public virtual void Before() {
57+
[NUnit.Framework.OneTimeSetUp]
58+
public static void Before() {
5859
CreateOrClearDestinationFolder(destinationFolder);
5960
}
6061

@@ -89,5 +90,77 @@ public virtual void RunLengthEncodingTest01() {
8990
NUnit.Framework.Assert.AreEqual(imgBytes1, cmpImgBytes1);
9091
NUnit.Framework.Assert.AreEqual(imgBytes2, cmpImgBytes2);
9192
}
93+
94+
[NUnit.Framework.Test]
95+
public virtual void IndirectRefInFilterAndNoTaggedPdfTest() {
96+
String inFile = sourceFolder + "indirectRefInFilterAndNoTaggedPdf.pdf";
97+
String outFile = destinationFolder + "destIndirectRefInFilterAndNoTaggedPdf.pdf";
98+
PdfDocument srcDoc = new PdfDocument(new PdfReader(inFile));
99+
PdfDocument outDoc = new PdfDocument(new PdfReader(inFile), new PdfWriter(outFile));
100+
outDoc.Close();
101+
PdfDocument doc = new PdfDocument(new PdfReader(outFile));
102+
PdfStream outStreamIm1 = doc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
103+
("Im1"));
104+
PdfStream outStreamIm2 = doc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
105+
("Im2"));
106+
PdfStream cmpStreamIm1 = srcDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new
107+
PdfName("Im1"));
108+
PdfStream cmpStreamIm2 = srcDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new
109+
PdfName("Im2"));
110+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStreamIm1, cmpStreamIm1));
111+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStreamIm2, cmpStreamIm2));
112+
srcDoc.Close();
113+
outDoc.Close();
114+
}
115+
116+
[NUnit.Framework.Test]
117+
public virtual void IndirectFilterInCatalogTest() {
118+
// TODO DEVSIX-1193 remove junitExpectedException and expected NullPointerException after fix
119+
String inFile = sourceFolder + "indFilterInCatalog.pdf";
120+
PdfDocument doc = new PdfDocument(new PdfReader(inFile), new PdfWriter(destinationFolder + "indFilterInCatalog.pdf"
121+
));
122+
NUnit.Framework.Assert.That(() => {
123+
doc.Close();
124+
}
125+
, NUnit.Framework.Throws.InstanceOf<NullReferenceException>())
126+
;
127+
}
128+
129+
[NUnit.Framework.Test]
130+
public virtual void IndirectFilterFlushedBeforeStreamTest() {
131+
// TODO DEVSIX-1193 remove junitExpectedException after fix
132+
String inFile = sourceFolder + "indFilterInCatalog.pdf";
133+
String @out = destinationFolder + "indirectFilterFlushedBeforeStreamTest.pdf";
134+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFile), new PdfWriter(@out));
135+
// Simulate the case in which filter is somehow already flushed before stream.
136+
// Either directly by user or because of any other reason.
137+
PdfObject filterObject = pdfDoc.GetPdfObject(6);
138+
filterObject.Flush();
139+
NUnit.Framework.Assert.That(() => {
140+
pdfDoc.Close();
141+
}
142+
, NUnit.Framework.Throws.InstanceOf<NullReferenceException>())
143+
;
144+
}
145+
146+
[NUnit.Framework.Test]
147+
public virtual void IndirectFilterMarkedToBeFlushedBeforeStreamTest() {
148+
// TODO DEVSIX-1193 remove junitExpectedException after fix
149+
String inFile = sourceFolder + "indFilterInCatalog.pdf";
150+
String @out = destinationFolder + "indirectFilterMarkedToBeFlushedBeforeStreamTest.pdf";
151+
PdfWriter writer = new PdfWriter(@out);
152+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFile), writer);
153+
// Simulate the case when indirect filter object is marked to be flushed before the stream itself.
154+
PdfObject filterObject = pdfDoc.GetPdfObject(6);
155+
filterObject.GetIndirectReference().SetState(PdfObject.MUST_BE_FLUSHED);
156+
// The image stream will be marked as MUST_BE_FLUSHED after page is flushed.
157+
pdfDoc.GetFirstPage().GetPdfObject().GetIndirectReference().SetState(PdfObject.MUST_BE_FLUSHED);
158+
NUnit.Framework.Assert.That(() => {
159+
writer.FlushWaitingObjects(JavaCollectionsUtil.EmptySet<PdfIndirectReference>());
160+
pdfDoc.Close();
161+
}
162+
, NUnit.Framework.Throws.InstanceOf<NullReferenceException>())
163+
;
164+
}
92165
}
93166
}

itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfStructTreeRootTest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.Collections.Generic;
4445
using System.IO;
46+
using iText.Kernel.Pdf.Tagging;
4547
using iText.Test;
4648

4749
namespace iText.Kernel.Pdf {
@@ -54,7 +56,7 @@ public class PdfStructTreeRootTest : ExtendedITextTest {
5456

5557
[NUnit.Framework.OneTimeSetUp]
5658
public static void BeforeClass() {
57-
ITextTest.CreateOrClearDestinationFolder(destinationFolder);
59+
CreateOrClearDestinationFolder(destinationFolder);
5860
}
5961

6062
[NUnit.Framework.Test]
@@ -71,5 +73,24 @@ public virtual void DirectStructTreeRootStampingModeTest() {
7173
NUnit.Framework.Assert.IsTrue(document.IsTagged());
7274
document.Close();
7375
}
76+
77+
[NUnit.Framework.Test]
78+
public virtual void SeveralSameElementsInStructTreeRootTest() {
79+
String inFile = sourceFolder + "severalSameElementsInStructTreeRoot.pdf";
80+
PdfDocument doc = new PdfDocument(new PdfReader(inFile), new PdfWriter(new MemoryStream()));
81+
PdfStructTreeRoot structTreeRoot = doc.GetStructTreeRoot();
82+
IList<PdfStructElem> kidsOfStructTreeRootKids = new List<PdfStructElem>();
83+
foreach (IStructureNode kid in structTreeRoot.GetKids()) {
84+
foreach (IStructureNode kidOfKid in kid.GetKids()) {
85+
if (kidOfKid is PdfStructElem) {
86+
kidsOfStructTreeRootKids.Add((PdfStructElem)kidOfKid);
87+
}
88+
}
89+
}
90+
structTreeRoot.Flush();
91+
foreach (PdfStructElem kidsOfStructTreeRootKid in kidsOfStructTreeRootKids) {
92+
NUnit.Framework.Assert.IsTrue(kidsOfStructTreeRootKid.IsFlushed());
93+
}
94+
}
7495
}
7596
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0751f3f9308a21e22d966bccd38274d7e288e21f
1+
f7a0ca8169f8f8229aeece4fdbc9e4ee8786e6b0

0 commit comments

Comments
 (0)