diff options
author | Dominik Stadler <centic@apache.org> | 2020-12-30 21:40:06 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2020-12-30 21:40:06 +0000 |
commit | 99dc9ea7f305fb05c868af8e1ae366ff5c7c6802 (patch) | |
tree | 3698ce677992b6bc654b9f1d6c0b5eb8dfed776d | |
parent | 42c4e7d89ac33f4a71fd57d87666dd1a89bbf118 (diff) | |
download | poi-99dc9ea7f305fb05c868af8e1ae366ff5c7c6802.tar.gz poi-99dc9ea7f305fb05c868af8e1ae366ff5c7c6802.zip |
Bug 64838: Do not populate cells with a paragraph when loading an existing document
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884958 13f79535-47bb-0310-9956-ffa450edef68
4 files changed, 29 insertions, 16 deletions
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<XWPFTableRow> 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,17 +172,16 @@ public class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell have at least one paragraph by default + // now paragraph or body element initially + assertEquals(0, cell.getParagraphs().size()); + assertEquals(0, cell.getBodyElements().size()); + + XWPFParagraph p = cell.addParagraph(); assertEquals(1, cell.getParagraphs().size()); assertEquals(1, cell.getBodyElements().size()); + assertSame(p, cell.getParagraphArray(0)); assertEquals(cell.getParagraphArray(0), cell.getBodyElements().get(0)); - 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)); - 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); |