From abf2f2947862a96544e6f5cfeb7ca0e524b96ae7 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 6 Apr 2016 19:49:59 +0000 Subject: [PATCH] Slightly improve error message Fix some IntelliJ/compiler warnings Use common interfaces where possible Cleanup after testing POIFSDump git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1738032 13f79535-47bb-0310-9956-ffa450edef68 --- .../aggregates/RowRecordsAggregate.java | 23 +++---- .../org/apache/poi/ss/formula/ptg/Ptg.java | 23 +++---- .../poi/ss/formula/TestFormulaParser.java | 9 +-- .../apache/poi/poifs/dev/TestPOIFSDump.java | 3 +- .../poi/ss/formula/eval/TestFormulaBugs.java | 66 ++++++++----------- 5 files changed, 50 insertions(+), 74 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java index ea1846a384..68b3e9e482 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java @@ -145,7 +145,7 @@ public final class RowRecordsAggregate extends RecordAggregate { public RowRecord getRow(int rowIndex) { int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex(); if (rowIndex < 0 || rowIndex > maxrow) { - throw new IllegalArgumentException("The row number must be between 0 and " + maxrow); + throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex); } return _rowRecords.get(Integer.valueOf(rowIndex)); } @@ -278,9 +278,9 @@ public final class RowRecordsAggregate extends RecordAggregate { // Calculate Offset from the start of a DBCellRecord to the first Row rv.visitRecord(dbcrBuilder.build(pos)); } - for (int i=0; i< _unknownRecords.size(); i++) { + for (Record _unknownRecord : _unknownRecords) { // Potentially breaking the file here since we don't know exactly where to write these records - rv.visitRecord(_unknownRecords.get(i)); + rv.visitRecord(_unknownRecord); } } @@ -364,28 +364,24 @@ public final class RowRecordsAggregate extends RecordAggregate { public boolean isRowGroupCollapsed(int row) { int collapseRow = findEndOfRowOutlineGroup(row) + 1; - if (getRow(collapseRow) == null) { - return false; - } - return getRow( collapseRow ).getColapsed(); + return getRow(collapseRow) != null && getRow(collapseRow).getColapsed(); } public void expandRow(int rowNumber) { - int idx = rowNumber; - if (idx == -1) + if (rowNumber == -1) return; // If it is already expanded do nothing. - if (!isRowGroupCollapsed(idx)) { + if (!isRowGroupCollapsed(rowNumber)) { return; } // Find the start of the group. - int startIdx = findStartOfRowOutlineGroup(idx); + int startIdx = findStartOfRowOutlineGroup(rowNumber); RowRecord row = getRow(startIdx); // Find the end of the group. - int endIdx = findEndOfRowOutlineGroup(idx); + int endIdx = findEndOfRowOutlineGroup(rowNumber); // expand: // collapsed bit must be unset @@ -394,7 +390,7 @@ public final class RowRecordsAggregate extends RecordAggregate { // to look at the start and the end of the current group to determine which // is the enclosing group // hidden bit only is altered for this outline level. ie. don't un-collapse contained groups - if (!isRowGroupHiddenByParent(idx)) { + if (!isRowGroupHiddenByParent(rowNumber)) { for (int i = startIdx; i <= endIdx; i++) { RowRecord otherRow = getRow(i); if (row.getOutlineLevel() == otherRow.getOutlineLevel() || !isRowGroupCollapsed(i)) { @@ -450,6 +446,7 @@ public final class RowRecordsAggregate extends RecordAggregate { * @deprecated use {@link #getCellValueIterator()} instead */ public CellValueRecordInterface[] getValueRecords() { + //noinspection deprecation return _valuesAgg.getValueRecords(); } diff --git a/src/java/org/apache/poi/ss/formula/ptg/Ptg.java b/src/java/org/apache/poi/ss/formula/ptg/Ptg.java index ed719e7707..bb6fb64fbf 100644 --- a/src/java/org/apache/poi/ss/formula/ptg/Ptg.java +++ b/src/java/org/apache/poi/ss/formula/ptg/Ptg.java @@ -43,7 +43,6 @@ import org.apache.poi.util.LittleEndianOutput; public abstract class Ptg { public static final Ptg[] EMPTY_PTG_ARRAY = { }; - /** * Reads size bytes of the input stream, to create an array of Ptgs. * Extra data (beyond size) may be read if and ArrayPtgs are present. @@ -174,8 +173,8 @@ public abstract class Ptg { */ public static int getEncodedSize(Ptg[] ptgs) { int result = 0; - for (int i = 0; i < ptgs.length; i++) { - result += ptgs[i].getSize(); + for (Ptg ptg : ptgs) { + result += ptg.getSize(); } return result; } @@ -185,8 +184,7 @@ public abstract class Ptg { */ public static int getEncodedSizeWithoutArrayData(Ptg[] ptgs) { int result = 0; - for (int i = 0; i < ptgs.length; i++) { - Ptg ptg = ptgs[i]; + for (Ptg ptg : ptgs) { if (ptg instanceof ArrayPtg) { result += ArrayPtg.PLAIN_TOKEN_SIZE; } else { @@ -203,15 +201,11 @@ public abstract class Ptg { * @return number of bytes written */ public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) { - int nTokens = ptgs.length; - LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset); List arrayPtgs = null; - for (int k = 0; k < nTokens; k++) { - Ptg ptg = ptgs[k]; - + for (Ptg ptg : ptgs) { ptg.write(out); if (ptg instanceof ArrayPtg) { if (arrayPtgs == null) { @@ -221,8 +215,8 @@ public abstract class Ptg { } } if (arrayPtgs != null) { - for (int i=0;i The test name contains the @@ -53,21 +44,21 @@ public final class TestFormulaBugs { InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls"); // original bug may have thrown exception here, // or output warning to stderr - HSSFWorkbook wb = new HSSFWorkbook(is); + Workbook wb = new HSSFWorkbook(is); - HSSFSheet sheet = wb.getSheetAt(0); - HSSFRow row = sheet.getRow(1); - HSSFCell cell = row.getCell(0); + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(1); + Cell cell = row.getCell(0); // this definitely would have failed due to 27349 assertEquals("VLOOKUP(1,'DATA TABLE'!$A$8:'DATA TABLE'!$B$10,2)", cell .getCellFormula()); // We might as well evaluate the formula - HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); CellValue cv = fe.evaluate(cell); - assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType()); assertEquals(3.0, cv.getNumberValue(), 0.0); wb.close(); @@ -81,13 +72,12 @@ public final class TestFormulaBugs { */ @Test public void test27405() throws Exception { - - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("input"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("input"); // input row 0 - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); - cell = row.createCell(1); + Row row = sheet.createRow(0); + /*Cell cell =*/ row.createCell(0); + Cell cell = row.createCell(1); cell.setCellValue(1); // B1 // input row 1 row = sheet.createRow(1); @@ -113,14 +103,14 @@ public final class TestFormulaBugs { // } // use POI's evaluator as an extra sanity check - HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); CellValue cv; cv = fe.evaluate(cell); - assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType()); assertEquals(1.0, cv.getNumberValue(), 0.0); cv = fe.evaluate(row.getCell(1)); - assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cv.getCellType()); + assertEquals(Cell.CELL_TYPE_BOOLEAN, cv.getCellType()); assertEquals(true, cv.getBooleanValue()); wb.close(); @@ -132,14 +122,14 @@ public final class TestFormulaBugs { */ @Test public void test42448() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("Sheet1"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet1 = wb.createSheet("Sheet1"); - HSSFRow row = sheet1.createRow(0); - HSSFCell cell = row.createCell(0); + Row row = sheet1.createRow(0); + Cell cell = row.createCell(0); // it's important to create the referenced sheet first - HSSFSheet sheet2 = wb.createSheet("A"); // note name 'A' + Sheet sheet2 = wb.createSheet("A"); // note name 'A' // TODO - POI crashes if the formula is added before this sheet // RuntimeException("Zero length string is an invalid sheet name") // Excel doesn't crash but the formula doesn't work until it is @@ -168,16 +158,16 @@ public final class TestFormulaBugs { double expectedResult = (4.0 * 8.0 + 5.0 * 9.0) / 10.0; - HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); CellValue cv = fe.evaluate(cell); - assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType()); assertEquals(expectedResult, cv.getNumberValue(), 0.0); wb.close(); } - private static void addCell(HSSFSheet sheet, int rowIx, int colIx, + private static void addCell(Sheet sheet, int rowIx, int colIx, double value) { sheet.createRow(rowIx).createCell(colIx).setCellValue(value); } -- 2.39.5