From 82eff19e0018e311d2ad7dd8e2e214ae48df86f3 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 16 Jul 2015 03:41:57 +0000 Subject: [PATCH] Fix some Eclipse warnings and adjust use of Generics, Comments, close() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691308 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/usermodel/examples/CreateCells.java | 2 + .../xssf/usermodel/examples/BigGridDemo.java | 38 +- src/java/org/apache/poi/hpsf/Section.java | 2 +- .../poi/ss/formula/eval/OperandResolver.java | 2 +- .../poi/ss/formula/functions/Countif.java | 2 +- .../poi/ss/formula/functions/Sumifs.java | 1 + .../apache/poi/hssf/usermodel/TestBugs.java | 348 +++++++++--------- 7 files changed, 203 insertions(+), 192 deletions(-) diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateCells.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateCells.java index ff8a0b73ca..d2141b0505 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateCells.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateCells.java @@ -50,5 +50,7 @@ public class CreateCells { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java index 2dbfccec41..d9928151aa 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java @@ -168,25 +168,29 @@ public class BigGridDemo { private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException { ZipFile zip = ZipHelper.openZipFile(zipfile); - ZipOutputStream zos = new ZipOutputStream(out); - - @SuppressWarnings("unchecked") - Enumeration en = (Enumeration) zip.entries(); - while (en.hasMoreElements()) { - ZipEntry ze = en.nextElement(); - if(!ze.getName().equals(entry)){ - zos.putNextEntry(new ZipEntry(ze.getName())); - InputStream is = zip.getInputStream(ze); - copyStream(is, zos); - is.close(); + try { + ZipOutputStream zos = new ZipOutputStream(out); + + @SuppressWarnings("unchecked") + Enumeration en = (Enumeration) zip.entries(); + while (en.hasMoreElements()) { + ZipEntry ze = en.nextElement(); + if(!ze.getName().equals(entry)){ + zos.putNextEntry(new ZipEntry(ze.getName())); + InputStream is = zip.getInputStream(ze); + copyStream(is, zos); + is.close(); + } } + zos.putNextEntry(new ZipEntry(entry)); + InputStream is = new FileInputStream(tmpfile); + copyStream(is, zos); + is.close(); + + zos.close(); + } finally { + zip.close(); } - zos.putNextEntry(new ZipEntry(entry)); - InputStream is = new FileInputStream(tmpfile); - copyStream(is, zos); - is.close(); - - zos.close(); } private static void copyStream(InputStream in, OutputStream out) throws IOException { diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java index 0c97a0e68a..3c8e5b7ce8 100644 --- a/src/java/org/apache/poi/hpsf/Section.java +++ b/src/java/org/apache/poi/hpsf/Section.java @@ -285,7 +285,7 @@ public class Section /* * Extract the dictionary (if available). */ - dictionary = (Map) getProperty(0); + dictionary = (Map) getProperty(0); } diff --git a/src/java/org/apache/poi/ss/formula/eval/OperandResolver.java b/src/java/org/apache/poi/ss/formula/eval/OperandResolver.java index 8e0ea3ba85..af4cb81b31 100644 --- a/src/java/org/apache/poi/ss/formula/eval/OperandResolver.java +++ b/src/java/org/apache/poi/ss/formula/eval/OperandResolver.java @@ -57,7 +57,7 @@ public final class OperandResolver { */ public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { - ValueEval result; + final ValueEval result; if (arg instanceof RefEval) { result = chooseSingleElementFromRef((RefEval) arg); } else if (arg instanceof AreaEval) { diff --git a/src/java/org/apache/poi/ss/formula/functions/Countif.java b/src/java/org/apache/poi/ss/formula/functions/Countif.java index 01f608a54b..9023815c36 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Countif.java +++ b/src/java/org/apache/poi/ss/formula/functions/Countif.java @@ -491,7 +491,7 @@ public final class Countif extends Fixed2ArgFunction { */ private static ValueEval evaluateCriteriaArg(ValueEval arg, int srcRowIndex, int srcColumnIndex) { try { - return OperandResolver.getSingleValue(arg, srcRowIndex, (short)srcColumnIndex); + return OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } diff --git a/src/java/org/apache/poi/ss/formula/functions/Sumifs.java b/src/java/org/apache/poi/ss/formula/functions/Sumifs.java index 087b72bc12..ebdddb69f7 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Sumifs.java +++ b/src/java/org/apache/poi/ss/formula/functions/Sumifs.java @@ -54,6 +54,7 @@ public final class Sumifs implements FreeRefFunction { public static final FreeRefFunction instance = new Sumifs(); public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { + // need at least 3 arguments and need to have an odd number of arguments (sum-range plus x*(criteria_range, criteria)) if(args.length < 3 || args.length % 2 == 0) { return ErrorEval.VALUE_INVALID; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 5223bb5b06..af6e8bb036 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -32,6 +32,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -110,9 +111,12 @@ public final class TestBugs extends BaseTestBugzillaIssues { File file; try { file = TempFile.createTempFile(simpleFileName + "#", ".xls"); - FileOutputStream out = new FileOutputStream(file); - wb.write(out); - out.close(); + OutputStream out = new FileOutputStream(file); + try { + wb.write(out); + } finally { + out.close(); + } } catch (IOException e) { throw new RuntimeException(e); } @@ -178,7 +182,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { cell = row.createCell(3); // Write test - cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellType(Cell.CELL_TYPE_STRING); setCellText(cell, "a test"); // change existing numeric cell value @@ -460,7 +464,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFRow row = sheet.getRow(i); if (row != null) { HSSFCell cell = row .getCell(0); - assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); count++; } } @@ -1151,13 +1155,13 @@ public final class TestBugs extends BaseTestBugzillaIssues { } private static void confirmCachedValue(double expectedValue, HSSFCell cell) { - assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType()); + assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType()); assertEquals(expectedValue, cell.getNumericCellValue(), 0.0); } private static void confirmCachedValue(String expectedValue, HSSFCell cell) { - assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCachedFormulaResultType()); + assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(Cell.CELL_TYPE_STRING, cell.getCachedFormulaResultType()); assertEquals(expectedValue, cell.getRichStringCellValue().getString()); } @@ -1270,7 +1274,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { s = wb.getSheet("OneVariable Table Completed"); r = s.getRow(3); c = r.getCell(4); - assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType()); + assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); // TODO - check the formula once tables and // arrays are properly supported @@ -1280,7 +1284,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { s = wb.getSheet("TwoVariable Table Example"); r = s.getRow(3); c = r.getCell(4); - assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType()); + assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); // TODO - check the formula once tables and // arrays are properly supported @@ -1333,7 +1337,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertTrue(wb.isSheetVeryHidden(2)); // Change 0 to be very hidden, and re-load - wb.setSheetHidden(0, HSSFWorkbook.SHEET_STATE_VERY_HIDDEN); + wb.setSheetHidden(0, Workbook.SHEET_STATE_VERY_HIDDEN); HSSFWorkbook nwb = writeOutAndReadBack(wb); @@ -1579,7 +1583,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { OPOIFSFileSystem fs = new OPOIFSFileSystem( HSSFITestDataProvider.instance.openWorkbookStream("46904.xls")); new HSSFWorkbook(fs.getRoot(), false).close(); - fail(); + fail("Should catch exception here"); } catch(OldExcelFormatException e) { assertTrue(e.getMessage().startsWith( "The supplied spreadsheet seems to be Excel" @@ -1590,7 +1594,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFITestDataProvider.instance.openWorkbookStream("46904.xls")); try { new HSSFWorkbook(fs.getRoot(), false).close(); - fail(); + fail("Should catch exception here"); } finally { fs.close(); } @@ -1659,11 +1663,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { public void bug47251() { // Firstly, try with one that triggers on InterfaceHdrRecord openSample("47251.xls"); - + // Now with one that triggers on NoteRecord openSample("47251_1.xls"); } - + /** * Round trip a file with an unusual UnicodeString/ExtRst record parts */ @@ -1671,35 +1675,35 @@ public final class TestBugs extends BaseTestBugzillaIssues { public void bug47847() throws Exception { HSSFWorkbook wb = openSample("47847.xls"); assertEquals(3, wb.getNumberOfSheets()); - + // Find the SST record UnicodeString withExt = wb.getWorkbook().getSSTString(0); UnicodeString withoutExt = wb.getWorkbook().getSSTString(31); - + assertEquals("O:Alloc:Qty", withExt.getString()); assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004); - + assertEquals("RT", withoutExt.getString()); assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000); - + // Something about continues... - + // Write out and re-read wb = writeOutAndReadBack(wb); assertEquals(3, wb.getNumberOfSheets()); - + // Check it's the same now withExt = wb.getWorkbook().getSSTString(0); withoutExt = wb.getWorkbook().getSSTString(31); - + assertEquals("O:Alloc:Qty", withExt.getString()); assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004); - + assertEquals("RT", withoutExt.getString()); assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000); } - + /** * Problem with cloning a sheet with a chart * contained in it. @@ -1708,21 +1712,21 @@ public final class TestBugs extends BaseTestBugzillaIssues { public void bug49096() throws Exception { HSSFWorkbook wb = openSample("49096.xls"); assertEquals(1, wb.getNumberOfSheets()); - + assertNotNull(wb.getSheetAt(0)); wb.cloneSheet(0); assertEquals(2, wb.getNumberOfSheets()); - + wb = writeOutAndReadBack(wb); assertEquals(2, wb.getNumberOfSheets()); } - + /** - * Newly created sheets need to get a + * Newly created sheets need to get a * proper TabID, otherwise print setup * gets confused on them. * Also ensure that print setup refs are - * by reference not value + * by reference not value */ @Test public void bug46664() throws Exception { @@ -1744,9 +1748,9 @@ public final class TestBugs extends BaseTestBugzillaIssues { 0, //start row 0 //end row ); - + wb = writeOutAndReadBack(wb); - + // Ensure the tab index TabIdRecord tr = null; for(Record r : wb.getWorkbook().getRecords()) { @@ -1757,108 +1761,106 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertNotNull(tr); assertEquals(1, tr._tabids.length); assertEquals(0, tr._tabids[0]); - + // Ensure the print setup assertEquals("new_sheet!$A$1:$C$1", wb.getPrintArea(0)); assertEquals("new_sheet!$A$1:$C$1", wb.getName("Print_Area").getRefersToFormula()); - + // Needs reference not value NameRecord nr = wb.getWorkbook().getNameRecord( wb.getNameIndex("Print_Area") - ); + ); assertEquals("Print_Area", nr.getNameText()); assertEquals(1, nr.getNameDefinition().length); assertEquals( - "new_sheet!$A$1:$C$1", + "new_sheet!$A$1:$C$1", ((Area3DPtg)nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb)) ); assertEquals('R', nr.getNameDefinition()[0].getRVAType()); } - + /** - * Problems with formula references to + * Problems with formula references to * sheets via URLs */ @Test public void bug45970() throws Exception { HSSFWorkbook wb = openSample("FormulaRefs.xls"); assertEquals(3, wb.getNumberOfSheets()); - + HSSFSheet s = wb.getSheetAt(0); HSSFRow row; - + row = s.getRow(0); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(1); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(2); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("Sheet1!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(3); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(4); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + // Change 4 row.getCell(1).setCellFormula("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2"); row.getCell(1).setCellValue(123.0); - + // Add 5 row = s.createRow(5); row.createCell(1, Cell.CELL_TYPE_FORMULA); row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1"); row.getCell(1).setCellValue(234.0); - - + + // Re-test wb = writeOutAndReadBack(wb); s = wb.getSheetAt(0); - + row = s.getRow(0); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); assertEquals(112.0, row.getCell(1).getNumericCellValue(),0); - + row = s.getRow(1); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(2); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("Sheet1!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + row = s.getRow(3); assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); - + // TODO - Fix these so they work... - if(1==2) { - row = s.getRow(4); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); - assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula()); - assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0); - - row = s.getRow(5); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); - assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula()); - assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0); - } + /*row = s.getRow(4); + assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula()); + assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0); + + row = s.getRow(5); + assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula()); + assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/ } - + /** * Test for a file with NameRecord with NameCommentRecord comments */ @@ -1867,26 +1869,26 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook wb = openSample("49185.xls"); Name name = wb.getName("foobarName"); assertEquals("This is a comment", name.getComment()); - + // Rename the name, comment comes with it name.setNameName("ChangedName"); assertEquals("This is a comment", name.getComment()); - + // Save and re-check wb = writeOutAndReadBack(wb); name = wb.getName("ChangedName"); assertEquals("This is a comment", name.getComment()); - + // Now try to change it name.setComment("Changed Comment"); assertEquals("Changed Comment", name.getComment()); - + // Save and re-check wb = writeOutAndReadBack(wb); name = wb.getName("ChangedName"); assertEquals("Changed Comment", name.getComment()); } - + /** * Vertically aligned text */ @@ -1897,11 +1899,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { Row r = s.getRow(0); Cell rotated = r.getCell(0); Cell normal = r.getCell(1); - + // Check the current ones assertEquals(0, normal.getCellStyle().getRotation()); assertEquals(0xff, rotated.getCellStyle().getRotation()); - + // Add a new style, also rotated CellStyle cs = wb.createCellStyle(); cs.setRotation((short)0xff); @@ -1909,22 +1911,22 @@ public final class TestBugs extends BaseTestBugzillaIssues { nc.setCellValue("New Rotated Text"); nc.setCellStyle(cs); assertEquals(0xff, nc.getCellStyle().getRotation()); - + // Write out and read back wb = writeOutAndReadBack(wb); - + // Re-check s = wb.getSheetAt(0); r = s.getRow(0); rotated = r.getCell(0); normal = r.getCell(1); nc = r.getCell(2); - + assertEquals(0, normal.getCellStyle().getRotation()); assertEquals(0xff, rotated.getCellStyle().getRotation()); assertEquals(0xff, nc.getCellStyle().getRotation()); } - + /** * Setting the user style name on custom styles */ @@ -1934,26 +1936,26 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFSheet s = wb.createSheet("Test"); HSSFRow r = s.createRow(0); HSSFCell c = r.createCell(0); - + HSSFCellStyle cs1 = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle(); - + assertEquals(21, cs1.getIndex()); cs1.setUserStyleName("Testing"); - + assertEquals(22, cs2.getIndex()); cs2.setUserStyleName("Testing 2"); - + assertEquals(23, cs3.getIndex()); cs3.setUserStyleName("Testing 3"); - + // Set one c.setCellStyle(cs1); - + // Write out and read back wb = writeOutAndReadBack(wb); - + // Re-check assertEquals("Testing", wb.getCellStyleAt((short)21).getUserStyleName()); assertEquals("Testing 2", wb.getCellStyleAt((short)22).getUserStyleName()); @@ -1966,7 +1968,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { short numCellStyles = wb.getNumCellStyles(); List namedStyles = Arrays.asList( "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5", - "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4", + "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4", "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3", "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3", "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text", @@ -1982,19 +1984,19 @@ public final class TestBugs extends BaseTestBugzillaIssues { } } assertTrue(namedStyles.containsAll(collecteddStyles)); - } - + } + /** * Regression with the PageSettingsBlock */ @Test public void bug49931() throws Exception { HSSFWorkbook wb = openSample("49931.xls"); - + assertEquals(1, wb.getNumberOfSheets()); assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString()); } - + /** * Missing left/right/centre options on a footer */ @@ -2009,7 +2011,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("Left text should be empty", "", f.getLeft()); assertEquals("Right text should be empty", "", f.getRight()); assertEquals( - "Center text should contain the illegal value", + "Center text should contain the illegal value", "BlahBlah blah blah ", f.getCenter() ); } @@ -2028,7 +2030,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook wb = openSample("50426.xls"); writeOutAndReadBack(wb); } - + /** * Last row number when shifting rows * @throws IOException @@ -2047,10 +2049,10 @@ public final class TestBugs extends BaseTestBugzillaIssues { Row row3 = sheet.createRow(2); Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING); cellA_3.setCellValue("Cell A,3"); - + // Test the last Row number it currently correct assertEquals(2, sheet.getLastRowNum()); - + // Shift the first row to the end sheet.shiftRows(0, 0, 3); assertEquals(3, sheet.getLastRowNum()); @@ -2058,7 +2060,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue()); assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue()); assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue()); - + // Shift the 2nd row up to the first one sheet.shiftRows(1, 1, -1); assertEquals(3, sheet.getLastRowNum()); @@ -2074,10 +2076,10 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue()); assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue()); assertEquals(-1, sheet.getRow(3).getLastCellNum()); - + // Now zap the empty 4th row - won't do anything sheet.removeRow(sheet.getRow(3)); - + // Test again the last row number which should be 2 assertEquals(2, sheet.getLastRowNum()); assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue()); @@ -2086,7 +2088,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { workbook.close(); } - + /** * If you send a file between Excel and OpenOffice enough, something * will turn the "General" format into "GENERAL" @@ -2098,7 +2100,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFRow r17 = s.getRow(16); HSSFRow r18 = s.getRow(17); HSSFDataFormatter df = new HSSFDataFormatter(); - + assertEquals(10.0, r17.getCell(1).getNumericCellValue(), 0); assertEquals(20.0, r17.getCell(2).getNumericCellValue(), 0); assertEquals(20.0, r17.getCell(3).getNumericCellValue(), 0); @@ -2108,7 +2110,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("10", df.formatCellValue(r17.getCell(1))); assertEquals("20", df.formatCellValue(r17.getCell(2))); assertEquals("20", df.formatCellValue(r17.getCell(3))); - + assertEquals(16.0, r18.getCell(1).getNumericCellValue(), 0); assertEquals(35.0, r18.getCell(2).getNumericCellValue(), 0); assertEquals(123.0, r18.getCell(3).getNumericCellValue(), 0); @@ -2119,7 +2121,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("35", df.formatCellValue(r18.getCell(2))); assertEquals("123", df.formatCellValue(r18.getCell(3))); } - + /** * A protected sheet with comments, when written out by * POI, ends up upsetting excel. @@ -2128,32 +2130,32 @@ public final class TestBugs extends BaseTestBugzillaIssues { */ @Test public void bug50833() throws Exception { - Biff8EncryptionKey.setCurrentUserPassword(null); - + Biff8EncryptionKey.setCurrentUserPassword(null); + HSSFWorkbook wb = openSample("50833.xls"); HSSFSheet s = wb.getSheetAt(0); assertEquals("Sheet1", s.getSheetName()); assertEquals(false, s.getProtect()); - + HSSFCell c = s.getRow(0).getCell(0); assertEquals("test cell value", c.getRichStringCellValue().getString()); - + HSSFComment cmt = c.getCellComment(); assertNotNull(cmt); assertEquals("Robert Lawrence", cmt.getAuthor()); assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString()); - + // Reload wb = writeOutAndReadBack(wb); s = wb.getSheetAt(0); c = s.getRow(0).getCell(0); - + // Re-check the comment cmt = c.getCellComment(); assertNotNull(cmt); assertEquals("Robert Lawrence", cmt.getAuthor()); assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString()); - + // TODO Identify what excel doesn't like, and check for that } @@ -2168,26 +2170,26 @@ public final class TestBugs extends BaseTestBugzillaIssues { /** * The spec says that ChartEndObjectRecord has 6 reserved - * bytes on the end, but we sometimes find files without... + * bytes on the end, but we sometimes find files without... */ @Test public void bug50939() throws Exception { HSSFWorkbook wb = openSample("50939.xls"); assertEquals(2, wb.getNumberOfSheets()); } - + @Test public void bug49219() throws Exception { HSSFWorkbook wb = openSample("49219.xls"); assertEquals(1, wb.getNumberOfSheets()); assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue()); } - + @Test public void bug48968() throws Exception { HSSFWorkbook wb = openSample("48968.xls"); assertEquals(1, wb.getNumberOfSheets()); - + DataFormatter fmt = new DataFormatter(); // Check the dates @@ -2200,7 +2202,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { // There is no such thing as 00/01/1900... assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue()); assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100)); - + // Check the cached values assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula()); assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0); @@ -2208,7 +2210,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0); assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula()); assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0); - + // Re-evaulate and check HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula()); @@ -2217,11 +2219,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0); assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula()); assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0); - + // Push the time forward a bit and check double date = s.getRow(0).getCell(0).getNumericCellValue(); s.getRow(0).getCell(0).setCellValue(date + 1.26); - + HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula()); assertEquals(11.0+6.0, s.getRow(5).getCell(0).getNumericCellValue(), 0); @@ -2230,9 +2232,9 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula()); assertEquals(54.0+24.0-60, s.getRow(7).getCell(0).getNumericCellValue(), 0); } - + /** - * Mixture of Ascii and Unicode strings in a + * Mixture of Ascii and Unicode strings in a * NameComment record */ @Test @@ -2242,25 +2244,25 @@ public final class TestBugs extends BaseTestBugzillaIssues { wb = writeOutAndReadBack(wb); assertEquals(1, wb.getNumberOfSheets()); } - + /** * File with exactly 256 data blocks (+header block) - * shouldn't break on POIFS loading + * shouldn't break on POIFS loading */ @SuppressWarnings("resource") @Test public void bug51461() throws Exception { byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls"); - + HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( new ByteArrayInputStream(data)).getRoot(), false); HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem( new ByteArrayInputStream(data)).getRoot(), false); - + assertEquals(2, wbPOIFS.getNumberOfSheets()); assertEquals(2, wbNPOIFS.getNumberOfSheets()); } - + /** * Large row numbers and NPOIFS vs POIFS */ @@ -2268,22 +2270,22 @@ public final class TestBugs extends BaseTestBugzillaIssues { @Test public void bug51535() throws Exception { byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls"); - + HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( new ByteArrayInputStream(data)).getRoot(), false); HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem( new ByteArrayInputStream(data)).getRoot(), false); - + for(HSSFWorkbook wb : new HSSFWorkbook[] {wbPOIFS, wbNPOIFS}) { assertEquals(3, wb.getNumberOfSheets()); - + // Check directly HSSFSheet s = wb.getSheetAt(0); assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue()); assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue()); assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue()); assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue()); - + // Extract and check ExcelExtractor ex = new ExcelExtractor(wb); String text = ex.getText(); @@ -2300,7 +2302,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook wb = openSample("51670.xls"); writeOutAndReadBack(wb); } - + /** * Sum across multiple workbooks * eg =SUM($Sheet2.A1:$Sheet3.A1) @@ -2310,15 +2312,15 @@ public final class TestBugs extends BaseTestBugzillaIssues { public void test48703() { HSSFWorkbook wb = openSample("48703.xls"); assertEquals(3, wb.getNumberOfSheets()); - + // Check reading the formula Sheet sheet = wb.getSheetAt(0); Row r = sheet.getRow(0); Cell c = r.getCell(0); - + assertEquals("SUM(Sheet2!A1:Sheet3!A1)", c.getCellFormula()); assertEquals(4.0, c.getNumericCellValue(), 0); - + // Check the evaluated result HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb); eval.evaluateFormulaCell(c); @@ -2356,7 +2358,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { // make sure we are still readable writeOutAndReadBack(workbook); } - + /** * Note - part of this test is still failing, see * {@link TestUnfixedBugs#test49612()} @@ -2381,7 +2383,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("evaluating d1", 30.0, eval.evaluate(d1).getNumberValue(), 0.001); assertEquals("evaluating e1", 30.0, eval.evaluate(e1).getNumberValue(), 0.001); } - + @Test public void bug51675(){ final List list = new ArrayList(); @@ -2398,13 +2400,13 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertTrue(list.get(list.size()-1).intValue() == UnknownRecord.BITMAP_00E9); assertTrue(list.get(list.size()-2).intValue() == UnknownRecord.HEADER_FOOTER_089C); } - + @Test public void bug52272() throws IOException{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sh = wb.createSheet(); HSSFPatriarch p = sh.createDrawingPatriarch(); - + HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor()); s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); @@ -2419,9 +2421,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook(); wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG); assertEquals(wb.getAllPictures().size(), 1); + wb.close(); wb.close(); wb = new HSSFWorkbook(); + wb = writeOutAndReadBack((HSSFWorkbook) wb); assertEquals(wb.getAllPictures().size(), 0); wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG); @@ -2482,7 +2486,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(0); HSSFCellStyle rstyle = row.getRowStyle(); - assertEquals(rstyle.getBorderBottom(), HSSFCellStyle.BORDER_DOUBLE); + assertEquals(rstyle.getBorderBottom(), CellStyle.BORDER_DOUBLE); } @Test @@ -2494,7 +2498,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { } finally { Biff8EncryptionKey.setCurrentUserPassword(null); } - + // One using the only-recently-documented encryption header type 4, // and the RC4 CryptoAPI encryption header structure try { @@ -2518,7 +2522,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { } assertEquals(0, comments); } - + /** * Files initially created with Excel 2010 can have >3 CF rules */ @@ -2526,10 +2530,10 @@ public final class TestBugs extends BaseTestBugzillaIssues { public void bug56482() { HSSFWorkbook wb = openSample("56482.xls"); assertEquals(1, wb.getNumberOfSheets()); - + HSSFSheet sheet = wb.getSheetAt(0); HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting(); - + assertEquals(5, cf.getNumConditionalFormattings()); } @@ -2541,11 +2545,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { InputStream stream = new FileInputStream(file); try { POIFSFileSystem fs = new POIFSFileSystem(stream); - wb = new HSSFWorkbook(fs); + wb = new HSSFWorkbook(fs); } finally { stream.close(); } - + assertEquals(3, wb.getNumberOfSheets()); wb.removeSheetAt(0); assertEquals(2, wb.getNumberOfSheets()); @@ -2556,7 +2560,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals(1, wb.getNumberOfSheets()); wb.removeSheetAt(0); assertEquals(0, wb.getNumberOfSheets()); - + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); assertEquals(0, wb.getNumberOfSheets()); } @@ -2564,17 +2568,17 @@ public final class TestBugs extends BaseTestBugzillaIssues { @Test public void bug56325a() throws IOException { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("56325a.xls"); - + HSSFSheet sheet = wb.cloneSheet(2); wb.setSheetName(3, "Clone 1"); sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3")); wb.setPrintArea(3, "$A$4:$C$10"); - + sheet = wb.cloneSheet(2); wb.setSheetName(4, "Clone 2"); sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3")); wb.setPrintArea(4, "$A$4:$C$10"); - + wb.removeSheetAt(2); Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); @@ -2587,51 +2591,51 @@ public final class TestBugs extends BaseTestBugzillaIssues { // fOut.close(); // } } - + /** * Formulas which reference named ranges, either in other * sheets, or workbook scoped but in other workbooks. - * Used to fail with + * Used to fail with * java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval) */ @Test public void bug56737() throws IOException { Workbook wb = openSample("56737.xls"); - + // Check the named range definitions Name nSheetScope = wb.getName("NR_To_A1"); Name nWBScope = wb.getName("NR_Global_B2"); assertNotNull(nSheetScope); assertNotNull(nWBScope); - + assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula()); assertEquals("Defines!$B$2", nWBScope.getRefersToFormula()); - + // Check the different kinds of formulas Sheet s = wb.getSheetAt(0); Cell cRefSName = s.getRow(1).getCell(3); Cell cRefWName = s.getRow(2).getCell(3); - + assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula()); - + // TODO Correct this, so that the filename is shown too, see bug #56742 // This is what Excel itself shows //assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula()); // TODO This isn't right, but it's what we currently generate.... assertEquals("NR_Global_B2", cRefWName.getCellFormula()); - + // Try to evaluate them FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue()); assertEquals(142, (int)eval.evaluate(cRefWName).getNumberValue()); - + // Try to evaluate everything eval.evaluateAll(); } - + /** - * ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to + * ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to * ExtendedFormatRecord when removing un-used styles */ @Test @@ -2639,20 +2643,20 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook workbook = new HSSFWorkbook( ); HSSFCellStyle style = workbook.createCellStyle(); HSSFCellStyle newStyle = workbook.createCellStyle(); - + HSSFSheet mySheet = workbook.createSheet(); HSSFRow row = mySheet.createRow(0); HSSFCell cell = row.createCell(0); - + // Use style cell.setCellStyle(style); // Switch to newStyle, style is now un-used cell.setCellStyle(newStyle); - + // Optimise HSSFOptimiser.optimiseCellStyles(workbook); } - + /** * Intersection formula ranges, eg =(C2:D3 D3:E4) */ @@ -2665,9 +2669,9 @@ public final class TestBugs extends BaseTestBugzillaIssues { Cell intF = s.getRow(2).getCell(0); assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); - + assertEquals("(C2:D3 D3:E4)", intF.getCellFormula()); - + // Check we can evaluate it correctly FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); assertEquals("4.0", eval.evaluate(intF).formatAsString()); @@ -2679,7 +2683,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { Sheet s = wb.getSheetAt(0); for(int row = 0;row < 7;row++) { assertEquals("A$1+B$1", s.getRow(row).getCell(2).getCellFormula()); - } + } } /** @@ -2692,7 +2696,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { assertEquals("International Communication Services SA", s.getRow(2).getCell(0).getStringCellValue()); assertEquals("Saudi Arabia-Riyadh", s.getRow(210).getCell(0).getStringCellValue()); } - + /** * Read, write, read for formulas point to cells in other files. * See {@link #bug46670()} for the main test, this just @@ -2705,20 +2709,20 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook wb; Sheet s; Cell c; - + // Expected values String refLocal = "'[refs/airport.xls]Sheet1'!$A$2"; String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2"; - + // Check we can read them correctly wb = openSample("46670_local.xls"); s = wb.getSheetAt(0); assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula()); - + wb = openSample("46670_http.xls"); s = wb.getSheetAt(0); assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula()); - + // Now try to set them to the same values, and ensure that // they end up as they did before, even with a save and re-load wb = openSample("46670_local.xls"); @@ -2726,18 +2730,18 @@ public final class TestBugs extends BaseTestBugzillaIssues { c = s.getRow(0).getCell(0); c.setCellFormula(refLocal); assertEquals(refLocal, c.getCellFormula()); - + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); s = wb.getSheetAt(0); assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula()); - + wb = openSample("46670_http.xls"); s = wb.getSheetAt(0); c = s.getRow(0).getCell(0); c.setCellFormula(refHttp); assertEquals(refHttp, c.getCellFormula()); - + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); s = wb.getSheetAt(0); assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula()); @@ -2748,11 +2752,11 @@ public final class TestBugs extends BaseTestBugzillaIssues { Workbook wb = openSample("57456.xls"); wb.close(); } - + @Test public void test57163() throws IOException { Workbook wb = openSample("57163.xls"); - + while (wb.getNumberOfSheets() > 1) { wb.removeSheetAt(1); } -- 2.39.5