[bug-69628] add test case

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1924643 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2025-03-26 20:52:10 +00:00
parent f90274e518
commit 9a6bd3902d
3 changed files with 49 additions and 19 deletions

View File

@ -44,7 +44,11 @@ public final class ZipArchiveFakeEntry extends ZipArchiveEntry implements Closea
private static int MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE; private static int MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE;
public static void setMaxEntrySize(int maxEntrySize) { public static void setMaxEntrySize(int maxEntrySize) {
MAX_ENTRY_SIZE = maxEntrySize; if(maxEntrySize < 0) {
MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE;
} else {
MAX_ENTRY_SIZE = maxEntrySize;
}
} }
public static int getMaxEntrySize() { public static int getMaxEntrySize() {
@ -61,7 +65,7 @@ public final class ZipArchiveFakeEntry extends ZipArchiveEntry implements Closea
final long entrySize = entry.getSize(); final long entrySize = entry.getSize();
final int threshold = ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles(); final int threshold = ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles();
if (threshold >= 0 && entrySize >= threshold) { if (threshold >= 0 && (entrySize >= threshold || entrySize == -1)) {
if (ZipInputStreamZipEntrySource.shouldEncryptTempFiles()) { if (ZipInputStreamZipEntrySource.shouldEncryptTempFiles()) {
encryptedTempData = new EncryptedTempData(); encryptedTempData = new EncryptedTempData();
try (OutputStream os = encryptedTempData.getOutputStream()) { try (OutputStream os = encryptedTempData.getOutputStream()) {

View File

@ -28,6 +28,9 @@ import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.List; import java.util.List;
import org.apache.poi.openxml4j.util.ZipArchiveFakeEntry;
import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange; import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;
@ -149,22 +152,22 @@ class TestXWPFBugs {
} }
} }
/** /**
* Removing a run needs to take into account position of run if paragraph contains hyperlink runs * Removing a run needs to take into account position of run if paragraph contains hyperlink runs
*/ */
@Test @Test
void test58618() throws IOException { void test58618() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx")) { try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx")) {
XWPFParagraph para = (XWPFParagraph) doc.getBodyElements().get(0); XWPFParagraph para = (XWPFParagraph) doc.getBodyElements().get(0);
assertNotNull(para); assertNotNull(para);
assertEquals("Some text some hyper links link link and some text.....", para.getText()); assertEquals("Some text some hyper links link link and some text.....", para.getText());
XWPFRun run = para.insertNewRun(para.getRuns().size()); XWPFRun run = para.insertNewRun(para.getRuns().size());
run.setText("New Text"); run.setText("New Text");
assertEquals("Some text some hyper links link link and some text.....New Text", para.getText()); assertEquals("Some text some hyper links link link and some text.....New Text", para.getText());
para.removeRun(para.getRuns().size() - 2); para.removeRun(para.getRuns().size() - 2);
assertEquals("Some text some hyper links link linkNew Text", para.getText()); assertEquals("Some text some hyper links link linkNew Text", para.getText());
} }
} }
@Test @Test
void test59378() throws IOException { void test59378() throws IOException {
@ -329,7 +332,7 @@ class TestXWPFBugs {
} }
} }
private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){ private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id) {
// create a numbering scheme // create a numbering scheme
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance(); CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
// give the scheme an ID // give the scheme an ID
@ -340,4 +343,27 @@ class TestXWPFBugs {
documentNumbering.addNum(abstractNumID); documentNumbering.addNum(abstractNumID);
} }
@Test
void testBug69628() throws IOException {
final int expectedParagraphs = 24;
// bug69628.docx has -1 entry sizes in the zip data
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
}
// test again with smaller byte array max
ZipArchiveFakeEntry.setMaxEntrySize(30 * 1024 * 1024);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
} finally {
ZipArchiveFakeEntry.setMaxEntrySize(-1);
}
// test again but temp files enabled
ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(1000);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
} finally {
ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(-1);
}
}
} }

Binary file not shown.