From 09464f1fea14d00273d782f3cefde26bda2b91f1 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Fri, 8 Jan 2021 00:26:16 +0000 Subject: [PATCH] Sonar fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885253 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/TestCellStyle.java | 130 +++++------------- .../usermodel/TestFormulaEvaluatorBugs.java | 41 +++--- .../poi/hssf/usermodel/TestFormulas.java | 3 +- .../poi/hssf/usermodel/TestHSSFChart.java | 75 +++++----- 4 files changed, 92 insertions(+), 157 deletions(-) diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java index 4f856657fb..7e2ba9f8c4 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java @@ -17,6 +17,8 @@ package org.apache.poi.hssf.usermodel; +import static org.apache.poi.ss.usermodel.BorderStyle.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -30,6 +32,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import java.util.Date; +import java.util.Random; +import java.util.stream.Stream; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.usermodel.BorderStyle; @@ -189,10 +193,10 @@ public final class TestCellStyle { HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); - cs.setBorderBottom(BorderStyle.THIN); - cs.setBorderLeft(BorderStyle.THIN); - cs.setBorderRight(BorderStyle.THIN); - cs.setBorderTop(BorderStyle.THIN); + cs.setBorderBottom(THIN); + cs.setBorderLeft(THIN); + cs.setBorderRight(THIN); + cs.setBorderTop(THIN); cs.setFillForegroundColor((short) 0xA); cs.setFillPattern(FillPatternType.DIAMONDS); fnt.setColor((short) 0xf); @@ -359,45 +363,17 @@ public final class TestCellStyle { @Test public void testGetSetBorderHair() throws IOException { - try (HSSFWorkbook wb = openSample("55341_CellStyleBorder.xls")) { - HSSFSheet s = wb.getSheetAt(0); - HSSFCellStyle cs; - - cs = s.getRow(0).getCell(0).getCellStyle(); - assertEquals(BorderStyle.HAIR, cs.getBorderRight()); - - cs = s.getRow(1).getCell(1).getCellStyle(); - assertEquals(BorderStyle.DOTTED, cs.getBorderRight()); - - cs = s.getRow(2).getCell(2).getCellStyle(); - assertEquals(BorderStyle.DASH_DOT_DOT, cs.getBorderRight()); - - cs = s.getRow(3).getCell(3).getCellStyle(); - assertEquals(BorderStyle.DASHED, cs.getBorderRight()); - - cs = s.getRow(4).getCell(4).getCellStyle(); - assertEquals(BorderStyle.THIN, cs.getBorderRight()); - - cs = s.getRow(5).getCell(5).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASH_DOT_DOT, cs.getBorderRight()); - - cs = s.getRow(6).getCell(6).getCellStyle(); - assertEquals(BorderStyle.SLANTED_DASH_DOT, cs.getBorderRight()); + BorderStyle[] bs = { + HAIR, DOTTED, DASH_DOT_DOT, DASHED, THIN, MEDIUM_DASH_DOT_DOT, SLANTED_DASH_DOT, + MEDIUM_DASH_DOT, MEDIUM_DASHED, MEDIUM, THICK, DOUBLE + }; - cs = s.getRow(7).getCell(7).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASH_DOT, cs.getBorderRight()); - - cs = s.getRow(8).getCell(8).getCellStyle(); - assertEquals(BorderStyle.MEDIUM_DASHED, cs.getBorderRight()); - - cs = s.getRow(9).getCell(9).getCellStyle(); - assertEquals(BorderStyle.MEDIUM, cs.getBorderRight()); - - cs = s.getRow(10).getCell(10).getCellStyle(); - assertEquals(BorderStyle.THICK, cs.getBorderRight()); - - cs = s.getRow(11).getCell(11).getCellStyle(); - assertEquals(BorderStyle.DOUBLE, cs.getBorderRight()); + try (HSSFWorkbook wb = openSample("55341_CellStyleBorder.xls")) { + HSSFSheet s = wb.getSheetAt(0); + for (int i = 0; i 0 && isDate, "cell " + idxCell + " is not a date."); - } + @Test + public void test56563() { + Stream.of("56563a.xls", "56563b.xls").parallel().forEach(fileName -> assertDoesNotThrow(() -> { + Random rand = new Random(); + for(int i=0; i<10; i++) { + Thread.sleep(rand.nextInt(300)); + try (Workbook wb = openSample(fileName)) { + for (Row row : wb.getSheetAt(0)) { + for (Cell cell : row) { + cell.getCellStyle().getDataFormatString(); + if (cell.getCellType() == CellType.NUMERIC) { + boolean isDate = DateUtil.isCellDateFormatted(cell); + int cid = cell.getColumnIndex(); + assertFalse(cid > 0 && isDate, "cell " + cid + " is not a date."); } } } } - } catch (Throwable e) { - exception = e; } - } - - public Throwable getException() { - return exception; - } - } - - @Test - public void test56563() throws Throwable { - CellFormatBugExample threadA = new CellFormatBugExample("56563a.xls"); - threadA.start(); - CellFormatBugExample threadB = new CellFormatBugExample("56563b.xls"); - threadB.start(); - - threadA.join(); - threadB.join(); - - if(threadA.getException() != null) { - throw threadA.getException(); - } - if(threadB.getException() != null) { - throw threadB.getException(); - } + })); } @Test @@ -509,7 +449,7 @@ public final class TestCellStyle { font.setColor(Font.COLOR_RED); CellStyle style = wb.createCellStyle(); - style.setBorderBottom(BorderStyle.DOTTED); + style.setBorderBottom(DOTTED); style.setFont(font); Cell cell = row.createCell(0); @@ -522,7 +462,7 @@ public final class TestCellStyle { newCell.setCellValue("2testtext2"); CellStyle newStyle = newCell.getCellStyle(); - assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottom()); + assertEquals(DOTTED, newStyle.getBorderBottom()); assertEquals(Font.COLOR_RED, ((HSSFCellStyle) newStyle).getFont(wb).getColor()); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java index dc03e67cfd..d51fa32d4c 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java @@ -17,7 +17,9 @@ package org.apache.poi.hssf.usermodel; +import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -43,6 +45,7 @@ import org.apache.poi.ss.formula.ptg.RefPtg; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.LocaleUtil; import org.junit.jupiter.api.BeforeAll; @@ -76,7 +79,7 @@ public final class TestFormulaEvaluatorBugs { // Open the existing file, tweak one value and // re-calculate - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("44636.xls"); + HSSFWorkbook wb = openSampleWorkbook("44636.xls"); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(0); @@ -129,7 +132,7 @@ public final class TestFormulaEvaluatorBugs { @Test public void test44297() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("44297.xls"); + HSSFWorkbook wb = openSampleWorkbook("44297.xls"); HSSFRow row; HSSFCell cell; @@ -192,7 +195,7 @@ public final class TestFormulaEvaluatorBugs { */ @Test public void test44410() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SingleLetterRanges.xls"); + HSSFWorkbook wb = openSampleWorkbook("SingleLetterRanges.xls"); HSSFSheet sheet = wb.getSheetAt(0); @@ -273,27 +276,23 @@ public final class TestFormulaEvaluatorBugs { @Test public void testClassCast_bug44861() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("44861.xls"); - - // Check direct - HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); - - // And via calls - int numSheets = wb.getNumberOfSheets(); - for (int i = 0; i < numSheets; i++) { - HSSFSheet s = wb.getSheetAt(i); - HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb); - - for (Iterator rows = s.rowIterator(); rows.hasNext();) { - HSSFRow r = (HSSFRow)rows.next(); - for (Iterator cells = r.cellIterator(); cells.hasNext();) { - HSSFCell c = (HSSFCell)cells.next(); - eval.evaluateFormulaCell(c); + try (HSSFWorkbook wb = openSampleWorkbook("44861.xls")) { + // Check direct + HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); + + // And via calls + for (Sheet s : wb) { + HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb); + + for (Row r : s) { + for (Cell c : r) { + CellType ct = eval.evaluateFormulaCell(c); + assertNotNull(ct); + } } } - } - wb.close(); + } } @Test diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java index 8ef9614e57..ee15db7700 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.usermodel; import static org.apache.poi.hssf.HSSFTestDataSamples.writeOutAndReadBack; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; @@ -864,7 +865,7 @@ public final class TestFormulas { public void testMissingArgPtg() throws IOException { try (HSSFWorkbook wb = new HSSFWorkbook()) { HSSFCell cell = wb.createSheet("Sheet1").createRow(4).createCell(0); - cell.setCellFormula("IF(A1=\"A\",1,)"); + assertDoesNotThrow(() -> cell.setCellFormula("IF(A1=\"A\",1,)")); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java index 494072402c..07900573d7 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFChart.java @@ -17,7 +17,9 @@ package org.apache.poi.hssf.usermodel; +import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; @@ -38,7 +40,7 @@ public final class TestHSSFChart { @Test public void testSingleChart() { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithChart.xls"); + HSSFWorkbook wb = openSampleWorkbook("WithChart.xls"); HSSFSheet s1 = wb.getSheetAt(0); HSSFSheet s2 = wb.getSheetAt(1); @@ -68,7 +70,7 @@ public final class TestHSSFChart { @Test public void testTwoCharts() { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithTwoCharts.xls"); + HSSFWorkbook wb = openSampleWorkbook("WithTwoCharts.xls"); HSSFSheet s1 = wb.getSheetAt(0); HSSFSheet s2 = wb.getSheetAt(1); @@ -101,7 +103,7 @@ public final class TestHSSFChart { @Test public void testThreeCharts() { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithThreeCharts.xls"); + HSSFWorkbook wb = openSampleWorkbook("WithThreeCharts.xls"); HSSFSheet s1 = wb.getSheetAt(0); HSSFSheet s2 = wb.getSheetAt(1); @@ -142,7 +144,7 @@ public final class TestHSSFChart { @Test public void testExistingSheet3() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49581.xls"); + HSSFWorkbook wb = openSampleWorkbook("49581.xls"); HSSFSheet sheet = wb.getSheetAt( 2 ) ; HSSFChart[] charts = HSSFChart.getSheetCharts( sheet ) ; @@ -165,7 +167,7 @@ public final class TestHSSFChart { @Test public void testExistingSheet2() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49581.xls"); + HSSFWorkbook wb = openSampleWorkbook("49581.xls"); HSSFSheet sheet = wb.getSheetAt( 1 ) ; HSSFChart[] charts = HSSFChart.getSheetCharts( sheet ) ; @@ -197,45 +199,38 @@ public final class TestHSSFChart { @Test public void testExistingSheet1() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49581.xls"); - HSSFSheet sheet = wb.getSheetAt( 0 ) ; - HSSFChart[] charts = HSSFChart.getSheetCharts( sheet ) ; + try (HSSFWorkbook wb = openSampleWorkbook("49581.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); + HSSFChart[] charts = HSSFChart.getSheetCharts(sheet); - for ( HSSFChart chart : charts ) { - //System.out.println( chart.getType() ) ; - HSSFSeries[] seriesArray = chart.getSeries() ; - //System.out.println( "seriesArray.length=" + seriesArray.length ) ; - for ( HSSFSeries series : seriesArray ) - { - //System.out.println( "serie.getNumValues()=" + series.getNumValues() ) ; - CellRangeAddressBase range ; - - range = series.getValuesCellRange() ; - //System.out.println( range.toString() ) ; - range.setLastRow( range.getLastRow() + 1 ) ; - series.setValuesCellRange( range ) ; - - range = series.getCategoryLabelsCellRange() ; - //System.out.println( range.toString() ) ; - range.setLastRow( range.getLastRow() + 1 ) ; - series.setCategoryLabelsCellRange( range ) ; - } + for (HSSFChart chart : charts) { + for (HSSFSeries series : chart.getSeries()) { + CellRangeAddressBase range; + + range = series.getValuesCellRange(); + range.setLastRow(range.getLastRow() + 1); + series.setValuesCellRange(range); - for ( int id = 0 ; id < 2 ; id++ ) - { - HSSFSeries newSeries = chart.createSeries() ; - newSeries.setValuesCellRange( new CellRangeAddress( 1 + id, 4, 3, 3 ) ) ; - String oldSeriesTitle = newSeries.getSeriesTitle() ; - if ( oldSeriesTitle != null ) - { - //System.out.println( "old series title: " + oldSeriesTitle ) ; - newSeries.setSeriesTitle( "new series" ) ; + range = series.getCategoryLabelsCellRange(); + range.setLastRow(range.getLastRow() + 1); + series.setCategoryLabelsCellRange(range); + } + + for (int id = 0; id < 2; id++) { + HSSFSeries newSeries = chart.createSeries(); + assertNotNull(newSeries); + newSeries.setValuesCellRange(new CellRangeAddress(1 + id, 4, 3, 3)); + String oldSeriesTitle = newSeries.getSeriesTitle(); + if (oldSeriesTitle != null) { + newSeries.setSeriesTitle("new series"); + } } } - } - HSSFChart chart = charts[ 2 ] ; - chart.removeSeries( chart.getSeries()[ 0 ] ) ; + HSSFChart chart = charts[2]; + chart.removeSeries(chart.getSeries()[0]); + assertEquals(2, chart.getSeries().length); + } } /** @@ -243,7 +238,7 @@ public final class TestHSSFChart { */ @Test public void test26862() throws IOException, Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleChart.xls"); + HSSFWorkbook wb = openSampleWorkbook("SimpleChart.xls"); HSSFSheet srcSheet = wb.getSheetAt(0); HSSFChart[] srcCharts = HSSFChart.getSheetCharts(srcSheet); assertEquals(1, srcCharts.length); -- 2.39.5