]> source.dussan.org Git - poi.git/commitdiff
Sonar fixes
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 5 Jan 2021 23:40:36 +0000 (23:40 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 5 Jan 2021 23:40:36 +0000 (23:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885179 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

index ab43d21e3d05f16eb83c1de4323cd0046bc33158..3b6576e178ce7fc803f44b7ad91662f924c23b7a 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hssf.record.common;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.io.ByteArrayInputStream;
@@ -350,21 +351,20 @@ public final class TestUnicodeString {
 
     @Test
     public void unicodeStringsNullPointer() throws IOException {
-        HSSFWorkbook wb = new HSSFWorkbook();
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
 
-        Sheet sheet = wb.createSheet("styles");
-        Row row = sheet.createRow(0);
-        Cell cell = row.createCell(0);
+            Sheet sheet = wb.createSheet("styles");
+            Row row = sheet.createRow(0);
+            Cell cell = row.createCell(0);
 
-        CellStyle style = wb.createCellStyle();
-        style.setFont(wb.createFont());
-        cell.setCellStyle(style);
+            CellStyle style = wb.createCellStyle();
+            style.setFont(wb.createFont());
+            cell.setCellStyle(style);
 
-        cell.setCellValue("test");
+            cell.setCellValue("test");
 
-        HSSFOptimiser.optimiseFonts(wb);
-
-        wb.close();
+            assertDoesNotThrow(() -> HSSFOptimiser.optimiseFonts(wb));
+        }
     }
 
     @Test
index 5bfdd5cb9f23f0217f2bfc349712f397dc6effa0..77195a51157e3f273211c3e5a5202ba44c75655a 100644 (file)
@@ -283,14 +283,11 @@ public final class TestBugs extends BaseTestBugzillaIssues {
     @Test
     public void bug27852() throws Exception {
         try (HSSFWorkbook wb = openSampleWorkbook("27852.xls")) {
-
-            for (int i = 0; i < wb.getNumberOfNames(); i++) {
-                HSSFName name = wb.getNameAt(i);
-                name.getNameName();
-                if (name.isFunctionName()) {
-                    continue;
+            for (HSSFName name : wb.getAllNames()) {
+                assertNotNull(name.getNameName());
+                if (!name.isFunctionName()) {
+                    assertNotNull(name.getRefersToFormula());
                 }
-                name.getRefersToFormula();
             }
         }
     }
@@ -305,9 +302,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
             HSSFSheet sheet = wb.createSheet();
             for (int i = 1; i < 400; i++) {
                 HSSFRow row = sheet.getRow(i);
-                if (row != null) {
-                    row.getCell(0);
-                }
+                assertNull(row);
             }
         }
     }
@@ -1970,6 +1965,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
         try (HSSFWorkbook wb = openSampleWorkbook("46250.xls")) {
             Sheet sh = wb.getSheet("Template");
             Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
+            int sIdx = wb.getSheetIndex(cSh);
 
             HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
             HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
@@ -1977,7 +1973,11 @@ public final class TestBugs extends BaseTestBugzillaIssues {
             tb.setString(new HSSFRichTextString("POI test"));
             tb.setAnchor(new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 10, 10));
 
-            writeOutAndReadBack(wb).close();
+            try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
+                HSSFSheet sh2 = wb2.getSheetAt(sIdx);
+                assertNotNull(sh2);
+                assertNotNull(sh2.getDrawingPatriarch());
+            }
         }
     }
 
@@ -1999,7 +1999,12 @@ public final class TestBugs extends BaseTestBugzillaIssues {
                 row.createCell(6).setCellValue("added cells.");
             }
 
-            writeOutAndReadBack(wb).close();
+            try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
+                Sheet sheet2 = wb2.getSheet("test-sheet");
+                Row row2 = sheet2.getRow(5);
+                assertNotNull(row2);
+                assertEquals(cal.getTime(),row2.getCell(2).getDateCellValue());
+            }
         }
     }
 
@@ -2299,12 +2304,11 @@ public final class TestBugs extends BaseTestBugzillaIssues {
     public void test57925() throws IOException {
         try (Workbook wb = openSampleWorkbook("57925.xls")) {
             wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
-
-            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
-                Sheet sheet = wb.getSheetAt(i);
+            DataFormatter df = new DataFormatter();
+            for (Sheet sheet : wb) {
                 for (Row row : sheet) {
                     for (Cell cell : row) {
-                        new DataFormatter().formatCellValue(cell);
+                        assertNotNull(df.formatCellValue(cell));
                     }
                 }
             }