]> source.dussan.org Git - poi.git/commitdiff
Bug 64838: Do not populate cells with a paragraph when loading an existing document
authorDominik Stadler <centic@apache.org>
Wed, 30 Dec 2020 21:40:06 +0000 (21:40 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 30 Dec 2020 21:40:06 +0000 (21:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884958 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java

index 57f7d2051650bdb5b0d14b5b46e9dee3740d7fda..1b68ed2559577fcf438d2078b843e3341fc5aae3 100644 (file)
@@ -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<>();
index faf96df93178343f8ade3174dd83d9995c8d50f3..16663012b5d80f5d124e9da450d110f50d550dfd 100644 (file)
@@ -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;
index d057e120ae27cb6ded05795d3880642e89fd975b..158eb2495dde6e3ea9c5ee8c1a685582f2d6043d 100644 (file)
@@ -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();
         }
index 93214b47595bc11a7595c4d11c13e2a5ff9816d1..1ac72e26f871514f5dba55406e210a9bf4546789 100644 (file)
@@ -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);