Sonar fixes
add asserts to tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885954 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java
index fdda400..9a0215e 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java
@@ -24,10 +24,13 @@
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
+import java.util.Objects;
+import java.util.Spliterator;
 import java.util.TreeMap;
+import java.util.stream.IntStream;
+import java.util.stream.StreamSupport;
 
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
@@ -446,31 +449,16 @@
     @Test
     void testBug56511() throws IOException {
         try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56511.xlsx")) {
-            for (Sheet sheet : wb) {
-                int lastRow = sheet.getLastRowNum();
-                for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= lastRow; rowIdx++) {
-                    Row row = sheet.getRow(rowIdx);
-                    if (row != null) {
-                        int lastCell = row.getLastCellNum();
-
-                        for (int cellIdx = row.getFirstCellNum(); cellIdx <= lastCell; cellIdx++) {
-
-                            Cell cell = row.getCell(cellIdx);
-                            if (cell != null) {
-                                //System.out.println("row " + rowIdx + " column " + cellIdx + ": " + cell.getCellType() + ": " + cell.toString());
-
-                                XSSFRichTextString richText = (XSSFRichTextString) cell.getRichStringCellValue();
-                                int anzFormattingRuns = richText.numFormattingRuns();
-                                for (int run = 0; run < anzFormattingRuns; run++) {
-                                    /*XSSFFont font =*/ richText.getFontOfFormattingRun(run);
-                                    //System.out.println("  run " + run
-                                    //        + " font " + (font == null ? "<null>" : font.getFontName()));
-                                }
-                            }
-                        }
-                    }
-                }
-            }
+            int[] idx = { 0 };
+            StreamSupport.stream(wb::spliterator, Spliterator.IMMUTABLE, false)
+                .flatMap(sheet -> StreamSupport.stream(sheet::spliterator, Spliterator.IMMUTABLE, false))
+                .filter(Objects::nonNull)
+                .flatMap(row -> StreamSupport.stream(row::spliterator, Spliterator.IMMUTABLE, false))
+                .filter(Objects::nonNull)
+                .map(Cell::getRichStringCellValue)
+                .map(XSSFRichTextString.class::cast)
+                .flatMap(x -> IntStream.range(0, x.numFormattingRuns()).mapToObj(x::getFontOfFormattingRun))
+                .forEach(f -> { if (idx[0]++ == 2) { assertNull(f); } else { assertNotNull(f); }} );
         }
     }
 
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 d054bfa..2071ca7 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
@@ -1274,7 +1274,9 @@
     @Timeout(value = 180, unit = SECONDS)
     @Test
     void bug51585() throws IOException {
-        XSSFTestDataSamples.openSampleWorkbook("51585.xlsx").close();
+        try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51585.xlsx")) {
+            assertNotNull(wb.getSheetAt(0));
+        }
     }
 
     private XSSFWorkbook setupSheet(){
@@ -1885,45 +1887,49 @@
      */
     @Test
     void testInsertCommentsToClonedSheet() throws IOException {
-    	Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx");
-		CreationHelper helper = wb.getCreationHelper();
-		Sheet sheet2 = wb.createSheet("Sheet 2");
-		Sheet sheet3 = wb.cloneSheet(0);
-		wb.setSheetName(2, "Sheet 3");
+    	try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx")) {
+            CreationHelper helper = wb.getCreationHelper();
+            Sheet sheet2 = wb.createSheet("Sheet 2");
+            Sheet sheet3 = wb.cloneSheet(0);
+            wb.setSheetName(2, "Sheet 3");
 
-		// Adding Comment to new created Sheet 2
-		addComments(helper, sheet2);
-		// Adding Comment to cloned Sheet 3
-		addComments(helper, sheet3);
+            Sheet[] sheets = { sheet2, sheet3 };
 
-        wb.close();
-    }
+            for (Sheet sheet : sheets) {
+                Drawing<?> drawing = sheet.createDrawingPatriarch();
 
-    private void addComments(CreationHelper helper, Sheet sheet) {
-		Drawing<?> drawing = sheet.createDrawingPatriarch();
+                for (int i = 0; i < 2; i++) {
+                    ClientAnchor anchor = helper.createClientAnchor();
+                    anchor.setCol1(0);
+                    anchor.setRow1(i);
+                    anchor.setCol2(2);
+                    anchor.setRow2(3 + i);
 
-		for (int i = 0; i < 2; i++) {
-			ClientAnchor anchor = helper.createClientAnchor();
-			anchor.setCol1(0);
-			anchor.setRow1(i);
-			anchor.setCol2(2);
-			anchor.setRow2(3 + i);
+                    Comment comment = drawing.createCellComment(anchor);
+                    comment.setString(helper.createRichTextString("BugTesting"));
 
-			Comment comment = drawing.createCellComment(anchor);
-			comment.setString(helper.createRichTextString("BugTesting"));
+                    Row row = sheet.getRow(i);
+                    if (row == null) {
+                        row = sheet.createRow(i);
+                    }
+                    Cell cell = row.getCell(0);
+                    if (cell == null) {
+                        cell = row.createCell(0);
+                    }
 
-			Row row = sheet.getRow(i);
-			if (row == null) {
-                row = sheet.createRow(i);
-            }
-			Cell cell = row.getCell(0);
-			if (cell == null) {
-                cell = row.createCell(0);
+                    cell.setCellComment(comment);
+                }
             }
 
-			cell.setCellComment(comment);
-		}
-
+            for (Sheet sheet : sheets) {
+                for (int i = 0; i < 2; i++) {
+                    CellAddress ref = new CellAddress(i, 0);
+                    Comment c = sheet.getCellComment(ref);
+                    assertNotNull(c);
+                    assertEquals("BugTesting", c.getString().getString());
+                }
+            }
+        }
     }
 
     // bug 59687:  XSSFSheet.RemoveRow doesn't handle row gaps properly when removing row comments
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
index cc9071e..06b15f7 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
@@ -17,18 +17,27 @@
 
 package org.apache.poi.xssf.usermodel;
 
-import org.apache.poi.ss.usermodel.*;
+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.assertThrows;
+
+import java.io.IOException;
+import java.util.stream.IntStream;
+
+import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Comment;
+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.CellUtil;
-import org.apache.poi.util.IOUtils;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.junit.jupiter.api.Test;
 
-import java.io.IOException;
-
-import static org.junit.jupiter.api.Assertions.*;
-
 public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
 
     public TestXSSFSheetShiftRows(){
@@ -390,61 +399,71 @@
     // bug 59983:  Wrong update of shared formulas after shiftRow
     @Test
     void testSharedFormulas() throws Exception {
-        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx");
-        XSSFSheet sheet = wb.getSheetAt(0);
-        assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
-        assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
-        assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
+        try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx")) {
+            XSSFSheet sheet = wb.getSheetAt(0);
+            assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
+            assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
+            assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
 
-        assertEquals("SUM(C3:C5)", getCellFormula(sheet, "C6"));
-        assertEquals("SUM(D3:D5)", getCellFormula(sheet, "D6"));
-        assertEquals("SUM(E3:E5)", getCellFormula(sheet, "E6"));
+            assertEquals("SUM(C3:C5)", getCellFormula(sheet, "C6"));
+            assertEquals("SUM(D3:D5)", getCellFormula(sheet, "D6"));
+            assertEquals("SUM(E3:E5)", getCellFormula(sheet, "E6"));
 
-        sheet.shiftRows(3, sheet.getLastRowNum(), 1);
+            sheet.shiftRows(3, sheet.getLastRowNum(), 1);
 
-        assertEquals("SUM(C2:C5)", getCellFormula(sheet, "C6"));
-        assertEquals("SUM(D2:D5)", getCellFormula(sheet, "D6"));
-        assertEquals("SUM(E2:E5)", getCellFormula(sheet, "E6"));
+            assertEquals("SUM(C2:C5)", getCellFormula(sheet, "C6"));
+            assertEquals("SUM(D2:D5)", getCellFormula(sheet, "D6"));
+            assertEquals("SUM(E2:E5)", getCellFormula(sheet, "E6"));
 
-        assertEquals("SUM(C3:C6)", getCellFormula(sheet, "C7"));
-        assertEquals("SUM(D3:D6)", getCellFormula(sheet, "D7"));
-        assertEquals("SUM(E3:E6)", getCellFormula(sheet, "E7"));
-        wb.close();
+            assertEquals("SUM(C3:C6)", getCellFormula(sheet, "C7"));
+            assertEquals("SUM(D3:D6)", getCellFormula(sheet, "D7"));
+            assertEquals("SUM(E3:E6)", getCellFormula(sheet, "E7"));
+        }
     }
 
     // bug 59983:  Wrong update of shared formulas after shiftRow
     @Test
     void testShiftSharedFormulas() throws Exception {
-        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx");
-        XSSFSheet sheet = wb.getSheetAt(0);
-        assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
-        assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
-        assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
+        try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx")) {
+            XSSFSheet sheet = wb.getSheetAt(0);
+            assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
+            assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
+            assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
 
-        assertEquals("SUM(C3:C5)", getCellFormula(sheet, "C6"));
-        assertEquals("SUM(D3:D5)", getCellFormula(sheet, "D6"));
-        assertEquals("SUM(E3:E5)", getCellFormula(sheet, "E6"));
+            assertEquals("SUM(C3:C5)", getCellFormula(sheet, "C6"));
+            assertEquals("SUM(D3:D5)", getCellFormula(sheet, "D6"));
+            assertEquals("SUM(E3:E5)", getCellFormula(sheet, "E6"));
 
-        sheet.shiftRows(sheet.getFirstRowNum(), 4, -1);
+            sheet.shiftRows(sheet.getFirstRowNum(), 4, -1);
 
-        assertEquals("SUM(C1:C3)", getCellFormula(sheet, "C4"));
-        assertEquals("SUM(D1:D3)", getCellFormula(sheet, "D4"));
-        assertEquals("SUM(E1:E3)", getCellFormula(sheet, "E4"));
+            assertEquals("SUM(C1:C3)", getCellFormula(sheet, "C4"));
+            assertEquals("SUM(D1:D3)", getCellFormula(sheet, "D4"));
+            assertEquals("SUM(E1:E3)", getCellFormula(sheet, "E4"));
 
-        assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C6"));
-        assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D6"));
-        assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E6"));
-        wb.close();
+            assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C6"));
+            assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D6"));
+            assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E6"));
+        }
     }
 
     // bug 60260: shift rows or rename a sheet containing a named range
     // that refers to formula with a unicode (non-ASCII) sheet name formula
     @Test
