Skip to content

Commit dafa9c8

Browse files
SnipxiText-CI
authored andcommitted
Add new test that demonstrates bug of PdfReader locking the file when exception occurred during parsing
DEVSIX-2133
1 parent 6a385a1 commit dafa9c8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.io.LogMessageConstant;
4646
import com.itextpdf.io.source.ByteArrayOutputStream;
4747
import com.itextpdf.io.source.ByteUtils;
48+
import com.itextpdf.io.util.FileUtil;
49+
import com.itextpdf.io.util.MessageFormatUtil;
4850
import com.itextpdf.kernel.PdfException;
4951
import com.itextpdf.kernel.utils.CompareTool;
5052
import com.itextpdf.test.ExtendedITextTest;
@@ -59,8 +61,9 @@ This file is part of the iText (R) project.
5961
import org.junit.experimental.categories.Category;
6062
import org.junit.rules.ExpectedException;
6163

64+
import java.io.File;
6265
import java.io.IOException;
63-
import com.itextpdf.io.util.MessageFormatUtil;
66+
import java.io.OutputStream;
6467
import java.util.ArrayList;
6568
import java.util.HashMap;
6669
import java.util.List;
@@ -1638,6 +1641,24 @@ public void hugeInvalidIndRefObjNumberTest() throws IOException {
16381641
pdfDoc.close();
16391642
}
16401643

1644+
@Test
1645+
@Ignore("DEVSIX-2133")
1646+
public void testFileIsNotLockedOnException() throws IOException {
1647+
File nonPdfFileName = new File(sourceFolder + "text_file.txt");
1648+
Assert.assertTrue(nonPdfFileName.exists());
1649+
boolean exceptionThrown = false;
1650+
try {
1651+
PdfReader reader = new PdfReader(nonPdfFileName);
1652+
} catch (com.itextpdf.io.IOException e) {
1653+
exceptionThrown = true;
1654+
1655+
// File should be available for writing
1656+
OutputStream stream = FileUtil.getFileOutputStream(nonPdfFileName);
1657+
stream.write(new byte[] {0});
1658+
}
1659+
Assert.assertTrue(exceptionThrown);
1660+
}
1661+
16411662
private boolean objectTypeEqualTo(PdfObject object, PdfName type) {
16421663
PdfName objectType = ((PdfDictionary) object).getAsName(PdfName.Type);
16431664
return type.equals(objectType);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

0 commit comments

Comments
 (0)