]> source.dussan.org Git - poi.git/commitdiff
[bug-62906] ensure table display name is always set
authorPJ Fanning <fanningpj@apache.org>
Fri, 26 Apr 2019 07:48:26 +0000 (07:48 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 26 Apr 2019 07:48:26 +0000 (07:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1858179 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java

index 563df1ecd0b06947a0b979f3ddd9e143ecaf2842..d0a43af3577f5feab369369cb988233d8d606f41 100644 (file)
@@ -4140,6 +4140,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
             table.setArea(tableArea);
         }
 
+        // Bug 62906: Must set a display name; can be overridden using setDisplayName
+        final String displayName = "Table" + tableNumber;
+        table.setDisplayName(displayName);
+
         return table;
     }
 
index de560ddd2e2e7f77916d0275d93287f21f84796e..b26f72be50d5e2721a0c318ecf95006c22b8ca96 100644 (file)
@@ -432,6 +432,9 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
      * @param name to use
      */
     public void setDisplayName(String name) {
+        if (name == null || name.isEmpty()) {
+            throw new IllegalArgumentException("Display name must not be null or empty");
+        }
         ctTable.setDisplayName(name);
     }
 
index 7841d9a82f58dc1606a92a2be0431bb8dc8bf5c5..3648f780d956611e95a4cdf6b683d668d33ca0e1 100644 (file)
@@ -171,7 +171,7 @@ public final class TestXSSFTable {
         try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
             XSSFTable table = wb.getTable("\\_Prime.1");
             assertEquals(0, table.getStartColIndex());
-        } 
+        }
     }
 
     @Test
@@ -229,17 +229,13 @@ public final class TestXSSFTable {
             assertEquals(3, table.getColumnCount());
         }
     }
-    
+
     @Test
     public void getAndSetDisplayName() throws IOException {
         try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
             XSSFTable table = wb.getTable("\\_Prime.1");
             assertEquals("\\_Prime.1", table.getDisplayName());
-
-            table.setDisplayName(null);
-            assertNull(table.getDisplayName());
-            assertEquals("\\_Prime.1", table.getName()); // name and display name are different
-
+            
             table.setDisplayName("Display name");
             assertEquals("Display name", table.getDisplayName());
             assertEquals("\\_Prime.1", table.getName()); // name and display name are different
@@ -253,6 +249,8 @@ public final class TestXSSFTable {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
             XSSFSheet sh = wb.createSheet();
             XSSFTable table = sh.createTable();
+            assertNotNull(table.getDisplayName());
+            assertNotNull(table.getCTTable().getDisplayName());
             CTTable ctTable = table.getCTTable();
             ctTable.setRef("B2:E8");
 
@@ -299,7 +297,7 @@ public final class TestXSSFTable {
             IOUtils.closeQuietly(wb);
         }
     }
-    
+
     @Test
     public void testGetDataRowCount() throws IOException {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
@@ -318,7 +316,7 @@ public final class TestXSSFTable {
             IOUtils.closeQuietly(wb);
         }
     }
-    
+
     @Test
     public void testSetDataRowCount() throws IOException {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
@@ -358,6 +356,8 @@ public final class TestXSSFTable {
 
             XSSFTable table1 = sheet.createTable(reference1);
             assertEquals("A1:C3", table1.getCTTable().getRef());
+            assertNotNull(table1.getDisplayName());
+            assertNotNull(table1.getCTTable().getDisplayName());
 
             assertEquals(1, table1.getCTTable().getTableColumns().getTableColumnArray(0).getId());
             assertEquals(2, table1.getCTTable().getTableColumns().getTableColumnArray(1).getId());
@@ -413,7 +413,7 @@ public final class TestXSSFTable {
             assertEquals(2, table.getRowCount());
         }
     }
-    
+
     @Test
     public void testCreateColumn() throws IOException {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
@@ -460,7 +460,7 @@ public final class TestXSSFTable {
             table.createColumn("Column 3", 3); // out of bounds
         }
     }
-    
+
     @Test
     public void testDifferentHeaderTypes() throws IOException {
         try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TablesWithDifferentHeaders.xlsx")) {
@@ -492,7 +492,7 @@ public final class TestXSSFTable {
             assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
         }
     }
-    
+
     /**
      * See https://stackoverflow.com/questions/44407111/apache-poi-cant-format-filled-cells-as-numeric
      */
@@ -546,4 +546,45 @@ public final class TestXSSFTable {
             IOUtils.closeQuietly(wb2);
         }
     }
+
+    @Test
+    public void testSetDisplayName() 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);
+            table1.setDisplayName("TableTest");
+            assertEquals("TableTest", table1.getDisplayName());
+            assertEquals("TableTest", table1.getCTTable().getDisplayName());
+        }
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testSetDisplayNameNull() 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);
+            table1.setDisplayName(null);
+        }
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testSetDisplayNameEmpty() 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);
+            table1.setDisplayName("");
+        }
+    }
 }