diff options
author | Sayi <sayi@apache.org> | 2021-05-20 09:57:32 +0000 |
---|---|---|
committer | Sayi <sayi@apache.org> | 2021-05-20 09:57:32 +0000 |
commit | cecab7f573f0cdf9ea74bedcf9b43dc535976e43 (patch) | |
tree | 8edae8ef879029d3da479da2fb57bbb20f442768 | |
parent | 974da9f981640f1bac886459fc241d2bc9e9ec29 (diff) | |
download | poi-cecab7f573f0cdf9ea74bedcf9b43dc535976e43.tar.gz poi-cecab7f573f0cdf9ea74bedcf9b43dc535976e43.zip |
Bug65292: Manual revert r1884958, Add a paragraph by default when creating a cell and not add a paragraph when loading an existing table cell
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1890042 13f79535-47bb-0310-9956-ffa450edef68
4 files changed, 57 insertions, 29 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index c01643ec0d..838688ffa9 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -63,6 +63,7 @@ public class XWPFTableRow { */ public XWPFTableCell createCell() { XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody()); + ensureBlockLevelElement(tableCell); tableCells.add(tableCell); return tableCell; } @@ -87,10 +88,19 @@ public class XWPFTableRow { public XWPFTableCell addNewTableCell() { CTTc cell = ctRow.addNewTc(); XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody()); + ensureBlockLevelElement(tableCell); tableCells.add(tableCell); return tableCell; } + private void ensureBlockLevelElement(XWPFTableCell tableCell) { + // If a table cell does not include at least one block-level element, + // then this document shall be considered corrupt. + if (tableCell.getParagraphs().isEmpty()) { + tableCell.addParagraph(); + } + } + /** * This element specifies the height of the current table row within the * current table. This height shall be used to determine the resulting diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java index 0feddb6819..70faecbd96 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java @@ -542,7 +542,7 @@ class TestXWPFTable { } @Test - void testCreateTable() throws Exception { + public void testCreateTable() throws Exception { // open an empty document try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) { @@ -554,23 +554,15 @@ 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(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), - "Empty cells should not have one paragraph: " + i); - + assertEquals(1, xwpfCell.getParagraphs().size(), "Empty cells should not have one paragraph."); xwpfCell = xwpfRow.getCell(i); - assertEquals(row != 0 || i != 0 ? 0 : 1, xwpfCell.getParagraphs().size(), - "Calling 'getCell' must not modify cells content: " + i); + assertEquals(1, xwpfCell.getParagraphs().size(), "Calling 'getCell' must not modify cells content."); } - - row++; } doc.getPackage().revert(); } 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 36d0277568..830f706c83 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 @@ -23,9 +23,8 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; 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 java.io.IOException; import java.util.List; import org.apache.poi.xwpf.XWPFTestDataSamples; @@ -169,16 +168,17 @@ class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // now paragraph or body element initially - assertEquals(0, cell.getParagraphs().size()); - assertEquals(0, cell.getBodyElements().size()); - - XWPFParagraph p = cell.addParagraph(); + // cell have at least one paragraph by default 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(); } @@ -189,10 +189,8 @@ class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell have no paragraph by default - assertNull(cell.getParagraphArray(0)); - - XWPFParagraph p0 = cell.addParagraph(); + // cell have at least one paragraph by default + XWPFParagraph p0 = cell.getParagraphArray(0); XWPFParagraph p1 = cell.addParagraph(); cell.addParagraph(); @@ -222,12 +220,7 @@ class TestXWPFTableCell { XWPFTableRow tr = table.createRow(); XWPFTableCell cell = tr.addNewTableCell(); - // cell should not have any elements by default - assertTrue(cell.getParagraphs().isEmpty()); - - XWPFParagraph p = cell.addParagraph(); - assertNotNull(p); - + // cell have at least one paragraph by default XWPFParagraph p0 = cell.getParagraphArray(0); XmlCursor newCursor = p0.getCTP().newCursor(); cell.insertNewTbl(newCursor); @@ -273,4 +266,22 @@ class TestXWPFTableCell { cell.setText("test text 2"); assertEquals("test text 1test text 2", cell.getText()); } + + @Test + void test65292() throws IOException { + XWPFDocument doc = new XWPFDocument(); + XWPFTable table = doc.createTable(1, 1); + XWPFTableCell cell = table.getRow(0).getCell(0); + + // cell have at least one paragraph by default when creating a cell + assertEquals(1, cell.getParagraphs().size()); + + cell.removeParagraph(0); + assertEquals(0, cell.getParagraphs().size()); + + // not add a paragraph when loading an existing empty table cell + XWPFDocument readDoc = XWPFTestDataSamples.writeOutAndReadBack(doc); + XWPFTableCell readCell = readDoc.getTableArray(0).getRow(0).getCell(0); + assertEquals(0, readCell.getParagraphs().size()); + } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableRow.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableRow.java index 1c1fe03c88..54772bda18 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableRow.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableRow.java @@ -43,6 +43,21 @@ class TestXWPFTableRow { } @Test + void testCreateCell() throws IOException { + try (XWPFDocument doc = new XWPFDocument()) { + XWPFTable table = doc.createTable(1, 2); + XWPFTableRow tr = table.createRow(); + XWPFTableCell cell = tr.createCell(); + assertNotNull(cell); + assertTrue(cell.getParagraphs().size() >= 1); + + cell = tr.addNewTableCell(); + assertNotNull(cell); + assertTrue(cell.getParagraphs().size() >= 1); + } + } + + @Test void testSetGetCantSplitRow() throws IOException { // create a table try (XWPFDocument doc = new XWPFDocument()) { |