From 92565ea07ce2a00640dca4683680e678977e79e5 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 10 Aug 2025 16:07:55 +0200 Subject: [PATCH] Adjust test with deeply nested file for JDK 25 It seems JDK 25 introduces more checks when parsing XML Also apply some IDE suggestions and fix deprecation warnings --- .../org/apache/poi/xwpf/TestXWPFBugs.java | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestXWPFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestXWPFBugs.java index 170473396e..5611064220 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestXWPFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestXWPFBugs.java @@ -29,6 +29,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.poi.POIDataSamples; import org.apache.poi.POIException; +import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePartName; @@ -137,11 +138,11 @@ class TestXWPFBugs { void bug59058() throws IOException, XmlException { String[] files = {"bug57031.docx", "bug59058.docx"}; for (String f : files) { - ZipFile zf = new ZipFile(samples.getFile(f)); - ZipArchiveEntry entry = zf.getEntry("word/document.xml"); - DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry)); - assertNotNull(document); - zf.close(); + try (ZipFile zf = ZipFile.builder().setFile(samples.getFile(f)).get()) { + ZipArchiveEntry entry = zf.getEntry("word/document.xml"); + DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry)); + assertNotNull(document); + } } } @@ -149,11 +150,11 @@ class TestXWPFBugs { void missingXsbs() throws IOException, XmlException { String[] files = {"bib-chernigovka.netdo.ru_download_docs_17459.docx"}; for (String f : files) { - ZipFile zf = new ZipFile(samples.getFile(f)); - ZipArchiveEntry entry = zf.getEntry("word/document.xml"); - DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry)); - assertNotNull(document); - zf.close(); + try (ZipFile zf = ZipFile.builder().setFile(samples.getFile(f)).get()) { + ZipArchiveEntry entry = zf.getEntry("word/document.xml"); + DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry)); + assertNotNull(document); + } } } @@ -277,11 +278,19 @@ class TestXWPFBugs { } @Test - public void testDeepTableCell() throws Exception { - // Document contains a table with nested cells. - IOException ex = assertThrows(IOException.class, - () -> XWPFTestDataSamples.openSampleDocument("deep-table-cell.docx")); - assertInstanceOf(POIException.class, ex.getCause()); - assertTrue(ex.getMessage().contains("Node depth exceeds maximum supported depth")); + public void testDeepTableCell() { + try { + // Document contains a table with nested cells. + //noinspection resource + XWPFTestDataSamples.openSampleDocument("deep-table-cell.docx"); + fail("Should catch exception"); + } catch (POIXMLException e) { + // JDK 25+ does more checks, so more than one exception are possible + assertTrue(e.getMessage().contains("The element \"w:t\" has a depth"), + "Had: " + e); + } catch (IOException e) { + assertInstanceOf(POIException.class, e.getCause()); + assertTrue(e.getMessage().contains("Node depth exceeds maximum supported depth")); + } } }