Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=1872397&r1=1872396&r2=1872397&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Mon Jan 6 21:48:00 2020 @@ -18,31 +18,33 @@ package org.apache.poi.hssf.usermodel; import static org.apache.poi.POITestCase.assertContains; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; +import static org.apache.poi.hssf.HSSFTestDataSamples.writeOutAndReadBack; +import static org.junit.Assert.*; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.TimeZone; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import javax.imageio.ImageIO; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hpsf.PropertySet; @@ -87,9 +89,13 @@ import org.apache.poi.ss.usermodel.Sheet import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.LocaleUtil; +import org.junit.AfterClass; import org.junit.Assume; +import org.junit.BeforeClass; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Testcases for bugs entered in bugzilla @@ -99,42 +105,57 @@ import org.junit.Test; * define the test in the base class {@link BaseTestBugzillaIssues}</b> */ public final class TestBugs extends BaseTestBugzillaIssues { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + public TestBugs() { super(HSSFITestDataProvider.instance); } - private static HSSFWorkbook openSample(String sampleFileName) { - return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName); + private static final Map<String,HSSFFormulaEvaluator> SIMPLE_REFS = new LinkedHashMap<>(); + + // References used for the simpleTest convenience method + @BeforeClass + public static void initSimpleRefs() { + String[] refs = { + "Calculations", + "/Documents and Settings/crawformk.EUU/Local Settings/Temporary Internet Files/OLK64/Daily Status Report Generation Files/DST - Daily Data Transfer Sheet - 2002.xls", + "Sheet1", + "/Documents and Settings/donnag/Local Settings/Temporary Internet Files/OLK7/0231324V1-1.xls", + "Sheet1", + "refs/airport.xls", + "Sheet1", + "9http://www.principlesofeconometrics.com/excel/airline.xls", + "Sheet1", + "C:Documents and Settings/Yegor/My Documents/csco.xls", + }; + + for (int i=0; i<refs.length; i+=2) { + HSSFWorkbook wb = new HSSFWorkbook(); + wb.createSheet(refs[i]); + SIMPLE_REFS.put(refs[i+1].replace("/", File.separator), wb.getCreationHelper().createFormulaEvaluator()); + } } - private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) { - return HSSFITestDataProvider.instance.writeOutAndReadBack(original); + @AfterClass + public static void cleanUpRefs() { + SIMPLE_REFS.clear(); } + /** * Test reading AND writing a complicated workbook * Test opening resulting sheet in excel */ @Test public void bug15228() throws Exception { - HSSFWorkbook wb = openSample("15228.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFRow r = s.createRow(0); - HSSFCell c = r.createCell(0); - c.setCellValue(10); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("15228.xls"); } @Test public void bug13796() throws Exception { - HSSFWorkbook wb = openSample("13796.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFRow r = s.createRow(0); - HSSFCell c = r.createCell(0); - c.setCellValue(10); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("13796.xls"); } /** @@ -142,20 +163,13 @@ public final class TestBugs extends Base **/ @Test public void bug14460() throws Exception { - HSSFWorkbook wb = openSample("14460.xls"); - wb.getSheetAt(0); - wb.close(); + simpleTest("14460.xls"); } @Test public void bug14330() throws Exception { - HSSFWorkbook wb1 = openSample("14330-1.xls"); - wb1.getSheetAt(0); - wb1.close(); - - HSSFWorkbook wb2 = openSample("14330-2.xls"); - wb2.getSheetAt(0); - wb2.close(); + simpleTest("14330-1.xls"); + simpleTest("14330-2.xls"); } private static void setCellText(HSSFCell cell, String text) { @@ -168,27 +182,28 @@ public final class TestBugs extends Base */ @Test public void bug15375() throws Exception { - HSSFWorkbook wb = openSample("15375.xls"); - HSSFSheet sheet = wb.getSheetAt(0); + try (HSSFWorkbook wb = openSampleWorkbook("15375.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); + + HSSFRow row = sheet.getRow(5); + HSSFCell cell = row.getCell(3); + if (cell == null) { + cell = row.createCell(3); + } + + // Write test + setCellText(cell, "a test"); + + // change existing numeric cell value - HSSFRow row = sheet.getRow(5); - HSSFCell cell = row.getCell(3); - if (cell == null) - cell = row.createCell(3); - - // Write test - setCellText(cell, "a test"); - - // change existing numeric cell value - - HSSFRow oRow = sheet.getRow(14); - HSSFCell oCell = oRow.getCell(4); - oCell.setCellValue(75); - oCell = oRow.getCell(5); - setCellText(oCell, "0.3"); + HSSFRow oRow = sheet.getRow(14); + HSSFCell oCell = oRow.getCell(4); + oCell.setCellValue(75); + oCell = oRow.getCell(5); + setCellText(oCell, "0.3"); - writeOutAndReadBack(wb).close(); - wb.close(); + writeOutAndReadBack(wb).close(); + } } /** @@ -205,35 +220,31 @@ public final class TestBugs extends Base */ @Test public void bug15556() throws Exception { - HSSFWorkbook wb = openSample("15556.xls"); - HSSFSheet sheet = wb.getSheetAt(0); - HSSFRow row = sheet.getRow(45); - assertNotNull("Read row fine!", row); - wb.close(); + simpleTest("15556.xls", wb -> assertNotNull(wb.getSheetAt(0).getRow(45))); } /** * Double byte strings */ @Test - public void bug22742() { - openSample("22742.xls"); + public void bug22742() throws IOException { + simpleTest("22742.xls"); } /** * Double byte strings */ @Test - public void bug12561_1() { - openSample("12561-1.xls"); + public void bug12561_1() throws IOException { + simpleTest("12561-1.xls"); } /** * Double byte strings */ @Test - public void bug12561_2() { - openSample("12561-2.xls"); + public void bug12561_2() throws IOException { + simpleTest("12561-2.xls"); } /** @@ -241,8 +252,8 @@ public final class TestBugs extends Base * File supplied by jubeson */ @Test - public void bug12843_1() { - openSample("12843-1.xls"); + public void bug12843_1() throws IOException { + simpleTest("12843-1.xls"); } /** @@ -250,45 +261,45 @@ public final class TestBugs extends Base * File supplied by Paul Chung */ @Test - public void bug12843_2() { - openSample("12843-2.xls"); + public void bug12843_2() throws IOException { + simpleTest("12843-2.xls"); } /** * Reference to Name */ @Test - public void bug13224() { - openSample("13224.xls"); + public void bug13224() throws IOException { + simpleTest("13224.xls"); } /** * Illegal argument exception - cannot store duplicate value in Map */ @Test - public void bug19599() { - openSample("19599-1.xls"); - openSample("19599-2.xls"); + public void bug19599() throws IOException { + simpleTest("19599-1.xls"); + simpleTest("19599-2.xls"); } @Test public void bug24215() throws Exception { - HSSFWorkbook wb = openSample("24215.xls"); - - for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { - HSSFSheet sheet = wb.getSheetAt(sheetIndex); - int rows = sheet.getLastRowNum(); - - for (int rowIndex = 0; rowIndex < rows; rowIndex++) { - HSSFRow row = sheet.getRow(rowIndex); - int cells = row.getLastCellNum(); + try (HSSFWorkbook wb = openSampleWorkbook("24215.xls")) { - for (int cellIndex = 0; cellIndex < cells; cellIndex++) { - row.getCell(cellIndex); + for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { + HSSFSheet sheet = wb.getSheetAt(sheetIndex); + int rows = sheet.getLastRowNum(); + + for (int rowIndex = 0; rowIndex < rows; rowIndex++) { + HSSFRow row = sheet.getRow(rowIndex); + int cells = row.getLastCellNum(); + + for (int cellIndex = 0; cellIndex < cells; cellIndex++) { + row.getCell(cellIndex); + } } } } - wb.close(); } /** @@ -299,16 +310,16 @@ public final class TestBugs extends Base @Test public void bugUnicodeStringFormulaRead() throws Exception { - HSSFWorkbook w = openSample("25695.xls"); + try (HSSFWorkbook w = openSampleWorkbook("25695.xls")) { - HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0); - HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1); - HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0); - HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1); - HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0); - HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1); - HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0); - HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1); + HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0); + HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1); + HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0); + HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1); + HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0); + HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1); + HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0); + HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1); /* // THAI code page @@ -325,34 +336,33 @@ public final class TestBugs extends Base System.out.println("d2="+unicodeString(d2)); */ - confirmSameCellText(a1, a2); - confirmSameCellText(b1, b2); - confirmSameCellText(c1, c2); - confirmSameCellText(d1, d2); - - HSSFWorkbook rw = writeOutAndReadBack(w); - w.close(); - - HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0); - HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1); - HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0); - HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1); - HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0); - HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1); - HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0); - HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1); - - confirmSameCellText(a1, ra1); - confirmSameCellText(b1, rb1); - confirmSameCellText(c1, rc1); - confirmSameCellText(d1, rd1); - - confirmSameCellText(a1, ra2); - confirmSameCellText(b1, rb2); - confirmSameCellText(c1, rc2); - confirmSameCellText(d1, rd2); - - rw.close(); + confirmSameCellText(a1, a2); + confirmSameCellText(b1, b2); + confirmSameCellText(c1, c2); + confirmSameCellText(d1, d2); + + try (HSSFWorkbook rw = writeOutAndReadBack(w)) { + + HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0); + HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1); + HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0); + HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1); + HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0); + HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1); + HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0); + HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1); + + confirmSameCellText(a1, ra1); + confirmSameCellText(b1, rb1); + confirmSameCellText(c1, rc1); + confirmSameCellText(d1, rd1); + + confirmSameCellText(a1, ra2); + confirmSameCellText(b1, rb2); + confirmSameCellText(c1, rc2); + confirmSameCellText(d1, rd2); + } + } } private static void confirmSameCellText(HSSFCell a, HSSFCell b) { @@ -363,16 +373,16 @@ public final class TestBugs extends Base * Error in opening wb */ @Test - public void bug32822() { - openSample("32822.xls"); + public void bug32822() throws IOException { + simpleTest("32822.xls"); } /** * fail to read wb with chart */ @Test - public void bug15573() { - openSample("15573.xls"); + public void bug15573() throws IOException { + simpleTest("15573.xls"); } /** @@ -380,98 +390,85 @@ public final class TestBugs extends Base */ @Test public void bug27852() throws Exception { - HSSFWorkbook wb = openSample("27852.xls"); + 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 (int i = 0; i < wb.getNumberOfNames(); i++) { + HSSFName name = wb.getNameAt(i); + name.getNameName(); + if (name.isFunctionName()) { + continue; + } + name.getRefersToFormula(); } - name.getRefersToFormula(); } - wb.close(); } @Test - public void bug33082() { - openSample("33082.xls"); + public void bug33082() throws IOException { + simpleTest("33082.xls"); } @Test - public void bug34775() { - try { - openSample("34775.xls"); - } catch (NullPointerException e) { - fail("identified bug 34775"); - } + public void bug34775() throws IOException { + // NullPointerException -> identified bug 34775 + simpleTest("34775.xls"); } /** * Error when reading then writing ArrayValues in NameRecord's */ @Test - public void bug37630() throws Exception { - HSSFWorkbook wb = openSample("37630.xls"); - writeOutAndReadBack(wb); - wb.close(); + public void bug37630() throws IOException { + simpleTest("37630.xls"); } /** * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet */ @Test - public void bug25183() throws Exception { - HSSFWorkbook wb = openSample("25183.xls"); - writeOutAndReadBack(wb); - wb.close(); + public void bug25183() throws IOException { + simpleTest("25183.xls"); } /** * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure */ @Test - public void bug26100() throws Exception { - HSSFWorkbook wb = openSample("26100.xls"); - writeOutAndReadBack(wb); - wb.close(); + public void bug26100() throws IOException { + simpleTest("26100.xls"); } /** * Bug 27933: Unable to use a template (xls) file containing a wmf graphic */ @Test - public void bug27933() throws Exception { - HSSFWorkbook wb = openSample("27933.xls"); - writeOutAndReadBack(wb); - wb.close(); + public void bug27933() throws IOException { + simpleTest("27933.xls"); } /** - * Bug 29206: NPE on HSSFSheet.getRow for blank rows + * Bug 29206: NPE on HSSFSheet.getRow for blank rows */ @Test public void bug29206() throws Exception { //the first check with blank workbook - HSSFWorkbook wb = openSample("Simple.xls"); - HSSFSheet sheet = wb.createSheet(); - for (int i = 1; i < 400; i++) { - HSSFRow row = sheet.getRow(i); - if (row != null) { - row.getCell(0); + try (HSSFWorkbook wb = openSampleWorkbook("Simple.xls")) { + HSSFSheet sheet = wb.createSheet(); + for (int i = 1; i < 400; i++) { + HSSFRow row = sheet.getRow(i); + if (row != null) { + row.getCell(0); + } } } - wb.close(); } /** * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic */ @Test - public void bug29675() throws Exception { - HSSFWorkbook wb = openSample("29675.xls"); - writeOutAndReadBack(wb); - wb.close(); + public void bug29675() throws IOException { + simpleTest("29675.xls"); } /** @@ -479,22 +476,22 @@ public final class TestBugs extends Base */ @Test public void bug29942() throws Exception { - HSSFWorkbook wb = openSample("29942.xls"); + try (HSSFWorkbook wb = openSampleWorkbook("29942.xls")) { - HSSFSheet sheet = wb.getSheetAt(0); - int count = 0; - for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { - HSSFRow row = sheet.getRow(i); - if (row != null) { - HSSFCell cell = row.getCell(0); - assertEquals(CellType.STRING, cell.getCellType()); - count++; + HSSFSheet sheet = wb.getSheetAt(0); + int count = 0; + for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { + HSSFRow row = sheet.getRow(i); + if (row != null) { + HSSFCell cell = row.getCell(0); + assertEquals(CellType.STRING, cell.getCellType()); + count++; + } } - } - assertEquals(85, count); //should read 85 rows + assertEquals(85, count); //should read 85 rows - writeOutAndReadBack(wb).close(); - wb.close(); + writeOutAndReadBack(wb).close(); + } } /** @@ -503,9 +500,7 @@ public final class TestBugs extends Base */ @Test public void bug29982() throws Exception { - HSSFWorkbook wb = openSample("29982.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("29982.xls"); } /** @@ -513,33 +508,33 @@ public final class TestBugs extends Base */ @Test public void bug30540() throws Exception { - HSSFWorkbook wb = openSample("30540.xls"); - - HSSFSheet s = wb.getSheetAt(0); - s.setRowBreak(1); - writeOutAndReadBack(wb).close(); + try (HSSFWorkbook wb = openSampleWorkbook("30540.xls")) { - wb.close(); + HSSFSheet s1 = wb.getSheetAt(0); + s1.setRowBreak(1); + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) { + HSSFSheet s2 = wb2.getSheetAt(0); + int[] act = s2.getRowBreaks(); + int[] exp = { 1 }; + assertArrayEquals(exp, act); + } + } } /** * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......? */ @Test - public void bug31749() throws Exception { - HSSFWorkbook wb = openSample("31749.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + public void bug31749() throws IOException { + simpleTest("31749.xls"); } /** * Bug 31979: {urgent help needed .....}poi library does not support form objects properly. */ @Test - public void bug31979() throws Exception { - HSSFWorkbook wb = openSample("31979.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + public void bug31979() throws IOException { + simpleTest("31979.xls"); } /** @@ -548,15 +543,11 @@ public final class TestBugs extends Base */ @Test public void bug35564() throws Exception { - HSSFWorkbook wb = openSample("35564.xls"); - - HSSFSheet sheet = wb.getSheetAt(0); - assertFalse(sheet.isGridsPrinted()); - assertFalse(sheet.getProtect()); - - writeOutAndReadBack(wb).close(); - - wb.close(); + simpleTest("35564.xls", wb -> { + HSSFSheet sheet = wb.getSheetAt(0); + assertFalse(sheet.isGridsPrinted()); + assertFalse(sheet.getProtect()); + }); } /** @@ -564,13 +555,7 @@ public final class TestBugs extends Base */ @Test public void bug35565() throws Exception { - HSSFWorkbook wb = openSample("35565.xls"); - - HSSFSheet sheet = wb.getSheetAt(0); - assertNotNull(sheet); - writeOutAndReadBack(wb).close(); - - wb.close(); + simpleTest("35565.xls", wb -> assertNotNull(wb.getSheetAt(0))); } /** @@ -578,9 +563,7 @@ public final class TestBugs extends Base */ @Test public void bug37376() throws Exception { - HSSFWorkbook wb = openSample("37376.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("37376.xls"); } /** @@ -588,20 +571,20 @@ public final class TestBugs extends Base */ @Test public void bug40285() throws Exception { - HSSFWorkbook wb = openSample("40285.xls"); + try (HSSFWorkbook wb = openSampleWorkbook("40285.xls")) { - HSSFSheet sheet = wb.getSheetAt(0); - int rownum = 0; - for (Iterator<Row> it = sheet.rowIterator(); it.hasNext(); rownum++) { - Row row = it.next(); - assertEquals(rownum, row.getRowNum()); - int cellNum = 0; - for (Iterator<Cell> it2 = row.cellIterator(); it2.hasNext(); cellNum++) { - Cell cell = it2.next(); - assertEquals(cellNum, cell.getColumnIndex()); + HSSFSheet sheet = wb.getSheetAt(0); + int rownum = 0; + for (Iterator<Row> it = sheet.rowIterator(); it.hasNext(); rownum++) { + Row row = it.next(); + assertEquals(rownum, row.getRowNum()); + int cellNum = 0; + for (Iterator<Cell> it2 = row.cellIterator(); it2.hasNext(); cellNum++) { + Cell cell = it2.next(); + assertEquals(cellNum, cell.getColumnIndex()); + } } } - wb.close(); } /** @@ -618,25 +601,23 @@ public final class TestBugs extends Base public void bug38266() throws Exception { String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"}; for (String file : files) { - HSSFWorkbook wb = openSample(file); - - HSSFSheet sheet = wb.getSheetAt(0); - int[] breaks = sheet.getRowBreaks(); - assertEquals(0, breaks.length); + try (HSSFWorkbook wb = openSampleWorkbook(file)) { - //add 3 row breaks - for (int j = 1; j <= 3; j++) { - sheet.setRowBreak(j * 20); + HSSFSheet sheet = wb.getSheetAt(0); + int[] breaks = sheet.getRowBreaks(); + assertEquals(0, breaks.length); + + //add 3 row breaks + for (int j = 1; j <= 3; j++) { + sheet.setRowBreak(j * 20); + } } - wb.close(); } } @Test public void bug40738() throws Exception { - HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("SimpleWithAutofilter.xls"); } /** @@ -644,10 +625,7 @@ public final class TestBugs extends Base */ @Test public void bug44200() throws Exception { - HSSFWorkbook wb = openSample("44200.xls"); - wb.cloneSheet(0); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("44200.xls", wb -> wb.cloneSheet(0)); } /** @@ -655,9 +633,7 @@ public final class TestBugs extends Base */ @Test public void bug44201() throws Exception { - HSSFWorkbook wb = openSample("44201.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("44201.xls"); } /** @@ -665,12 +641,8 @@ public final class TestBugs extends Base */ @Test public void bug37684() throws Exception { - HSSFWorkbook wb1 = openSample("37684-1.xls"); - writeOutAndReadBack(wb1).close(); - wb1.close(); - HSSFWorkbook wb2 = openSample("37684-2.xls"); - writeOutAndReadBack(wb2).close(); - wb2.close(); + simpleTest("37684-1.xls"); + simpleTest("37684-2.xls"); } /** @@ -678,9 +650,7 @@ public final class TestBugs extends Base */ @Test public void bug41139() throws Exception { - HSSFWorkbook wb = openSample("41139.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("41139.xls"); } /** @@ -689,12 +659,7 @@ public final class TestBugs extends Base */ @Test public void bug41546() throws Exception { - HSSFWorkbook wb1 = openSample("41546.xls"); - assertEquals(1, wb1.getNumberOfSheets()); - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - assertEquals(1, wb2.getNumberOfSheets()); - wb2.close(); - wb1.close(); + simpleTest("41546.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -703,9 +668,7 @@ public final class TestBugs extends Base */ @Test public void bug42564() throws Exception { - HSSFWorkbook wb = openSample("ex42564-21435.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("ex42564-21435.xls"); } /** @@ -715,9 +678,7 @@ public final class TestBugs extends Base */ @Test public void bug42564Alt() throws Exception { - HSSFWorkbook wb = openSample("ex42564-21503.xls"); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("ex42564-21503.xls"); } /** @@ -726,31 +687,30 @@ public final class TestBugs extends Base */ @Test public void bug42618() throws Exception { - HSSFWorkbook wb1 = openSample("SimpleWithChoose.xls"); - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - // Check we detect the string properly too - HSSFSheet s = wb2.getSheetAt(0); + try (HSSFWorkbook wb1 = openSampleWorkbook("SimpleWithChoose.xls"); + HSSFWorkbook wb2 = writeOutAndReadBack(wb1)){ + // Check we detect the string properly too + HSSFSheet s = wb2.getSheetAt(0); + + // Textual value + HSSFRow r1 = s.getRow(0); + HSSFCell c1 = r1.getCell(1); + assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString()); + + // Formula Value + HSSFRow r2 = s.getRow(1); + HSSFCell c2 = r2.getCell(1); + assertEquals(25, (int) c2.getNumericCellValue()); - // Textual value - HSSFRow r1 = s.getRow(0); - HSSFCell c1 = r1.getCell(1); - assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString()); - - // Formula Value - HSSFRow r2 = s.getRow(1); - HSSFCell c2 = r2.getCell(1); - assertEquals(25, (int) c2.getNumericCellValue()); - - try { - assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula()); - } catch (IllegalStateException e) { - if (e.getMessage().startsWith("Too few arguments") - && e.getMessage().indexOf("ConcatPtg") > 0) { - fail("identified bug 44306"); + try { + assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula()); + } catch (IllegalStateException e) { + if (e.getMessage().startsWith("Too few arguments") + && e.getMessage().indexOf("ConcatPtg") > 0) { + fail("identified bug 44306"); + } } } - wb2.close(); } /** @@ -758,11 +718,8 @@ public final class TestBugs extends Base */ @Test public void bug43251() throws Exception { - // Used to blow up with an IllegalArgumentException - // when creating a FileSharingRecord - HSSFWorkbook wb = openSample("43251.xls"); - assertEquals(1, wb.getNumberOfSheets()); - wb.close(); + // Used to blow up with an IllegalArgumentException when creating a FileSharingRecord + simpleTest("43251.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -771,11 +728,8 @@ public final class TestBugs extends Base */ @Test public void bug44471() throws Exception { - // Used to blow up with an ArrayIndexOutOfBounds - // when creating a StyleRecord - HSSFWorkbook wb = openSample("OddStyleRecord.xls"); - assertEquals(1, wb.getNumberOfSheets()); - wb.close(); + // Used to blow up with an ArrayIndexOutOfBounds when creating a StyleRecord + simpleTest("OddStyleRecord.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -784,19 +738,16 @@ public final class TestBugs extends Base */ @Test public void bug44536() throws Exception { - // Used to blow up with an IllegalArgumentException - // when creating a FileSharingRecord - HSSFWorkbook wb1 = openSample("ReadOnlyRecommended.xls"); + // Used to blow up with an IllegalArgumentException when creating a FileSharingRecord + simpleTest("ReadOnlyRecommended.xls", wb -> { + // Check read only advised + assertEquals(3, wb.getNumberOfSheets()); + assertTrue(wb.isWriteProtected()); + }); - // Check read only advised - assertEquals(3, wb1.getNumberOfSheets()); - assertTrue(wb1.isWriteProtected()); - wb1.close(); // But also check that another wb isn't - HSSFWorkbook wb2 = openSample("SimpleWithChoose.xls"); - assertFalse(wb2.isWriteProtected()); - wb2.close(); + simpleTest("SimpleWithChoose.xls", wb -> assertFalse(wb.isWriteProtected())); } /** @@ -805,13 +756,9 @@ public final class TestBugs extends Base */ @Test public void bug44593() throws Exception { - // Used to blow up with an IllegalArgumentException - // when creating a DVRecord - // Now won't, but no idea if this means we have - // rubbish in the DVRecord or not... - HSSFWorkbook wb = openSample("44593.xls"); - assertEquals(2, wb.getNumberOfSheets()); - wb.close(); + // Used to blow up with an IllegalArgumentException when creating a DVRecord + // Now won't, but no idea if this means we have rubbish in the DVRecord or not... + simpleTest("44593.xls", wb -> assertEquals(2, wb.getNumberOfSheets())); } /** @@ -821,9 +768,7 @@ public final class TestBugs extends Base @Test public void bug44643() throws Exception { // Used to blow up with an IllegalArgumentException - HSSFWorkbook wb = openSample("44643.xls"); - assertEquals(1, wb.getNumberOfSheets()); - wb.close(); + simpleTest("44643.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -832,23 +777,23 @@ public final class TestBugs extends Base */ @Test public void bug44693() throws Exception { - HSSFWorkbook wb = openSample("44693.xls"); - HSSFSheet s = wb.getSheetAt(0); + try (HSSFWorkbook wb = openSampleWorkbook("44693.xls")) { + HSSFSheet s = wb.getSheetAt(0); - // Rows are 1 to 713 - assertEquals(0, s.getFirstRowNum()); - assertEquals(712, s.getLastRowNum()); - assertEquals(713, s.getPhysicalNumberOfRows()); - - // Now check the iterator - int rowsSeen = 0; - for (Iterator<Row> i = s.rowIterator(); i.hasNext(); ) { - Row r = i.next(); - assertNotNull(r); - rowsSeen++; + // Rows are 1 to 713 + assertEquals(0, s.getFirstRowNum()); + assertEquals(712, s.getLastRowNum()); + assertEquals(713, s.getPhysicalNumberOfRows()); + + // Now check the iterator + int rowsSeen = 0; + for (Iterator<Row> i = s.rowIterator(); i.hasNext(); ) { + Row r = i.next(); + assertNotNull(r); + rowsSeen++; + } + assertEquals(713, rowsSeen); } - assertEquals(713, rowsSeen); - wb.close(); } /** @@ -856,11 +801,7 @@ public final class TestBugs extends Base */ @Test public void bug28774() throws Exception { - HSSFWorkbook wb = openSample("28774.xls"); - assertTrue("no errors reading sample xls", true); - writeOutAndReadBack(wb).close(); - assertTrue("no errors writing sample xls", true); - wb.close(); + simpleTest("28774.xls"); } /** @@ -869,11 +810,7 @@ public final class TestBugs extends Base */ @Test public void bug44891() throws Exception { - HSSFWorkbook wb = openSample("44891.xls"); - assertTrue("no errors reading sample xls", true); - writeOutAndReadBack(wb).close(); - assertTrue("no errors writing sample xls", true); - wb.close(); + simpleTest("44891.xls"); } /** @@ -883,29 +820,17 @@ public final class TestBugs extends Base */ @Test public void bug44235() throws Exception { - HSSFWorkbook wb = openSample("44235.xls"); - assertTrue("no errors reading sample xls", true); - writeOutAndReadBack(wb).close(); - assertTrue("no errors writing sample xls", true); - wb.close(); + simpleTest("44235.xls"); } @Test public void bug36947() throws Exception { - HSSFWorkbook wb = openSample("36947.xls"); - assertTrue("no errors reading sample xls", true); - writeOutAndReadBack(wb).close(); - assertTrue("no errors writing sample xls", true); - wb.close(); + simpleTest("36947.xls"); } @Test public void bug39634() throws Exception { - HSSFWorkbook wb = openSample("39634.xls"); - assertTrue("no errors reading sample xls", true); - writeOutAndReadBack(wb).close(); - assertTrue("no errors writing sample xls", true); - wb.close(); + simpleTest("39634.xls"); } /** @@ -914,34 +839,31 @@ public final class TestBugs extends Base */ @Test(expected = IllegalArgumentException.class) public void bug44840() throws Exception { - HSSFWorkbook wb = openSample("WithCheckBoxes.xls"); + try (HSSFWorkbook wb = openSampleWorkbook("WithCheckBoxes.xls")) { - // Take a look at the embedded objects - List<HSSFObjectData> objects = wb.getAllEmbeddedObjects(); - assertEquals(1, objects.size()); - - HSSFObjectData obj = objects.get(0); - assertNotNull(obj); - - // Peek inside the underlying record - EmbeddedObjectRefSubRecord rec = obj.findObjectRecord(); - assertNotNull(rec); - -// assertEquals(32, rec.field_1_stream_id_offset); - assertEquals(0, rec.getStreamId().intValue()); // WRONG! - assertEquals("Forms.CheckBox.1", rec.getOLEClassName()); - assertEquals(12, rec.getObjectData().length); - - // Doesn't have a directory - assertFalse(obj.hasDirectoryEntry()); - assertNotNull(obj.getObjectData()); - assertEquals(12, obj.getObjectData().length); - assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName()); + // Take a look at the embedded objects + List<HSSFObjectData> objects = wb.getAllEmbeddedObjects(); + assertEquals(1, objects.size()); + + HSSFObjectData obj = objects.get(0); + assertNotNull(obj); + + // Peek inside the underlying record + EmbeddedObjectRefSubRecord rec = obj.findObjectRecord(); + assertNotNull(rec); + + // assertEquals(32, rec.field_1_stream_id_offset); + assertEquals(0, rec.getStreamId().intValue()); // WRONG! + assertEquals("Forms.CheckBox.1", rec.getOLEClassName()); + assertEquals(12, rec.getObjectData().length); + + // Doesn't have a directory + assertFalse(obj.hasDirectoryEntry()); + assertNotNull(obj.getObjectData()); + assertEquals(12, obj.getObjectData().length); + assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName()); - try { obj.getDirectory(); - } finally { - wb.close(); } } @@ -952,33 +874,33 @@ public final class TestBugs extends Base */ @Test public void bug30978() throws Exception { - HSSFWorkbook wb1 = openSample("30978-alt.xls"); - assertEquals(1, wb1.getNumberOfNames()); - assertEquals(3, wb1.getNumberOfSheets()); - - // Check all names fit within range, and use - // DeletedArea3DPtg - InternalWorkbook w = wb1.getWorkbook(); - assertNames(wb1, w); - - // Delete the 2nd sheet - wb1.removeSheetAt(1); - - // Re-check - assertEquals(1, wb1.getNumberOfNames()); - assertEquals(2, wb1.getNumberOfSheets()); - assertNames(wb1, w); + try (HSSFWorkbook wb1 = openSampleWorkbook("30978-alt.xls")) { + assertEquals(1, wb1.getNumberOfNames()); + assertEquals(3, wb1.getNumberOfSheets()); + + // Check all names fit within range, and use + // DeletedArea3DPtg + InternalWorkbook w = wb1.getWorkbook(); + assertNames(wb1, w); + + // Delete the 2nd sheet + wb1.removeSheetAt(1); + + // Re-check + assertEquals(1, wb1.getNumberOfNames()); + assertEquals(2, wb1.getNumberOfSheets()); + assertNames(wb1, w); + + // Save and re-load + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + w = wb2.getWorkbook(); - // Save and re-load - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - w = wb2.getWorkbook(); + assertEquals(1, wb2.getNumberOfNames()); + assertEquals(2, wb2.getNumberOfSheets()); - assertEquals(1, wb2.getNumberOfNames()); - assertEquals(2, wb2.getNumberOfSheets()); - - assertNames(wb2, w); - wb2.close(); + assertNames(wb2, w); + } + } } private void assertNames(HSSFWorkbook wb1, InternalWorkbook w) { @@ -997,85 +919,55 @@ public final class TestBugs extends Base */ @Test public void bug45338() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - assertEquals(4, wb.getNumberOfFontsAsInt()); - - HSSFSheet s = wb.createSheet(); - s.createRow(0); - s.createRow(1); - s.getRow(0).createCell(0); - s.getRow(1).createCell(0); - - assertEquals(4, wb.getNumberOfFontsAsInt()); - - HSSFFont f1 = wb.getFontAt(0); - assertFalse(f1.getBold()); - - // Check that asking for the same font - // multiple times gives you the same thing. - // Otherwise, our tests wouldn't work! - assertSame(wb.getFontAt(0), wb.getFontAt(0)); - assertEquals( - wb.getFontAt(0), - wb.getFontAt(0) - ); - assertEquals( - wb.getFontAt(2), - wb.getFontAt(2) - ); - assertNotSame(wb.getFontAt(0), wb.getFontAt(2)); - - // Look for a new font we have - // yet to add - assertNull( - wb.findFont( - false, (short) 123, (short) 22, - "Thingy", false, true, (short) 2, (byte) 2 - ) - ); - - HSSFFont nf = wb.createFont(); - assertEquals(5, wb.getNumberOfFontsAsInt()); - - assertEquals(5, nf.getIndex()); - assertEquals(nf, wb.getFontAt(5)); - - nf.setBold(false); - nf.setColor((short) 123); - nf.setFontHeight((short) 22); - nf.setFontName("Thingy"); - nf.setItalic(false); - nf.setStrikeout(true); - nf.setTypeOffset((short) 2); - nf.setUnderline((byte) 2); - - assertEquals(5, wb.getNumberOfFontsAsInt()); - assertEquals(nf, wb.getFontAt(5)); - - // Find it now - assertNotNull( - wb.findFont( - false, (short) 123, (short) 22, - "Thingy", false, true, (short) 2, (byte) 2 - ) - ); - HSSFFont font = wb.findFont( - false, (short) 123, (short) 22, - "Thingy", false, true, (short) 2, (byte) 2 - ); - assertNotNull(font); - assertEquals( - 5, - font.getIndex() - ); - assertEquals(nf, - wb.findFont( - false, (short) 123, (short) 22, - "Thingy", false, true, (short) 2, (byte) 2 - ) - ); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + assertEquals(4, wb.getNumberOfFontsAsInt()); - wb.close(); + HSSFSheet s = wb.createSheet(); + s.createRow(0); + s.createRow(1); + s.getRow(0).createCell(0); + s.getRow(1).createCell(0); + + assertEquals(4, wb.getNumberOfFontsAsInt()); + + HSSFFont f1 = wb.getFontAt(0); + assertFalse(f1.getBold()); + + // Check that asking for the same font multiple times gives you the same thing. + // Otherwise, our tests wouldn't work! + assertSame(wb.getFontAt(0), wb.getFontAt(0)); + assertEquals(wb.getFontAt(0), wb.getFontAt(0)); + assertEquals(wb.getFontAt(2), wb.getFontAt(2)); + assertNotSame(wb.getFontAt(0), wb.getFontAt(2)); + + // Look for a new font we have yet to add + assertNull(wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2)); + + HSSFFont nf = wb.createFont(); + assertEquals(5, wb.getNumberOfFontsAsInt()); + + assertEquals(5, nf.getIndex()); + assertEquals(nf, wb.getFontAt(5)); + + nf.setBold(false); + nf.setColor((short) 123); + nf.setFontHeight((short) 22); + nf.setFontName("Thingy"); + nf.setItalic(false); + nf.setStrikeout(true); + nf.setTypeOffset((short) 2); + nf.setUnderline((byte) 2); + + assertEquals(5, wb.getNumberOfFontsAsInt()); + assertEquals(nf, wb.getFontAt(5)); + + // Find it now + assertNotNull(wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2)); + HSSFFont font = wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2); + assertNotNull(font); + assertEquals(5, font.getIndex()); + assertEquals(nf, wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2)); + } } /** @@ -1084,87 +976,87 @@ public final class TestBugs extends Base */ @Test public void bugZipCodeFormulas() throws Exception { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet s = wb1.createSheet(); - s.createRow(0); - HSSFCell c1 = s.getRow(0).createCell(0); - HSSFCell c2 = s.getRow(0).createCell(1); - HSSFCell c3 = s.getRow(0).createCell(2); - - // As number and string - c1.setCellFormula("70164"); - c2.setCellFormula("\"70164\""); - c3.setCellFormula("\"90210\""); - - // Check the formulas - assertEquals("70164", c1.getCellFormula()); - assertEquals("\"70164\"", c2.getCellFormula()); - - // And check the values - blank - confirmCachedValue(0.0, c1); - confirmCachedValue(0.0, c2); - confirmCachedValue(0.0, c3); - - // Try changing the cached value on one of the string - // formula cells, so we can see it updates properly - c3.setCellValue(new HSSFRichTextString("test")); - confirmCachedValue("test", c3); - try { - c3.getNumericCellValue(); - fail("exception should have been thrown"); - } catch (IllegalStateException e) { - assertEquals("Cannot get a NUMERIC value from a STRING formula cell", e.getMessage()); - } - - - // Now evaluate, they should all be changed - HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1); - eval.evaluateFormulaCell(c1); - eval.evaluateFormulaCell(c2); - eval.evaluateFormulaCell(c3); - - // Check that the cells now contain - // the correct values - confirmCachedValue(70164.0, c1); - confirmCachedValue("70164", c2); - confirmCachedValue("90210", c3); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet s = wb1.createSheet(); + s.createRow(0); + HSSFCell c1 = s.getRow(0).createCell(0); + HSSFCell c2 = s.getRow(0).createCell(1); + HSSFCell c3 = s.getRow(0).createCell(2); + + // As number and string + c1.setCellFormula("70164"); + c2.setCellFormula("\"70164\""); + c3.setCellFormula("\"90210\""); + + // Check the formulas + assertEquals("70164", c1.getCellFormula()); + assertEquals("\"70164\"", c2.getCellFormula()); + + // And check the values - blank + confirmCachedValue(0.0, c1); + confirmCachedValue(0.0, c2); + confirmCachedValue(0.0, c3); + + // Try changing the cached value on one of the string + // formula cells, so we can see it updates properly + c3.setCellValue(new HSSFRichTextString("test")); + confirmCachedValue("test", c3); + try { + c3.getNumericCellValue(); + fail("exception should have been thrown"); + } catch (IllegalStateException e) { + assertEquals("Cannot get a NUMERIC value from a STRING formula cell", e.getMessage()); + } - // Write and read - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - HSSFSheet ns = wb2.getSheetAt(0); - HSSFCell nc1 = ns.getRow(0).getCell(0); - HSSFCell nc2 = ns.getRow(0).getCell(1); - HSSFCell nc3 = ns.getRow(0).getCell(2); - - // Re-check - confirmCachedValue(70164.0, nc1); - confirmCachedValue("70164", nc2); - confirmCachedValue("90210", nc3); - - int i = 0; - for (Iterator<CellValueRecordInterface> it = ns.getSheet().getCellValueIterator(); it.hasNext(); i++) { - CellValueRecordInterface cvr = it.next(); - if (cvr instanceof FormulaRecordAggregate) { - FormulaRecordAggregate fr = (FormulaRecordAggregate) cvr; - - if (i == 0) { - assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001); - assertNull(fr.getStringRecord()); - } else if (i == 1) { - assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001); - assertNotNull(fr.getStringRecord()); - assertEquals("70164", fr.getStringRecord().getString()); - } else { - assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001); - assertNotNull(fr.getStringRecord()); - assertEquals("90210", fr.getStringRecord().getString()); + // Now evaluate, they should all be changed + HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1); + eval.evaluateFormulaCell(c1); + eval.evaluateFormulaCell(c2); + eval.evaluateFormulaCell(c3); + + // Check that the cells now contain + // the correct values + confirmCachedValue(70164.0, c1); + confirmCachedValue("70164", c2); + confirmCachedValue("90210", c3); + + + // Write and read + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + HSSFSheet ns = wb2.getSheetAt(0); + HSSFCell nc1 = ns.getRow(0).getCell(0); + HSSFCell nc2 = ns.getRow(0).getCell(1); + HSSFCell nc3 = ns.getRow(0).getCell(2); + + // Re-check + confirmCachedValue(70164.0, nc1); + confirmCachedValue("70164", nc2); + confirmCachedValue("90210", nc3); + + int i = 0; + for (Iterator<CellValueRecordInterface> it = ns.getSheet().getCellValueIterator(); it.hasNext(); i++) { + CellValueRecordInterface cvr = it.next(); + if (cvr instanceof FormulaRecordAggregate) { + FormulaRecordAggregate fr = (FormulaRecordAggregate) cvr; + + if (i == 0) { + assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001); + assertNull(fr.getStringRecord()); + } else if (i == 1) { + assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001); + assertNotNull(fr.getStringRecord()); + assertEquals("70164", fr.getStringRecord().getString()); + } else { + assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001); + assertNotNull(fr.getStringRecord()); + assertEquals("90210", fr.getStringRecord().getString()); + } + } } + assertEquals(3, i); } } - assertEquals(3, i); - wb2.close(); } private static void confirmCachedValue(double expectedValue, HSSFCell cell) { @@ -1193,32 +1085,32 @@ public final class TestBugs extends Base @Ignore("For now, blows up with an exception from ExtPtg") @Test public void test43623() throws Exception { - HSSFWorkbook wb1 = openSample("43623.xls"); - assertEquals(1, wb1.getNumberOfSheets()); - - HSSFSheet s1 = wb1.getSheetAt(0); - - HSSFCell c1 = s1.getRow(0).getCell(2); - HSSFCell c2 = s1.getRow(1).getCell(2); - HSSFCell c3 = s1.getRow(2).getCell(2); + try (HSSFWorkbook wb1 = openSampleWorkbook("43623.xls")) { + assertEquals(1, wb1.getNumberOfSheets()); - // These formula contents are a guess... - assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula()); - assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula()); - assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula()); + HSSFSheet s1 = wb1.getSheetAt(0); - // Save and re-open, ensure it still works - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - HSSFSheet ns1 = wb2.getSheetAt(0); - HSSFCell nc1 = ns1.getRow(0).getCell(2); - HSSFCell nc2 = ns1.getRow(1).getCell(2); - HSSFCell nc3 = ns1.getRow(2).getCell(2); - - assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula()); - assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula()); - assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula()); - wb2.close(); + HSSFCell c1 = s1.getRow(0).getCell(2); + HSSFCell c2 = s1.getRow(1).getCell(2); + HSSFCell c3 = s1.getRow(2).getCell(2); + + // These formula contents are a guess... + assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula()); + assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula()); + assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula()); + + // Save and re-open, ensure it still works + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + HSSFSheet ns1 = wb2.getSheetAt(0); + HSSFCell nc1 = ns1.getRow(0).getCell(2); + HSSFCell nc2 = ns1.getRow(1).getCell(2); + HSSFCell nc3 = ns1.getRow(2).getCell(2); + + assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula()); + assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula()); + assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula()); + } + } } /** @@ -1227,50 +1119,50 @@ public final class TestBugs extends Base */ @Test public void bug30635() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); - // No rows, first/last return -1 - assertEquals(-1, s.getFirstRowNum()); - assertEquals(-1, s.getLastRowNum()); - assertEquals(0, s.getPhysicalNumberOfRows()); - - // One row, most things are 0, physical is 1 - s.createRow(0); - assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); - assertEquals(1, s.getPhysicalNumberOfRows()); - - // And another, things change - s.createRow(4); - assertEquals(0, s.getFirstRowNum()); - assertEquals(4, s.getLastRowNum()); - assertEquals(2, s.getPhysicalNumberOfRows()); - - - // Now start on cells - HSSFRow r = s.getRow(0); - assertEquals(-1, r.getFirstCellNum()); - assertEquals(-1, r.getLastCellNum()); - assertEquals(0, r.getPhysicalNumberOfCells()); - - // Add a cell, things move off -1 - r.createCell(0); - assertEquals(0, r.getFirstCellNum()); - assertEquals(1, r.getLastCellNum()); // last cell # + 1 - assertEquals(1, r.getPhysicalNumberOfCells()); - - r.createCell(1); - assertEquals(0, r.getFirstCellNum()); - assertEquals(2, r.getLastCellNum()); // last cell # + 1 - assertEquals(2, r.getPhysicalNumberOfCells()); - - r.createCell(4); - assertEquals(0, r.getFirstCellNum()); - assertEquals(5, r.getLastCellNum()); // last cell # + 1 - assertEquals(3, r.getPhysicalNumberOfCells()); + // No rows, first/last return -1 + assertEquals(-1, s.getFirstRowNum()); + assertEquals(-1, s.getLastRowNum()); + assertEquals(0, s.getPhysicalNumberOfRows()); + + // One row, most things are 0, physical is 1 + s.createRow(0); + assertEquals(0, s.getFirstRowNum()); + assertEquals(0, s.getLastRowNum()); + assertEquals(1, s.getPhysicalNumberOfRows()); + + // And another, things change + s.createRow(4); + assertEquals(0, s.getFirstRowNum()); + assertEquals(4, s.getLastRowNum()); + assertEquals(2, s.getPhysicalNumberOfRows()); + + + // Now start on cells + HSSFRow r = s.getRow(0); + assertEquals(-1, r.getFirstCellNum()); + assertEquals(-1, r.getLastCellNum()); + assertEquals(0, r.getPhysicalNumberOfCells()); + + // Add a cell, things move off -1 + r.createCell(0); + assertEquals(0, r.getFirstCellNum()); + assertEquals(1, r.getLastCellNum()); // last cell # + 1 + assertEquals(1, r.getPhysicalNumberOfCells()); + + r.createCell(1); + assertEquals(0, r.getFirstCellNum()); + assertEquals(2, r.getLastCellNum()); // last cell # + 1 + assertEquals(2, r.getPhysicalNumberOfCells()); + + r.createCell(4); + assertEquals(0, r.getFirstCellNum()); + assertEquals(5, r.getLastCellNum()); // last cell # + 1 + assertEquals(3, r.getPhysicalNumberOfCells()); - wb.close(); + } } /** @@ -1278,32 +1170,32 @@ public final class TestBugs extends Base */ @Test public void bug44958() throws Exception { - HSSFWorkbook wb = openSample("44958.xls"); - HSSFSheet s; - HSSFRow r; - HSSFCell c; - - // Check the contents of the formulas - - // E4 to G9 of sheet 4 make up the table - s = wb.getSheet("OneVariable Table Completed"); - r = s.getRow(3); - c = r.getCell(4); - assertEquals(CellType.FORMULA, c.getCellType()); - - // TODO - check the formula once tables and - // arrays are properly supported - - - // E4 to H9 of sheet 5 make up the table - s = wb.getSheet("TwoVariable Table Example"); - r = s.getRow(3); - c = r.getCell(4); - assertEquals(CellType.FORMULA, c.getCellType()); - - // TODO - check the formula once tables and - // arrays are properly supported - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("44958.xls")) { + HSSFSheet s; + HSSFRow r; + HSSFCell c; + + // Check the contents of the formulas + + // E4 to G9 of sheet 4 make up the table + s = wb.getSheet("OneVariable Table Completed"); + r = s.getRow(3); + c = r.getCell(4); + assertEquals(CellType.FORMULA, c.getCellType()); + + // TODO - check the formula once tables and + // arrays are properly supported + + + // E4 to H9 of sheet 5 make up the table + s = wb.getSheet("TwoVariable Table Example"); + r = s.getRow(3); + c = r.getCell(4); + assertEquals(CellType.FORMULA, c.getCellType()); + + // TODO - check the formula once tables and + // arrays are properly supported + } } /** @@ -1311,10 +1203,13 @@ public final class TestBugs extends Base */ @Test public void bug45322() throws Exception { - HSSFWorkbook wb = openSample("44958.xls"); - HSSFSheet sh = wb.getSheetAt(0); - for (short i = 0; i < 30; i++) sh.autoSizeColumn(i); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("44958.xls")) { + HSSFSheet sh = wb.getSheetAt(0); + for (short i = 0; i < 30; i++) { + sh.autoSizeColumn(i); + assertNotEquals(0, sh.getColumnWidth(i)); + } + } } /** @@ -1323,21 +1218,21 @@ public final class TestBugs extends Base */ @Test public void bug45414() throws Exception { - HSSFWorkbook wb1 = openSample("WithThreeCharts.xls"); - wb1.getSheetAt(0).setForceFormulaRecalculation(true); - wb1.getSheetAt(1).setForceFormulaRecalculation(false); - wb1.getSheetAt(2).setForceFormulaRecalculation(true); - - // Write out and back in again - // This used to break - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - - // Check now set as it should be - assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation()); - assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation()); - assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation()); - wb2.close(); + try (HSSFWorkbook wb1 = openSampleWorkbook("WithThreeCharts.xls")) { + wb1.getSheetAt(0).setForceFormulaRecalculation(true); + wb1.getSheetAt(1).setForceFormulaRecalculation(false); + wb1.getSheetAt(2).setForceFormulaRecalculation(true); + + // Write out and back in again + // This used to break + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + + // Check now set as it should be + assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation()); + assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation()); + assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation()); + } + } } /** @@ -1345,29 +1240,28 @@ public final class TestBugs extends Base */ @Test public void bug45761() throws Exception { - HSSFWorkbook wb1 = openSample("45761.xls"); - assertEquals(3, wb1.getNumberOfSheets()); - - assertFalse(wb1.isSheetHidden(0)); - assertFalse(wb1.isSheetVeryHidden(0)); - assertTrue(wb1.isSheetHidden(1)); - assertFalse(wb1.isSheetVeryHidden(1)); - assertFalse(wb1.isSheetHidden(2)); - assertTrue(wb1.isSheetVeryHidden(2)); + try (HSSFWorkbook wb1 = openSampleWorkbook("45761.xls")) { + assertEquals(3, wb1.getNumberOfSheets()); - // Change sheet 0 to be very hidden, and re-load - wb1.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN); - - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - - assertFalse(wb2.isSheetHidden(0)); - assertTrue(wb2.isSheetVeryHidden(0)); - assertTrue(wb2.isSheetHidden(1)); - assertFalse(wb2.isSheetVeryHidden(1)); - assertFalse(wb2.isSheetHidden(2)); - assertTrue(wb2.isSheetVeryHidden(2)); - wb2.close(); + assertFalse(wb1.isSheetHidden(0)); + assertFalse(wb1.isSheetVeryHidden(0)); + assertTrue(wb1.isSheetHidden(1)); + assertFalse(wb1.isSheetVeryHidden(1)); + assertFalse(wb1.isSheetHidden(2)); + assertTrue(wb1.isSheetVeryHidden(2)); + + // Change sheet 0 to be very hidden, and re-load + wb1.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN); + + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + assertFalse(wb2.isSheetHidden(0)); + assertTrue(wb2.isSheetVeryHidden(0)); + assertTrue(wb2.isSheetHidden(1)); + assertFalse(wb2.isSheetVeryHidden(1)); + assertFalse(wb2.isSheetHidden(2)); + assertTrue(wb2.isSheetVeryHidden(2)); + } + } } /** @@ -1377,49 +1271,49 @@ public final class TestBugs extends Base */ @Test public void bug45777() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet s = wb.createSheet(); - char[] cc248 = new char[248]; - Arrays.fill(cc248, 'x'); - String s248 = new String(cc248); - - String s249 = s248 + "1"; - String s250 = s248 + "12"; - String s251 = s248 + "123"; - assertEquals(248, s248.length()); - assertEquals(249, s249.length()); - assertEquals(250, s250.length()); - assertEquals(251, s251.length()); - - - // Try on headers - s.getHeader().setCenter(s248); - assertEquals(254, s.getHeader().getRawText().length()); - writeOutAndReadBack(wb).close(); - - s.getHeader().setCenter(s251); - assertEquals(257, s.getHeader().getRawText().length()); - writeOutAndReadBack(wb).close(); - - // header can be more than 256 bytes - s.getHeader().setCenter(s250); // 256 bytes required - s.getHeader().setCenter(s251); // 257 bytes required - - // Now try on footers - s.getFooter().setCenter(s248); - assertEquals(254, s.getFooter().getRawText().length()); - writeOutAndReadBack(wb).close(); - - s.getFooter().setCenter(s251); - assertEquals(257, s.getFooter().getRawText().length()); - writeOutAndReadBack(wb).close(); - - // footer can be more than 256 bytes - s.getFooter().setCenter(s250); // 256 bytes required - s.getFooter().setCenter(s251); // 257 bytes required + char[] cc248 = new char[248]; + Arrays.fill(cc248, 'x'); + String s248 = new String(cc248); + + String s249 = s248 + "1"; + String s250 = s248 + "12"; + String s251 = s248 + "123"; + assertEquals(248, s248.length()); + assertEquals(249, s249.length()); + assertEquals(250, s250.length()); + assertEquals(251, s251.length()); + + + // Try on headers + s.getHeader().setCenter(s248); + assertEquals(254, s.getHeader().getRawText().length()); + writeOutAndReadBack(wb).close(); + + s.getHeader().setCenter(s251); + assertEquals(257, s.getHeader().getRawText().length()); + writeOutAndReadBack(wb).close(); + + // header can be more than 256 bytes + s.getHeader().setCenter(s250); // 256 bytes required + s.getHeader().setCenter(s251); // 257 bytes required + + // Now try on footers + s.getFooter().setCenter(s248); + assertEquals(254, s.getFooter().getRawText().length()); + writeOutAndReadBack(wb).close(); + + s.getFooter().setCenter(s251); + assertEquals(257, s.getFooter().getRawText().length()); + writeOutAndReadBack(wb).close(); + + // footer can be more than 256 bytes + s.getFooter().setCenter(s250); // 256 bytes required + s.getFooter().setCenter(s251); // 257 bytes required - wb.close(); + } } /** @@ -1427,11 +1321,7 @@ public final class TestBugs extends Base */ @Test public void bug45784() throws Exception { - // This used to break - HSSFWorkbook wb = openSample("45784.xls"); - assertEquals(1, wb.getNumberOfSheets()); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("45784.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -1439,37 +1329,37 @@ public final class TestBugs extends Base */ @Test public void bug45492() throws Exception { - HSSFWorkbook wb = openSample("45492.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFRow r = s.getRow(0); - HSSFPalette p = wb.getCustomPalette(); - - HSSFCell auto = r.getCell(0); - HSSFCell grey = r.getCell(1); - HSSFCell red = r.getCell(2); - HSSFCell blue = r.getCell(3); - HSSFCell green = r.getCell(4); - - assertEquals(64, auto.getCellStyle().getFillForegroundColor()); - assertEquals(64, auto.getCellStyle().getFillBackgroundColor()); - assertEquals("0:0:0", p.getColor(64).getHexString()); - - assertEquals(22, grey.getCellStyle().getFillForegroundColor()); - assertEquals(64, grey.getCellStyle().getFillBackgroundColor()); - assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString()); - - assertEquals(10, red.getCellStyle().getFillForegroundColor()); - assertEquals(64, red.getCellStyle().getFillBackgroundColor()); - assertEquals("FFFF:0:0", p.getColor(10).getHexString()); - - assertEquals(12, blue.getCellStyle().getFillForegroundColor()); - assertEquals(64, blue.getCellStyle().getFillBackgroundColor()); - assertEquals("0:0:FFFF", p.getColor(12).getHexString()); - - assertEquals(11, green.getCellStyle().getFillForegroundColor()); - assertEquals(64, green.getCellStyle().getFillBackgroundColor()); - assertEquals("0:FFFF:0", p.getColor(11).getHexString()); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("45492.xls")) { + HSSFSheet s = wb.getSheetAt(0); + HSSFRow r = s.getRow(0); + HSSFPalette p = wb.getCustomPalette(); + + HSSFCell auto = r.getCell(0); + HSSFCell grey = r.getCell(1); + HSSFCell red = r.getCell(2); + HSSFCell blue = r.getCell(3); + HSSFCell green = r.getCell(4); + + assertEquals(64, auto.getCellStyle().getFillForegroundColor()); + assertEquals(64, auto.getCellStyle().getFillBackgroundColor()); + assertEquals("0:0:0", p.getColor(64).getHexString()); + + assertEquals(22, grey.getCellStyle().getFillForegroundColor()); + assertEquals(64, grey.getCellStyle().getFillBackgroundColor()); + assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString()); + + assertEquals(10, red.getCellStyle().getFillForegroundColor()); + assertEquals(64, red.getCellStyle().getFillBackgroundColor()); + assertEquals("FFFF:0:0", p.getColor(10).getHexString()); + + assertEquals(12, blue.getCellStyle().getFillForegroundColor()); + assertEquals(64, blue.getCellStyle().getFillBackgroundColor()); + assertEquals("0:0:FFFF", p.getColor(12).getHexString()); + + assertEquals(11, green.getCellStyle().getFillForegroundColor()); + assertEquals(64, green.getCellStyle().getFillBackgroundColor()); + assertEquals("0:FFFF:0", p.getColor(11).getHexString()); + } } /** @@ -1477,11 +1367,7 @@ public final class TestBugs extends Base */ @Test public void bug46137() throws Exception { - // This used to break - HSSFWorkbook wb = openSample("46137.xls"); - assertEquals(7, wb.getNumberOfSheets()); - writeOutAndReadBack(wb).close(); - wb.close(); + simpleTest("46137.xls", wb -> assertEquals(7, wb.getNumberOfSheets())); } /** @@ -1490,9 +1376,7 @@ public final class TestBugs extends Base */ @Test public void bug45290() throws Exception { - HSSFWorkbook wb = openSample("45290.xls"); - assertEquals(1, wb.getNumberOfSheets()); - wb.close(); + simpleTest("45290.xls", wb -> assertEquals(1, wb.getNumberOfSheets())); } /** @@ -1501,20 +1385,21 @@ public final class TestBugs extends Base */ @Test public void bug30070() throws Exception { - HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission' - HSSFSheet sh = wb.getSheetAt(0); - HSSFCell cell = sh.getRow(0).getCell(1); - - //B1 uses VBA in the formula - assertEquals("Commission(A1)", cell.getCellFormula()); - - //name sales_1 refers to Commission(Sheet0!$A$1) - int idx = wb.getNameIndex("sales_1"); - assertTrue(idx != -1); - - HSSFName name = wb.getNameAt(idx); - assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula()); - wb.close(); + //contains custom VBA function 'Commission' + try (HSSFWorkbook wb = openSampleWorkbook("30070.xls")) { + HSSFSheet sh = wb.getSheetAt(0); + HSSFCell cell = sh.getRow(0).getCell(1); + + //B1 uses VBA in the formula + assertEquals("Commission(A1)", cell.getCellFormula()); + + //name sales_1 refers to Commission(Sheet0!$A$1) + int idx = wb.getNameIndex("sales_1"); + assertTrue(idx != -1); + + HSSFName name = wb.getNameAt(idx); + assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula()); + } } /** @@ -1533,13 +1418,13 @@ public final class TestBugs extends Base */ @Test public void bug27364() throws Exception { - HSSFWorkbook wb = openSample("27364.xls"); - HSSFSheet sheet = wb.getSheetAt(0); + try (HSSFWorkbook wb = openSampleWorkbook("27364.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); - assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula()); - assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula()); - assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula()); - wb.close(); + assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula()); + assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula()); + assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula()); + } } /** @@ -1548,11 +1433,11 @@ public final class TestBugs extends Base */ @Test public void bug31661() throws Exception { - HSSFWorkbook wb = openSample("31661.xls"); - HSSFSheet sheet = wb.getSheetAt(0); - HSSFCell cell = sheet.getRow(11).getCell(10); //K11 - assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula()); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("31661.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); + HSSFCell cell = sheet.getRow(11).getCell(10); //K11 + assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula()); + } } /** @@ -1560,14 +1445,14 @@ public final class TestBugs extends Base */ @Test public void bug27394() throws Exception { - HSSFWorkbook wb = openSample("27394.xls"); - assertEquals("\u0161\u017E", wb.getSheetName(0)); - assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1)); - HSSFSheet sheet = wb.getSheetAt(0); - - assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue()); - assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue()); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("27394.xls")) { + assertEquals("\u0161\u017E", wb.getSheetName(0)); + assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1)); + HSSFSheet sheet = wb.getSheetAt(0); + + assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue()); + assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue()); + } } /** @@ -1575,25 +1460,16 @@ public final class TestBugs extends Base */ @Test public void bug32191() throws IOException { - HSSFWorkbook wb = openSample("27394.xls"); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - wb.write(out); - out.close(); - int size1 = out.size(); - - out = new ByteArrayOutputStream(); - wb.write(out); - out.close(); - int size2 = out.size(); - - assertEquals(size1, size2); - out = new ByteArrayOutputStream(); - wb.write(out); - out.close(); - int size3 = out.size(); - assertEquals(size2, size3); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("27394.xls"); + ByteArrayOutputStream out1 = new ByteArrayOutputStream(); + ByteArrayOutputStream out2 = new ByteArrayOutputStream(); + ByteArrayOutputStream out3 = new ByteArrayOutputStream()) { + wb.write(out1); + wb.write(out2); + wb.write(out3); + assertArrayEquals(out1.toByteArray(), out2.toByteArray()); + assertArrayEquals(out2.toByteArray(), out3.toByteArray()); + } } /** @@ -1601,28 +1477,10 @@ public final class TestBugs extends Base * (is an excel 95 file though) */ @Test - public void bug46904() throws Exception { - try { - POIFSFileSystem fs = new POIFSFileSystem( - HSSFITestDataProvider.instance.openWorkbookStream("46904.xls")); - new HSSFWorkbook(fs.getRoot(), false).close(); - fail("Should catch exception here"); - } catch (OldExcelFormatException e) { - assertTrue(e.getMessage().startsWith( - "The supplied spreadsheet seems to be Excel" - )); - } - try { - try (POIFSFileSystem fs = new POIFSFileSystem( - HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"))) { - new HSSFWorkbook(fs.getRoot(), false).close(); - fail("Should catch exception here"); - } - } catch (OldExcelFormatException e) { - assertTrue(e.getMessage().startsWith( - "The supplied spreadsheet seems to be Excel" - )); - } + public void bug46904a() throws Exception { + thrown.expect(OldExcelFormatException.class); + thrown.expectMessage("The supplied spreadsheet seems to be Excel"); + simpleTest("46904.xls"); } /** @@ -1631,10 +1489,10 @@ public final class TestBugs extends Base */ @Test public void bug47034() throws Exception { - HSSFWorkbook wb = openSample("47034.xls"); - assertEquals(893, wb.getNumberOfNames()); - assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300)); - wb.close(); + simpleTest("47034.xls", wb -> { + assertEquals(893, wb.getNumberOfNames()); + assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300)); + }); } /** @@ -1643,14 +1501,14 @@ public final class TestBugs extends Base */ @Test public void bug46368() throws Exception { - HSSFWorkbook wb = openSample("46368.xls"); - HSSFSheet s = wb.getSheetAt(0); - HSSFCell cell1 = s.getRow(0).getCell(0); - assertEquals(32770, cell1.getStringCellValue().length()); - - HSSFCell cell2 = s.getRow(2).getCell(0); - assertEquals(32766, cell2.getStringCellValue().length()); - wb.close(); + try (HSSFWorkbook wb = openSampleWorkbook("46368.xls")) { + HSSFSheet s = wb.getSheetAt(0); + HSSFCell cell1 = s.getRow(0).getCell(0); + assertEquals(32770, cell1.getStringCellValue().length()); + + HSSFCell cell2 = s.getRow(2).getCell(0); + assertEquals(32766, cell2.getStringCellValue().length()); + } } /** @@ -1658,37 +1516,37 @@ public final class TestBugs extends Base */ @Test public void bug48180() throws Exception { - HSSFWorkbook wb = openSample("48180.xls"); + try (HSSFWorkbook wb = openSampleWorkbook("48180.xls")) { - HSSFSheet s = wb.getSheetAt(0); - HSSFCell cell1 = s.getRow(0).getCell(0); - assertEquals("test ", cell1.getStringCellValue()); - - HSSFCell cell2 = s.getRow(0).getCell(1); - assertEquals(1.0, cell2.getNumericCellValue(), 0.0); - wb.close(); + HSSFSheet s = wb.getSheetAt(0); + HSSFCell cell1 = s.getRow(0).getCell(0); + assertEquals("test ", cell1.getStringCellValue()); + + HSSFCell cell2 = s.getRow(0).getCell(1); + assertEquals(1.0, cell2.getNumericCellValue(), 0.0); + } } /** * POI 3.5 beta 7 can not read excel file contain list box (Form Control) */ @Test - public void bug47701() { - openSample("47701.xls"); + public void bug47701() throws IOException { + simpleTest("47701.xls"); } @Test - public void bug48026() { - openSample("48026.xls"); + public void bug48026() throws IOException { + simpleTest("48026.xls"); } @Test - public void bug47251() { + public void bug47251() throws IOException { // Firstly, try with one that triggers on InterfaceHdrRecord - openSample("47251.xls"); + simpleTest("47251.xls"); // Now with one that triggers on NoteRecord - openSample("47251_1.xls"); + simpleTest("47251_1.xls"); } /** @@ -1696,37 +1554,37 @@ public final class TestBugs extends Base */ @Test public void bug47847() throws Exception { - HSSFWorkbook wb1 = openSample("47847.xls"); - assertEquals(3, wb1.getNumberOfSheets()); + try (HSSFWorkbook wb1 = openSampleWorkbook("47847.xls")) { + assertEquals(3, wb1.getNumberOfSheets()); - // Find the SST record - UnicodeString withExt = wb1.getWorkbook().getSSTString(0); - UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31); + // Find the SST record + UnicodeString withExt = wb1.getWorkbook().getSSTString(0); + UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31); - assertEquals("O:Alloc:Qty", withExt.getString()); - assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004)); + assertEquals("O:Alloc:Qty", withExt.getString()); + assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004)); - assertEquals("RT", withoutExt.getString()); - assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004)); + assertEquals("RT", withoutExt.getString()); + assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004)); - // Something about continues... + // Something about continues... - // Write out and re-read - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - assertEquals(3, wb2.getNumberOfSheets()); + // Write out and re-read + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + assertEquals(3, wb2.getNumberOfSheets()); - // Check it's the same now - withExt = wb2.getWorkbook().getSSTString(0); - withoutExt = wb2.getWorkbook().getSSTString(31); + // Check it's the same now + withExt = wb2.getWorkbook().getSSTString(0); + withoutExt = wb2.getWorkbook().getSSTString(31); - assertEquals("O:Alloc:Qty", withExt.getString()); - assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004)); + assertEquals("O:Alloc:Qty", withExt.getString()); + assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004)); - assertEquals("RT", withoutExt.getString()); - assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004)); - wb2.close(); + assertEquals("RT", withoutExt.getString()); + assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004)); + } + } } /** @@ -1735,17 +1593,17 @@ public final class TestBugs extends Base */ @Test public void bug49096() throws Exception { - HSSFWorkbook wb1 = openSample("49096.xls"); - assertEquals(1, wb1.getNumberOfSheets()); + try (HSSFWorkbook wb1 = openSampleWorkbook("49096.xls")) { + assertEquals(1, wb1.getNumberOfSheets()); - assertNotNull(wb1.getSheetAt(0)); - wb1.cloneSheet(0); - assertEquals(2, wb1.getNumberOfSheets()); + assertNotNull(wb1.getSheetAt(0)); + wb1.cloneSheet(0); + assertEquals(2, wb1.getNumberOfSheets()); - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - assertEquals(2, wb2.getNumberOfSheets()); - wb2.close(); + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + assertEquals(2, wb2.getNumberOfSheets()); + } + } } /** @@ -1757,57 +1615,56 @@ public final class TestBugs extends Base */ @Test public void bug46664() throws Exception { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet("new_sheet"); - HSSFRow row = sheet.createRow((short) 0); - row.createCell(0).setCellValue(new HSSFRichTextString("Column A")); - row.createCell(1).setCellValue(new HSSFRichTextString("Column B")); - row.createCell(2).setCellValue(new HSSFRichTextString("Column C")); - row.createCell(3).setCellValue(new HSSFRichTextString("Column D")); - row.createCell(4).setCellValue(new HSSFRichTextString("Column E")); - row.createCell(5).setCellValue(new HSSFRichTextString("Column F")); - - //set print area from column a to column c (on first row) - wb1.setPrintArea( - 0, //sheet index - 0, //start column - 2, //end column - 0, //start row - 0 //end row - ); - - HSSFWorkbook wb2 = writeOutAndReadBack(wb1); - wb1.close(); - - // Ensure the tab index - TabIdRecord tr = null; - for (Record r : wb2.getWorkbook().getRecords()) { - if (r instanceof TabIdRecord) { - tr = (TabIdRecord) r; - } - } - assertNotNull(tr); - assertEquals(1, tr._tabids.length); - assertEquals(0, tr._tabids[0]); - - // Ensure the print setup - assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0)); - HSSFName printArea = wb2.getName("Print_Area"); - assertNotNull(printArea); - assertEquals("new_sheet!$A$1:$C$1", printArea.getRefersToFormula()); - - // Needs reference not value - NameRecord nr = wb2.getWorkbook().getNameRecord( - wb2.getNameIndex("Print_Area") - ); - assertEquals("Print_Area", nr.getNameText()); - assertEquals(1, nr.getNameDefinition().length); - assertEquals( - "new_sheet!$A$1:$C$1", - ((Area3DPtg) nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb2)) - ); - assertEquals('R', nr.getNameDefinition()[0].getRVAType()); - wb2.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet("new_sheet"); + HSSFRow row = sheet.createRow((short) 0); + row.createCell(0).setCellValue(new HSSFRichTextString("Column A")); + row.createCell(1).setCellValue(new HSSFRichTextString("Column B")); + row.createCell(2).setCellValue(new HSSFRichTextString("Column C")); + row.createCell(3).setCellValue(new HSSFRichTextString("Column D")); + row.createCell(4).setCellValue(new HSSFRichTextString("Column E")); + row.createCell(5).setCellValue(new HSSFRichTextString("Column F")); + + //set print area from column a to column c (on first row) + wb1.setPrintArea( + 0, //sheet index + 0, //start column + 2, //end column + 0, //start row + 0 //end row + ); + + try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { + // Ensure the tab index + TabIdRecord tr = null; + for (Record r : wb2.getWorkbook().getRecords()) { + if (r instanceof TabIdRecord) { + tr = (TabIdRecord) r; + } + } + assertNotNull(tr); + assertEquals(1, tr._tabids.length); + assertEquals(0, tr._tabids[0]); + + // Ensure the print setup + assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0));
[... 1863 lines stripped ...] --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org