-    void shiftRowsWithUnicodeNamedRange() {
-        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("unicodeSheetName.xlsx");
-        XSSFSheet sheet = wb.getSheetAt(0);
-        sheet.shiftRows(1, 2, 3);
-        IOUtils.closeQuietly(wb);
+    void shiftRowsWithUnicodeNamedRange() throws IOException {
+        try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("unicodeSheetName.xlsx")) {
+            XSSFSheet sheet = wb.getSheetAt(0);
+            sheet.shiftRows(1, 2, 3);
+
+            Integer[] exp = { 1, null, null, 4, 2, 3, 7, 8, 9 };
+            IntStream.rangeClosed(0, 8).forEach(i -> {
+                Row row = sheet.getRow(i);
+                if (exp[i] == null) {
+                    assertNull(row);
+                } else {
+                    assertEquals(exp[i], (int)row.getCell(0).getNumericCellValue());
+                }
+            });
+        }
     }
 
     @Test
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
index 550e16f..fcabe24 100644
--- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
+++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
@@ -22,7 +22,6 @@
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -57,22 +56,23 @@
 
     @Test
     void testAddStylesToDocument() throws IOException {
-        XWPFDocument docOut = new XWPFDocument();
-        XWPFStyles styles = docOut.createStyles();
+        try (XWPFDocument docOut = new XWPFDocument()) {
+            XWPFStyles styles = docOut.createStyles();
 
-        String strStyleId = "headline1";
-        CTStyle ctStyle = CTStyle.Factory.newInstance();
+            String strStyleId = "headline1";
+            CTStyle ctStyle = CTStyle.Factory.newInstance();
 
-        ctStyle.setStyleId(strStyleId);
-        XWPFStyle s = new XWPFStyle(ctStyle);
-        styles.addStyle(s);
+            ctStyle.setStyleId(strStyleId);
+            XWPFStyle s = new XWPFStyle(ctStyle);
+            styles.addStyle(s);
 
-        assertTrue(styles.styleExist(strStyleId));
+            assertTrue(styles.styleExist(strStyleId));
 
-        XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
+            XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
 
-        styles = docIn.getStyles();
-        assertTrue(styles.styleExist(strStyleId));
+            styles = docIn.getStyles();
+            assertTrue(styles.styleExist(strStyleId));
+        }
     }
 
     /**
@@ -96,17 +96,18 @@
      * YK: tests below don't make much sense,
      * they exist only to copy xml beans to pi-ooxml-lite.jar
      */
