From be52865b38fed9d482121af42f2f05f0d12d9d2f Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 29 Aug 2019 05:16:05 +0000 Subject: [PATCH] Fix bug #61490, for some tables in documents, we should not remove the last cell Check the expected number of cells to see if the last cell should be removed. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866055 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hwpf/usermodel/TableRow.java | 5 +- .../apache/poi/hwpf/usermodel/TestBugs.java | 43 +++++++++++++----- test-data/document/61490.doc | Bin 0 -> 31232 bytes 3 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 test-data/document/61490.doc diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java index 7080addfcf..c405b915d7 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableRow.java @@ -174,7 +174,8 @@ public final class TableRow extends Range cells.add( tableCell ); } - if ( !cells.isEmpty() ) + // sometimes there are "fake" cells which we need to exclude + if ( !cells.isEmpty() && cells.size() != expectedCellsCount ) { TableCell lastCell = cells.get( cells.size() - 1 ); if ( lastCell.numParagraphs() == 1 @@ -196,7 +197,7 @@ public final class TableRow extends Range _tprops.setItcMac( (short) cells.size() ); } - _cells = cells.toArray( new TableCell[cells.size()] ); + _cells = cells.toArray(new TableCell[0]); _cellsFound = true; } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index b1ec9aaf6d..0fb5da3c7a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -20,6 +20,7 @@ import static org.apache.poi.POITestCase.assertContains; import static org.apache.poi.POITestCase.assertNotContained; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.File; @@ -39,15 +40,17 @@ import org.apache.poi.hwpf.converter.AbstractWordUtils; import org.apache.poi.hwpf.converter.WordToTextConverter; import org.apache.poi.hwpf.extractor.Word6Extractor; import org.apache.poi.hwpf.extractor.WordExtractor; -import org.apache.poi.hwpf.model.*; +import org.apache.poi.hwpf.model.FieldsDocumentPart; +import org.apache.poi.hwpf.model.FileInformationBlock; +import org.apache.poi.hwpf.model.PicturesTable; +import org.apache.poi.hwpf.model.PlexOfField; +import org.apache.poi.hwpf.model.SubdocumentType; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.IOUtils; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.junit.Test; -import junit.framework.TestCase; - /** * Test different problems reported in the Apache Bugzilla * against HWPF @@ -61,7 +64,7 @@ public class TestBugs{ .replaceAll("\r", "\n").trim(); String newActual = actual.replaceAll("\r\n", "\n" ) .replaceAll("\r", "\n").trim(); - TestCase.assertEquals(newExpected, newActual); + assertEquals(newExpected, newActual); } private static void assertTableStructures(Range expected, Range actual ) { @@ -72,7 +75,7 @@ public class TestBugs{ Paragraph actParagraph = actual.getParagraph(p); assertEqualsIgnoreNewline(expParagraph.text(), actParagraph.text()); - assertEquals("Diffent isInTable flags for paragraphs #" + p + assertEquals("Different isInTable flags for paragraphs #" + p + " -- " + expParagraph + " -- " + actParagraph + ".", expParagraph.isInTable(), actParagraph.isInTable()); assertEquals(expParagraph.isTableRowEnd(), @@ -98,8 +101,8 @@ public class TestBugs{ } } - private String getText(String samplefile) throws IOException { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile(samplefile); + private String getText(String sampleFile) throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile(sampleFile); WordExtractor extractor = new WordExtractor(doc); try { return extractor.getText(); @@ -667,7 +670,7 @@ public class TestBugs{ } /** - * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException + * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutOfBoundsException * with no stack trace (broken after revision 1178063) */ @Test @@ -676,7 +679,7 @@ public class TestBugs{ } /** - * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException + * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutOfBoundsException * with no stack trace (broken after revision 1178063) */ @Test @@ -685,7 +688,7 @@ public class TestBugs{ } /** - * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException + * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutOfBoundsException * with no stack trace (broken after revision 1178063) */ @Test @@ -782,7 +785,7 @@ public class TestBugs{ } } - // Save away and re-read the document to prove the chages are permanent + // Save away and re-read the document to prove the changes are permanent document = HWPFTestDataSamples.writeOutAndReadBack(document); overallRange = document.getOverallRange(); numParas = overallRange.numParagraphs(); @@ -871,4 +874,22 @@ public class TestBugs{ document.close(); } + + @Test + public void test61490CellCountInTable() throws Exception { + try(HWPFDocument doc = HWPFTestDataSamples.openSampleFile("61490.doc")){ + Range range = doc.getRange(); + + System.out.println("print table"); + TableIterator tableIter = new TableIterator(range); + assertTrue(tableIter.hasNext()); + Table table = tableIter.next(); + TableRow row = table.getRow(2); + assertEquals(3, row.numCells()); + for(int cellIdx = 0;cellIdx < row.numCells(); cellIdx++) { + TableCell cell = row.getCell(cellIdx); + assertEquals("3" + (cellIdx+1), cell.text().trim()); + } + } + } } diff --git a/test-data/document/61490.doc b/test-data/document/61490.doc new file mode 100644 index 0000000000000000000000000000000000000000..67d57e7f105559a2cda6549d97b0b15b8e366242 GIT binary patch literal 31232 zcmeHQ2_ThA`=7Jm*q4y49Gs+^j)N=_iB=Wbw215!;UX=FqW#v@uH`G0N=j+bO4_A; z)h^oIRJ3Wq`9IHlyg5#F>n`8-|CTeS-}}rwGxN;MGs`9P_J0KL$8uZ7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