mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
974da9f981
commit
cecab7f573
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,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
|
||||
|
|
Loading…
Reference in New Issue