diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 57f7d20516..1b68ed2559 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -82,9 +82,7 @@ public class XWPFTableCell implements IBody, ICell { this.ctTc = cell; this.part = part; this.tableRow = tableRow; - // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt. - if (cell.sizeOfPArray() < 1) - cell.addNewP(); + bodyElements = new ArrayList<>(); paragraphs = new ArrayList<>(); tables = new ArrayList<>(); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index faf96df931..16663012b5 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.OutputStream; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java index d057e120ae..158eb2495d 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java @@ -554,15 +554,23 @@ public class TestXWPFTable { // assert the table is empty List rows = table.getRows(); assertEquals(noRows, rows.size(), "Table has less rows than requested."); + int row = 0; for (XWPFTableRow xwpfRow : rows) { assertNotNull(xwpfRow); + assertEquals(noCols, xwpfRow.getTableCells().size(), + "Row has less columns than requested."); for (int i = 0; i < 7; i++) { XWPFTableCell xwpfCell = xwpfRow.getCell(i); assertNotNull(xwpfCell); - assertEquals(1, xwpfCell.getParagraphs().size(), "Empty cells should not have one paragraph."); + assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), + "Empty cells should not have one paragraph: " + i); + xwpfCell = xwpfRow.getCell(i); - assertEquals(1, xwpfCell.getParagraphs().size(), "Calling 'getCell' must not modify cells content."); + assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), + "Calling 'getCell' must not modify cells content: " + i); } + + row++; } doc.getPackage().revert(); } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java index 93214b4759..1ac72e26f8 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java @@ -22,6 +22,8 @@ package org.apache.poi.xwpf.usermodel; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -170,16 +172,15 @@ public class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell have at least one paragraph by default - assertEquals(1, cell.getParagraphs().size()); - assertEquals(1, cell.getBodyElements().size()); - assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0)); + // now paragraph or body element initially + assertEquals(0, cell.getParagraphs().size()); + assertEquals(0, cell.getBodyElements().size()); XWPFParagraph p = cell.addParagraph(); - assertEquals(2, cell.getParagraphs().size()); - assertEquals(2, cell.getBodyElements().size()); - assertEquals(p, cell.getParagraphArray(1)); - assertEquals(cell.getParagraphArray(1), cell.getBodyElements().get(1)); + assertEquals(1, cell.getParagraphs().size()); + assertEquals(1, cell.getBodyElements().size()); + assertSame(p, cell.getParagraphArray(0)); + assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0)); doc.close(); } @@ -191,8 +192,10 @@ public class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell have at least one paragraph by default - XWPFParagraph p0 = cell.getParagraphArray(0); + // cell have no paragraph by default + assertNull(cell.getParagraphArray(0)); + + XWPFParagraph p0 = cell.addParagraph(); XWPFParagraph p1 = cell.addParagraph(); cell.addParagraph(); @@ -222,7 +225,12 @@ public class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell have at least one paragraph by default + // cell should not have any elements by default + assertTrue(cell.getParagraphs().isEmpty()); + + XWPFParagraph p = cell.addParagraph(); + assertNotNull(p); + XWPFParagraph p0 = cell.getParagraphArray(0); XmlCursor newCursor = p0.getCTP().newCursor(); cell.insertNewTbl(newCursor);