From fa573c72dab03a6196f12159c810b26244bcef95 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 30 Jul 2025 21:40:21 +0100 Subject: [PATCH] add xwpf test --- .../org/apache/poi/xwpf/usermodel/XWPFTableCell.java | 11 +++++++++-- .../apache/poi/xwpf/usermodel/TestXWPFTableCell.java | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 27f7dbfaf9..6e590e5f9e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -79,10 +79,16 @@ public class XWPFTableCell implements IBody, ICell { * If a table cell does not include at least one block-level element, then this document shall be considered corrupt */ public XWPFTableCell(CTTc cell, XWPFTableRow tableRow, IBody part) { + if (cell == null) { + throw new IllegalArgumentException("CTTc cannot be null"); + } + if (tableRow == null) { + throw new IllegalArgumentException("tableRow cannot be null"); + } this.ctTc = cell; this.part = part; this.tableRow = tableRow; - this.xwpfDocument = part.getXWPFDocument(); + this.xwpfDocument = part == null ? null : part.getXWPFDocument(); bodyElements = new ArrayList<>(); paragraphs = new ArrayList<>(); @@ -530,9 +536,10 @@ public class XWPFTableCell implements IBody, ICell { return xwpfDocument; } else if (part instanceof XWPFTableCell) { return getCellDocument((XWPFTableCell) part, 0); - } else { + } else if (part != null) { return part.getXWPFDocument(); } + return null; } private static final int MAX_RECURSION_DEPTH = 1000; diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java index 9d29cb8247..688133d772 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java @@ -271,6 +271,7 @@ class TestXWPFTableCell { XWPFDocument doc = new XWPFDocument(); XWPFTable table = doc.createTable(1, 1); XWPFTableCell cell = table.getRow(0).getCell(0); + assertEquals(doc, cell.getXWPFDocument()); // cell have at least one paragraph by default when creating a cell assertEquals(1, cell.getParagraphs().size()); @@ -282,6 +283,7 @@ class TestXWPFTableCell { XWPFDocument readDoc = XWPFTestDataSamples.writeOutAndReadBack(doc); XWPFTableCell readCell = readDoc.getTableArray(0).getRow(0).getCell(0); assertEquals(0, readCell.getParagraphs().size()); + assertEquals(readDoc, readCell.getXWPFDocument()); } @Test