-    @SuppressWarnings("resource")
     @Test
-    void testLanguages() {
-        XWPFDocument docOut = new XWPFDocument();
-        XWPFStyles styles = docOut.createStyles();
-        styles.setEastAsia("Chinese");
+    void testLanguages() throws IOException {
+        try (XWPFDocument docOut = new XWPFDocument()) {
+            XWPFStyles styles = docOut.createStyles();
+            styles.setEastAsia("Chinese");
+            styles.setSpellingLanguage("English");
 
-        styles.setSpellingLanguage("English");
+            CTFonts def = CTFonts.Factory.newInstance();
+            styles.setDefaultFonts(def);
 
-        CTFonts def = CTFonts.Factory.newInstance();
-        styles.setDefaultFonts(def);
+            assertEquals(1, styles.getDefaultRunStyle().getRPr().sizeOfRFontsArray());
+        }
     }
 
     @Test
@@ -130,22 +131,23 @@
 
     @Test
     void testSetStyles_Bug57254() throws IOException {
-        XWPFDocument docOut = new XWPFDocument();
-        XWPFStyles styles = docOut.createStyles();
+        try (XWPFDocument docOut = new XWPFDocument()) {
+            XWPFStyles styles = docOut.createStyles();
 
-        CTStyles ctStyles = CTStyles.Factory.newInstance();
-        String strStyleId = "headline1";
-        CTStyle ctStyle = ctStyles.addNewStyle();
+            CTStyles ctStyles = CTStyles.Factory.newInstance();
+            String strStyleId = "headline1";
+            CTStyle ctStyle = ctStyles.addNewStyle();
 
-        ctStyle.setStyleId(strStyleId);
-        styles.setStyles(ctStyles);
+            ctStyle.setStyleId(strStyleId);
+            styles.setStyles(ctStyles);
 
-        assertTrue(styles.styleExist(strStyleId));
+            assertTrue(styles.styleExist(strStyleId));
 
-        XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
+            XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
 
-        styles = docIn.getStyles();
-        assertTrue(styles.styleExist(strStyleId));
+            styles = docIn.getStyles();
+            assertTrue(styles.styleExist(strStyleId));
+        }
     }
 
     @Test