From f006c8ee6b278f16c5abd8649f0dd131e5a4aae1 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 13 Nov 2018 08:20:47 +0000 Subject: [PATCH] add testcase for https://bz.apache.org/bugzilla/show_bug.cgi?id=62906 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1846489 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/usermodel/examples/CreateTable.java | 4 +- .../apache/poi/xssf/usermodel/XSSFSheet.java | 9 +- .../poi/xssf/usermodel/TestXSSFSheet.java | 16 ++- .../poi/xssf/usermodel/TestXSSFTable.java | 98 ++++++++++++------- 4 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java index c2fc068afe..079aa163b0 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java @@ -36,8 +36,8 @@ public class CreateTable { public static void main(String[] args) throws IOException { - try (Workbook wb = new XSSFWorkbook()) { - XSSFSheet sheet = (XSSFSheet) wb.createSheet(); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = wb.createSheet(); // Set which area the table should be placed in AreaReference reference = wb.getCreationHelper().createAreaReference( diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index d776f8fc00..bbd7eda838 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -4109,18 +4109,17 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1; // the id could already be taken after insertion/deletion of different tables - outerloop: - while(true) { + boolean loop = true; + while(loop) { + loop = false; for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) { String fileName = XSSFRelation.TABLE.getFileName(tableNumber); if(fileName.equals(packagePart.getPartName().getName())) { // duplicate found, increase the number and start iterating again tableNumber++; - continue outerloop; + loop = true; } } - - break; } RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 63fbf8b616..de828ba1c9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -55,10 +55,7 @@ import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.util.CellAddress; -import org.apache.poi.ss.util.CellRangeAddress; -import org.apache.poi.ss.util.CellReference; -import org.apache.poi.ss.util.CellUtil; +import org.apache.poi.ss.util.*; import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -1993,12 +1990,11 @@ public final class TestXSSFSheet extends BaseTestXSheet { @Test public void testGetHeaderFooterProperties() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sh = wb.createSheet(); - - XSSFHeaderFooterProperties hfProp = sh.getHeaderFooterProperties(); - assertNotNull(hfProp); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sh = wb.createSheet(); - wb.close(); + XSSFHeaderFooterProperties hfProp = sh.getHeaderFooterProperties(); + assertNotNull(hfProp); + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java index e8cda3ee92..7b0331a658 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java @@ -348,15 +348,45 @@ public final class TestXSSFTable { IOUtils.closeQuietly(wb); } - + + @Test + public void testCreateTableIds() throws IOException { + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = wb.createSheet(); + + AreaReference reference1 = wb.getCreationHelper().createAreaReference( + new CellReference(0, 0), new CellReference(2, 2)); + + XSSFTable table1 = sheet.createTable(reference1); + + assertEquals(1, table1.getCTTable().getTableColumns().getTableColumnArray(0).getId()); + assertEquals(2, table1.getCTTable().getTableColumns().getTableColumnArray(1).getId()); + assertEquals(3, table1.getCTTable().getTableColumns().getTableColumnArray(2).getId()); + + assertEquals(1, table1.getCTTable().getId()); + + AreaReference reference2 = wb.getCreationHelper().createAreaReference( + new CellReference(10, 10), new CellReference(12, 12)); + + XSSFTable table2 = sheet.createTable(reference2); + + // these IDs dupplicate those from table1 and may be cause of https://bz.apache.org/bugzilla/show_bug.cgi?id=62906 + assertEquals(1, table2.getCTTable().getTableColumns().getTableColumnArray(0).getId()); + assertEquals(2, table2.getCTTable().getTableColumns().getTableColumnArray(1).getId()); + assertEquals(3, table2.getCTTable().getTableColumns().getTableColumnArray(2).getId()); + + assertEquals(2, table2.getCTTable().getId()); + } + } + @Test public void testSetArea() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sh = wb.createSheet(); - + AreaReference tableArea = new AreaReference("B10:D12", wb.getSpreadsheetVersion()); XSSFTable table = sh.createTable(tableArea); - + assertEquals(3, table.getColumnCount()); assertEquals(3, table.getRowCount()); @@ -366,11 +396,11 @@ public final class TestXSSFTable { assertEquals(3, table.getColumnCount()); assertEquals(3, table.getRowCount()); - + // increase size by 1 row and 1 column AreaReference tableArea3 = new AreaReference("B11:E14", wb.getSpreadsheetVersion()); table.setArea(tableArea3); - + assertEquals(4, table.getColumnCount()); assertEquals(4, table.getRowCount()); @@ -380,43 +410,41 @@ public final class TestXSSFTable { assertEquals(2, table.getColumnCount()); assertEquals(2, table.getRowCount()); - - IOUtils.closeQuietly(wb); + } } @Test - public void testCreateColumn() { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sh = wb.createSheet(); - - AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion()); - XSSFTable table = sh.createTable(tableArea); + public void testCreateColumn() throws IOException { + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sh = wb.createSheet(); - assertEquals(1, table.getColumnCount()); - assertEquals(2, table.getRowCount()); + AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion()); + XSSFTable table = sh.createTable(tableArea); - // add columns - XSSFTableColumn c1 = table.getColumns().get(0); - XSSFTableColumn cB = table.createColumn("Column B"); - XSSFTableColumn cD = table.createColumn("Column D"); - XSSFTableColumn cC = table.createColumn("Column C", 2); // add between B and D - table.updateReferences(); - table.updateHeaders(); + assertEquals(1, table.getColumnCount()); + assertEquals(2, table.getRowCount()); - assertEquals(4, table.getColumnCount()); - assertEquals(2, table.getRowCount()); + // add columns + XSSFTableColumn c1 = table.getColumns().get(0); + XSSFTableColumn cB = table.createColumn("Column B"); + XSSFTableColumn cD = table.createColumn("Column D"); + XSSFTableColumn cC = table.createColumn("Column C", 2); // add between B and D + table.updateReferences(); + table.updateHeaders(); - // column IDs start at 1, and increase in the order columns are added (see bug #62740) - assertEquals("Column c ID", 1, c1.getId()); - assertTrue("Column B ID", c1.getId() < cB.getId()); - assertTrue("Column D ID", cB.getId() < cD.getId()); - assertTrue("Column C ID", cD.getId() < cC.getId()); - assertEquals("Column 1", table.getColumns().get(0).getName()); // generated name - assertEquals("Column B", table.getColumns().get(1).getName()); - assertEquals("Column C", table.getColumns().get(2).getName()); - assertEquals("Column D", table.getColumns().get(3).getName()); + assertEquals(4, table.getColumnCount()); + assertEquals(2, table.getRowCount()); - IOUtils.closeQuietly(wb); + // column IDs start at 1, and increase in the order columns are added (see bug #62740) + assertEquals("Column c ID", 1, c1.getId()); + assertTrue("Column B ID", c1.getId() < cB.getId()); + assertTrue("Column D ID", cB.getId() < cD.getId()); + assertTrue("Column C ID", cD.getId() < cC.getId()); + assertEquals("Column 1", table.getColumns().get(0).getName()); // generated name + assertEquals("Column B", table.getColumns().get(1).getName()); + assertEquals("Column C", table.getColumns().get(2).getName()); + assertEquals("Column D", table.getColumns().get(3).getName()); + } } @Test(expected = IllegalArgumentException.class) -- 2.39.5