diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/RepeatingRowsAndColumns.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/RepeatingRowsAndColumns.java index df4e3bb64..6ceee7c26 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/RepeatingRowsAndColumns.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/RepeatingRowsAndColumns.java @@ -17,20 +17,23 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; - -import java.io.IOException; import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.util.CellRangeAddress; -/** - * @author Glen Stampoultzis (glens at apache.org) - */ public class RepeatingRowsAndColumns { public static void main(String[] args) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("first sheet"); - wb.createSheet("second sheet"); - wb.createSheet("third sheet"); + HSSFSheet sheet2 = wb.createSheet("second sheet"); + HSSFSheet sheet3 = wb.createSheet("third sheet"); HSSFFont boldFont = wb.createFont(); boldFont.setFontHeightInPoints((short)22); @@ -45,14 +48,17 @@ public class RepeatingRowsAndColumns { cell.setCellStyle(boldStyle); // Set the columns to repeat from column 0 to 2 on the first sheet - wb.setRepeatingRowsAndColumns(0,0,2,-1,-1); + sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); // Set the rows to repeat from row 0 to 2 on the second sheet. - wb.setRepeatingRowsAndColumns(1,-1,-1,0,2); + sheet2.setRepeatingRows(CellRangeAddress.valueOf("1:3")); // Set the the repeating rows and columns on the third sheet. - wb.setRepeatingRowsAndColumns(2,4,5,1,2); + CellRangeAddress cra = CellRangeAddress.valueOf("D1:E2"); + sheet3.setRepeatingColumns(cra); + sheet3.setRepeatingRows(cra); 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/WorkingWithPageSetup.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java index d1be7af17..25f994f95 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java @@ -20,6 +20,7 @@ import java.io.FileOutputStream; 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.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -63,9 +64,11 @@ public class WorkingWithPageSetup { row4.createCell(5).setCellValue(2.5); // Set the columns to repeat from column 0 to 2 on the first sheet - wb.setRepeatingRowsAndColumns(0,0,2,-1,-1); + sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); // Set the the repeating rows and columns on the second sheet. - wb.setRepeatingRowsAndColumns(1,4,5,1,2); + CellRangeAddress cra = CellRangeAddress.valueOf("E2:F3"); + sheet2.setRepeatingColumns(cra); + sheet2.setRepeatingRows(cra); //set the print area for the first sheet wb.setPrintArea(0, 1, 2, 0, 3); @@ -74,5 +77,7 @@ public class WorkingWithPageSetup { FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index eb097c62e..e4b006833 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -2199,6 +2199,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { * * @return the name of this sheet */ + @SuppressWarnings("resource") @Override public String getSheetName() { HSSFWorkbook wb = getWorkbook(); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index ca4e79a8e..5f179f032 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -17,8 +17,8 @@ package org.apache.poi.hssf.usermodel; -import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME; +import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -89,7 +89,6 @@ import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; 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.ss.util.WorkbookUtil; import org.apache.poi.util.Configurator; import org.apache.poi.util.HexDump; @@ -1104,57 +1103,6 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss : true; } - /** - * Sets the repeating rows and columns for a sheet (as found in - * 2003:File->PageSetup->Sheet, 2007:Page Layout->Print Titles). - * This is function is included in the workbook - * because it creates/modifies name records which are stored at the - * workbook level. - *
- * To set just repeating columns: - *
- * workbook.setRepeatingRowsAndColumns(0,0,1,-1-1); - *- * To set just repeating rows: - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); - *- * To remove all repeating rows and columns for a sheet. - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1); - *- * - * @param sheetIndex 0 based index to sheet. - * @param startColumn 0 based start of repeating columns. - * @param endColumn 0 based end of repeating columns. - * @param startRow 0 based start of repeating rows. - * @param endRow 0 based end of repeating rows. - * - * @deprecated use {@link HSSFSheet#setRepeatingRows(CellRangeAddress)} - * or {@link HSSFSheet#setRepeatingColumns(CellRangeAddress)} - */ - @Override - @Deprecated - public void setRepeatingRowsAndColumns(int sheetIndex, - int startColumn, int endColumn, - int startRow, int endRow) { - HSSFSheet sheet = getSheetAt(sheetIndex); - - CellRangeAddress rows = null; - CellRangeAddress cols = null; - - if (startRow != -1) { - rows = new CellRangeAddress(startRow, endRow, -1, -1); - } - if (startColumn != -1) { - cols = new CellRangeAddress(-1, -1, startColumn, endColumn); - } - - sheet.setRepeatingRows(rows); - sheet.setRepeatingColumns(cols); - } - - int findExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode) { for(int defNameIndex =0; defNameIndex
- * To set just repeating columns: - *
- * workbook.setRepeatingRowsAndColumns(0,0,1,-1-1); - *- * To set just repeating rows: - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); - *- * To remove all repeating rows and columns for a sheet. - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1); - *- * - * @param sheetIndex 0 based index to sheet. - * @param startColumn 0 based start of repeating columns. - * @param endColumn 0 based end of repeating columns. - * @param startRow 0 based start of repeating rows. - * @param endRow 0 based end of repeating rows. - * - * @deprecated use {@link Sheet#setRepeatingRows(CellRangeAddress)} - * or {@link Sheet#setRepeatingColumns(CellRangeAddress)} - */ - @Deprecated - void setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow); - /** * Create a new Font and add it to the workbook's font table * diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 8e15739b4..3342fb1b8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -47,7 +47,6 @@ import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; 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.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.util.TempFile; @@ -777,41 +776,6 @@ public class SXSSFWorkbook implements Workbook { } } - /** - * Sets the repeating rows and columns for a sheet (as found in - * File->PageSetup->Sheet). This is function is included in the workbook - * because it creates/modifies name records which are stored at the - * workbook level. - *
- * To set just repeating columns: - *
- * workbook.setRepeatingRowsAndColumns(0,0,1,-1-1); - *- * To set just repeating rows: - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); - *- * To remove all repeating rows and columns for a sheet. - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1); - *- * - * @param sheetIndex 0 based index to sheet. - * @param startColumn 0 based start of repeating columns. - * @param endColumn 0 based end of repeating columns. - * @param startRow 0 based start of repeating rows. - * @param endRow 0 based end of repeating rows. - * - * @deprecated use {@link SXSSFSheet#setRepeatingRows(CellRangeAddress)} - * or {@link SXSSFSheet#setRepeatingColumns(CellRangeAddress)} - */ - @Deprecated - @Override - public void setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) - { - _wb.setRepeatingRowsAndColumns(sheetIndex,startColumn,endColumn,startRow,endRow); - } - /** * Create a new Font and add it to the workbook's font table * diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index c02cf5227..f09313524 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -62,7 +62,6 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; 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.ss.util.CellReference; import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.util.Beta; @@ -1451,53 +1450,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { setPrintArea(sheetIndex, reference); } - - /** - * Sets the repeating rows and columns for a sheet. - * - * To set just repeating columns: - *
- * workbook.setRepeatingRowsAndColumns(0,0,1,-1,-1); - *- * To set just repeating rows: - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); - *- * To remove all repeating rows and columns for a sheet. - *
- * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1); - *- * - * @param sheetIndex 0 based index to sheet. - * @param startColumn 0 based start of repeating columns. - * @param endColumn 0 based end of repeating columns. - * @param startRow 0 based start of repeating rows. - * @param endRow 0 based end of repeating rows. - * - * @deprecated use {@link XSSFSheet#setRepeatingRows(CellRangeAddress)} - * or {@link XSSFSheet#setRepeatingColumns(CellRangeAddress)} - */ - @Deprecated - @Override - public void setRepeatingRowsAndColumns(int sheetIndex, - int startColumn, int endColumn, - int startRow, int endRow) { - XSSFSheet sheet = getSheetAt(sheetIndex); - - CellRangeAddress rows = null; - CellRangeAddress cols = null; - - if (startRow != -1) { - rows = new CellRangeAddress(startRow, endRow, -1, -1); - } - if (startColumn != -1) { - cols = new CellRangeAddress(-1, -1, startColumn, endColumn); - } - - sheet.setRepeatingRows(rows); - sheet.setRepeatingColumns(cols); - } - private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) { //windows excel example: Sheet1!$C$3:$E$4 CellReference colRef = new CellReference(sheetName, startR, startC, true, true); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java index 0f37da3ba..f131195c1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java @@ -23,8 +23,8 @@ import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; import org.apache.poi.ss.usermodel.PrintSetup; 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.xssf.SXSSFITestDataProvider; -import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.junit.Ignore; import org.junit.Test; @@ -45,16 +45,16 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { * Setting repeating rows and columns shouldn't break * any print settings that were there before */ - @SuppressWarnings("deprecation") @Test public void bug49253() throws Exception { Workbook wb1 = new SXSSFWorkbook(); Workbook wb2 = new SXSSFWorkbook(); + CellRangeAddress cra = CellRangeAddress.valueOf("C2:D3"); // No print settings before repeating Sheet s1 = wb1.createSheet(); - - wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2); + s1.setRepeatingColumns(cra); + s1.setRepeatingRows(cra); PrintSetup ps1 = s1.getPrintSetup(); assertEquals(false, ps1.getValidSettings()); @@ -68,8 +68,8 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { ps2.setLandscape(false); assertEquals(true, ps2.getValidSettings()); assertEquals(false, ps2.getLandscape()); - - wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2); + s2.setRepeatingColumns(cra); + s2.setRepeatingRows(cra); ps2 = s2.getPrintSetup(); assertEquals(true, ps2.getValidSettings()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 2fd86cff5..e6b4f099d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -1070,18 +1070,18 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * Setting repeating rows and columns shouldn't break * any print settings that were there before */ - @SuppressWarnings("deprecation") @Test public void bug49253() throws IOException { XSSFWorkbook wb1 = new XSSFWorkbook(); XSSFWorkbook wb2 = new XSSFWorkbook(); + CellRangeAddress cra = CellRangeAddress.valueOf("C2:D3"); // No print settings before repeating XSSFSheet s1 = wb1.createSheet(); assertEquals(false, s1.getCTWorksheet().isSetPageSetup()); assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); - - wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2); + s1.setRepeatingColumns(cra); + s1.setRepeatingRows(cra); assertEquals(true, s1.getCTWorksheet().isSetPageSetup()); assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); @@ -1100,8 +1100,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { ps2.setLandscape(false); assertEquals(true, ps2.getValidSettings()); assertEquals(false, ps2.getLandscape()); - - wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2); + s2.setRepeatingColumns(cra); + s2.setRepeatingRows(cra); ps2 = s2.getPrintSetup(); assertEquals(true, s2.getCTWorksheet().isSetPageSetup()); @@ -2133,8 +2133,8 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { /** * A .xlsx file with no Shared Strings table should open fine * in read-only mode - * @throws InvalidFormatException */ + @SuppressWarnings("resource") @Test public void bug57482() throws IOException, InvalidFormatException { for (PackageAccess access : new PackageAccess[] { @@ -2497,6 +2497,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * .xlsx supports 64000 cell styles, the style indexes after * 32,767 must not be -32,768, then -32,767, -32,766 */ + @SuppressWarnings("resource") @Test public void bug57880() throws IOException { int numStyles = 33000; @@ -2835,7 +2836,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } @Test - public void test58731() throws Exception { + public void test58731() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58731.xlsx"); Sheet sheet = wb.createSheet("Java Books"); @@ -2866,6 +2867,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertNotNull(sheet.getRow(0)); assertNotNull(sheet.getRow(0).getCell(0)); assertEquals(bookData[0][0], sheet.getRow(0).getCell(0).getStringCellValue()); + + wb2.close(); + wb.close(); } /** @@ -2875,17 +2879,19 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { * ! Rule: Package require content types when retrieving a part from a package. [M.1.14] */ @Test - public void test58760() throws Exception { - Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58760.xlsx"); - assertEquals(1, wb.getNumberOfSheets()); - assertEquals("Sheet1", wb.getSheetName(0)); - wb = XSSFTestDataSamples.writeOutAndReadBack(wb); - assertEquals(1, wb.getNumberOfSheets()); - assertEquals("Sheet1", wb.getSheetName(0)); + public void test58760() throws IOException { + Workbook wb1 = XSSFTestDataSamples.openSampleWorkbook("58760.xlsx"); + assertEquals(1, wb1.getNumberOfSheets()); + assertEquals("Sheet1", wb1.getSheetName(0)); + Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1); + assertEquals(1, wb2.getNumberOfSheets()); + assertEquals("Sheet1", wb2.getSheetName(0)); + wb2.close(); + wb1.close(); } @Test - public void test57236() throws Exception { + public void test57236() throws IOException { // Having very small numbers leads to different formatting, Excel uses the scientific notation, but POI leads to "0" /* @@ -2958,7 +2964,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { @Ignore("Creates files for checking results manually, actual values are tested in Test*CellStyle") @Test - public void test58043() throws Exception { + public void test58043() throws IOException { createXls(); createXlsx(); } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java index 2a92a9516..49a925c13 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java @@ -21,8 +21,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.lang.reflect.Field; - import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -32,6 +30,7 @@ import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.BaseTestNamedRange; import org.apache.poi.ss.util.AreaReference; +import org.apache.poi.ss.util.CellRangeAddress; import org.junit.Test; /** @@ -61,20 +60,19 @@ public final class TestHSSFName extends BaseTestNamedRange { HSSFSheet sheet = wb.createSheet("FirstSheet"); // set repeating rows and columns twice for the first sheet + CellRangeAddress cra = CellRangeAddress.valueOf("A1:A3"); for (int i = 0; i < 2; i++) { - wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3-1); + sheet.setRepeatingColumns(cra); + sheet.setRepeatingRows(cra); sheet.createFreezePane(0, 3); } assertEquals(1, wb.getNumberOfNames()); HSSFName nr1 = wb.getNameAt(0); assertEquals("Print_Titles", nr1.getNameName()); -// if (false) { -// // TODO - full column references not rendering properly, absolute markers not present either -// assertEquals("FirstSheet!$A:$A,FirstSheet!$1:$3", nr1.getRefersToFormula()); -// } else { - assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula()); -// } + // TODO - full column references not rendering properly, absolute markers not present either + // assertEquals("FirstSheet!$A:$A,FirstSheet!$1:$3", nr1.getRefersToFormula()); + assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula()); // Save and re-open HSSFWorkbook nwb = HSSFTestDataSamples.writeOutAndReadBack(wb); @@ -89,7 +87,9 @@ public final class TestHSSFName extends BaseTestNamedRange { // check that setting RR&C on a second sheet causes a new Print_Titles built-in // name to be created sheet = nwb.createSheet("SecondSheet"); - nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0); + cra = CellRangeAddress.valueOf("B1:C1"); + sheet.setRepeatingColumns(cra); + sheet.setRepeatingRows(cra); assertEquals(2, nwb.getNumberOfNames()); HSSFName nr2 = nwb.getNameAt(1); @@ -97,19 +97,6 @@ public final class TestHSSFName extends BaseTestNamedRange { assertEquals("Print_Titles", nr2.getNameName()); assertEquals("SecondSheet!B:C,SecondSheet!$A$1:$IV$1", nr2.getRefersToFormula()); -// if (false) { -// // In case you fancy checking in excel, to ensure it -// // won't complain about the file now -// try { -// File tempFile = TempFile.createTempFile("POI-45126-", ".xls"); -// FileOutputStream fout = new FileOutputStream(tempFile); -// nwb.write(fout); -// fout.close(); -// System.out.println("check out " + tempFile.getAbsolutePath()); -// } catch (IOException e) { -// throw new RuntimeException(e); -// } -// } nwb.close(); } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index efc7625e7..64bfb2370 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -17,9 +17,11 @@ package org.apache.poi.hssf.usermodel; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.IOException; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -28,24 +30,17 @@ import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Name; 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.TempFile; - -import junit.framework.TestCase; +import org.junit.Test; /** * Class to test Workbook functionality - * - * @author Andrew C. Oliver - * @author Greg Merrill - * @author Siggi Cherem */ -public final class TestWorkbook extends TestCase { +public final class TestWorkbook { private static final String LAST_NAME_KEY = "lastName"; private static final String FIRST_NAME_KEY = "firstName"; private static final String SSN_KEY = "ssn"; @@ -70,22 +65,17 @@ public final class TestWorkbook extends TestCase { * Last row, first row is tested against the correct values (99,0).
* FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.
- * */ + @Test public void testWriteSheetSimple() throws IOException { - File file = TempFile.createTempFile("testWriteSheetSimple", - ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet s = wb1.createSheet(); for (int rownum = 0; rownum < 100; rownum++) { - r = s.createRow(rownum); + HSSFRow r = s.createRow(rownum); for (int cellnum = 0; cellnum < 50; cellnum += 2) { - c = r.createCell(cellnum); + HSSFCell c = r.createCell(cellnum); c.setCellValue(rownum * 10000 + cellnum + ((( double ) rownum / 1000) + (( double ) cellnum / 10000))); @@ -93,11 +83,19 @@ public final class TestWorkbook extends TestCase { c.setCellValue(new HSSFRichTextString("TEST")); } } - wb.write(out); - out.close(); - sanityChecker.checkHSSFWorkbook(wb); + + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + + sanityChecker.checkHSSFWorkbook(wb1); assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); + + sanityChecker.checkHSSFWorkbook(wb2); + s = wb2.getSheetAt(0); + assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); + assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); + wb2.close(); + wb1.close(); } /** @@ -108,25 +106,17 @@ public final class TestWorkbook extends TestCase { * Last row, first row is tested against the correct values (74,25).
* FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.
- * */ - - public void testWriteModifySheetSimple() - throws IOException - { - File file = TempFile.createTempFile("testWriteSheetSimple", - ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; + @Test + public void testWriteModifySheetSimple() throws IOException { + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet s = wb1.createSheet(); for (int rownum = 0; rownum < 100; rownum++) { - r = s.createRow(rownum); + HSSFRow r = s.createRow(rownum); for (int cellnum = 0; cellnum < 50; cellnum += 2) { - c = r.createCell(cellnum); + HSSFCell c = r.createCell(cellnum); c.setCellValue(rownum * 10000 + cellnum + ((( double ) rownum / 1000) + (( double ) cellnum / 10000))); @@ -135,19 +125,27 @@ public final class TestWorkbook extends TestCase { } } for (int rownum = 0; rownum < 25; rownum++) { - r = s.getRow(rownum); + HSSFRow r = s.getRow(rownum); s.removeRow(r); } for (int rownum = 75; rownum < 100; rownum++) { - r = s.getRow(rownum); + HSSFRow r = s.getRow(rownum); s.removeRow(r); } - wb.write(out); - out.close(); - sanityChecker.checkHSSFWorkbook(wb); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + + sanityChecker.checkHSSFWorkbook(wb1); assertEquals("LAST ROW == 74", 74, s.getLastRowNum()); assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum()); + + sanityChecker.checkHSSFWorkbook(wb2); + s = wb2.getSheetAt(0); + assertEquals("LAST ROW == 74", 74, s.getLastRowNum()); + assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum()); + + wb2.close(); + wb1.close(); } /** @@ -156,15 +154,15 @@ public final class TestWorkbook extends TestCase { * SUCCESS: HSSF reads the sheet. Matches values in their particular positions.
* FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values * in the sheet in their known positions.
- * */ - - public void testReadSimple() { - HSSFWorkbook workbook = openSample("Simple.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); + @Test + public void testReadSimple() throws IOException { + HSSFWorkbook wb = openSample("Simple.xls"); + HSSFSheet sheet = wb.getSheetAt(0); HSSFCell cell = sheet.getRow(0).getCell(0); assertEquals(REPLACE_ME, cell .getRichStringCellValue().getString()); + wb.close(); } /** @@ -173,18 +171,19 @@ public final class TestWorkbook extends TestCase { * SUCCESS: HSSF reads the sheet. Matches values in their particular positions and format is correct
* FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values * in the sheet in their known positions.
- * */ - - public void testReadSimpleWithDataFormat() { - HSSFWorkbook workbook = openSample("SimpleWithDataFormat.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); - HSSFDataFormat format = workbook.createDataFormat(); + @Test + public void testReadSimpleWithDataFormat() throws IOException { + HSSFWorkbook wb = openSample("SimpleWithDataFormat.xls"); + HSSFSheet sheet = wb.getSheetAt(0); + HSSFDataFormat format = wb.createDataFormat(); HSSFCell cell = sheet.getRow(0).getCell(0); assertEquals(1.25,cell.getNumericCellValue(), 1e-10); assertEquals(format.getFormat(cell.getCellStyle().getDataFormat()), "0.0"); + + wb.close(); } /** @@ -193,47 +192,36 @@ public final class TestWorkbook extends TestCase { * SUCCESS: HSSF reads the sheet. Matches values in their particular positions and format is correct
* FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values * in the sheet in their known positions.
- * */ - + @Test public void testWriteDataFormat() throws IOException { - File file = TempFile.createTempFile("testWriteDataFormat", ".xls"); - FileOutputStream out = new FileOutputStream(file); - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; - HSSFDataFormat format = wb.createDataFormat(); - HSSFCellStyle cs = wb.createCellStyle(); + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet s1 = wb1.createSheet(); + HSSFDataFormat format = wb1.createDataFormat(); + HSSFCellStyle cs = wb1.createCellStyle(); short df = format.getFormat("0.0"); cs.setDataFormat(df); - r = s.createRow(0); - c = r.createCell(0); - c.setCellStyle(cs); - c.setCellValue(1.25); + HSSFCell c1 = s1.createRow(0).createCell(0); + c1.setCellStyle(cs); + c1.setCellValue(1.25); - wb.write(out); - out.close(); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); - FileInputStream stream = new FileInputStream(file); - POIFSFileSystem fs = new POIFSFileSystem(stream); - HSSFWorkbook workbook = new HSSFWorkbook(fs); - HSSFSheet sheet = workbook.getSheetAt(0); - HSSFCell cell = sheet.getRow(0).getCell(0); - format = workbook.createDataFormat(); + HSSFSheet s2 = wb2.getSheetAt(0); + HSSFCell c2 = s2.getRow(0).getCell(0); + format = wb2.createDataFormat(); - assertEquals(1.25, cell.getNumericCellValue(), 1e-10); + assertEquals(1.25, c2.getNumericCellValue(), 1e-10); assertEquals(format.getFormat(df), "0.0"); - assertEquals(format, workbook.createDataFormat()); + assertEquals(format, wb2.createDataFormat()); - stream.close(); - - workbook.close(); - wb.close(); + wb2.close(); + wb1.close(); } /** @@ -244,15 +232,17 @@ public final class TestWorkbook extends TestCase { * in the sheet in their known positions.
* */ - - public void testReadEmployeeSimple() { - HSSFWorkbook workbook = openSample("Employee.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); + @Test + public void testReadEmployeeSimple() throws IOException { + HSSFWorkbook wb = openSample("Employee.xls"); + HSSFSheet sheet = wb.getSheetAt(0); assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(1).getRichStringCellValue().getString()); assertEquals(LAST_NAME_KEY, sheet.getRow(3).getCell(2).getRichStringCellValue().getString()); assertEquals(FIRST_NAME_KEY, sheet.getRow(4).getCell(2).getRichStringCellValue().getString()); assertEquals(SSN_KEY, sheet.getRow(5).getCell(2).getRichStringCellValue().getString()); + + wb.close(); } /** @@ -265,20 +255,22 @@ public final class TestWorkbook extends TestCase { * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.
- * */ - - public void testModifySimple() { - HSSFWorkbook workbook = openSample("Simple.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); + @Test + public void testModifySimple() throws IOException { + HSSFWorkbook wb1 = openSample("Simple.xls"); + HSSFSheet sheet = wb1.getSheetAt(0); HSSFCell cell = sheet.getRow(0).getCell(0); cell.setCellValue(new HSSFRichTextString(REPLACED)); - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); - sheet = workbook.getSheetAt(0); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + sheet = wb2.getSheetAt(0); cell = sheet.getRow(0).getCell(0); assertEquals(REPLACED, cell.getRichStringCellValue().getString()); + + wb2.close(); + wb1.close(); } /** @@ -292,20 +284,20 @@ public final class TestWorkbook extends TestCase { * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced or the incorrect cell has its value replaced * or is incorrect.
- * */ - public void testModifySimpleWithSkip() { - HSSFWorkbook workbook = openSample("SimpleWithSkip.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); + @Test + public void testModifySimpleWithSkip() throws IOException { + HSSFWorkbook wb1 = openSample("SimpleWithSkip.xls"); + HSSFSheet sheet = wb1.getSheetAt(0); HSSFCell cell = sheet.getRow(0).getCell(1); cell.setCellValue(new HSSFRichTextString(REPLACED)); cell = sheet.getRow(1).getCell(0); cell.setCellValue(new HSSFRichTextString(REPLACED)); - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - sheet = workbook.getSheetAt(0); + sheet = wb2.getSheetAt(0); cell = sheet.getRow(0).getCell(1); assertEquals(REPLACED, cell.getRichStringCellValue().getString()); cell = sheet.getRow(0).getCell(0); @@ -314,6 +306,9 @@ public final class TestWorkbook extends TestCase { assertEquals(REPLACED, cell.getRichStringCellValue().getString()); cell = sheet.getRow(1).getCell(1); assertEquals(DO_NOT_REPLACE, cell.getRichStringCellValue().getString()); + + wb2.close(); + wb1.close(); } /** @@ -327,28 +322,26 @@ public final class TestWorkbook extends TestCase { * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.
- * */ - public void testModifySimpleWithStyling() { - HSSFWorkbook workbook = openSample("SimpleWithStyling.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); + @Test + public void testModifySimpleWithStyling() throws IOException { + HSSFWorkbook wb1 = openSample("SimpleWithStyling.xls"); + HSSFSheet sheet = wb1.getSheetAt(0); - for (int k = 0; k < 4; k++) - { + for (int k = 0; k < 4; k++) { HSSFCell cell = sheet.getRow(k).getCell(0); - cell.setCellValue(new HSSFRichTextString(REPLACED)); } - - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); - sheet = workbook.getSheetAt(0); - for (int k = 0; k < 4; k++) - { + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + sheet = wb2.getSheetAt(0); + for (int k = 0; k < 4; k++) { HSSFCell cell = sheet.getRow(k).getCell(0); - assertEquals(REPLACED, cell.getRichStringCellValue().getString()); } + + wb2.close(); + wb1.close(); } /** @@ -362,12 +355,12 @@ public final class TestWorkbook extends TestCase { * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced.
- * */ - public void testModifyEmployee() { - HSSFWorkbook workbook = openSample("Employee.xls"); - HSSFSheet sheet = workbook.getSheetAt(0); - HSSFCell cell = sheet.getRow(3).getCell(2); + @Test + public void testModifyEmployee() throws IOException { + HSSFWorkbook wb1 = openSample("Employee.xls"); + HSSFSheet sheet = wb1.getSheetAt(0); + HSSFCell cell = sheet.getRow(3).getCell(2); cell.setCellValue(new HSSFRichTextString(LAST_NAME_VALUE)); cell = sheet.getRow(4).getCell(2); @@ -375,12 +368,15 @@ public final class TestWorkbook extends TestCase { cell = sheet.getRow(5).getCell(2); cell.setCellValue(new HSSFRichTextString(SSN_VALUE)); - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); - sheet = workbook.getSheetAt(0); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + sheet = wb2.getSheetAt(0); assertEquals(EMPLOYEE_INFORMATION, sheet.getRow(1).getCell(1).getRichStringCellValue().getString()); assertEquals(LAST_NAME_VALUE, sheet.getRow(3).getCell(2).getRichStringCellValue().getString()); assertEquals(FIRST_NAME_VALUE, sheet.getRow(4).getCell(2).getRichStringCellValue().getString()); assertEquals(SSN_VALUE, sheet.getRow(5).getCell(2).getRichStringCellValue().getString()); + + wb2.close(); + wb1.close(); } /** @@ -389,15 +385,17 @@ public final class TestWorkbook extends TestCase { * identify the cell as numeric and convert it to a NumberRecord.
* SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell.
* FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell
- * */ - public void testReadSheetWithRK() { - HSSFWorkbook h = openSample("rk.xls"); - HSSFSheet s = h.getSheetAt(0); - HSSFCell c = s.getRow(0).getCell(0); - int a = c.getCellType(); + @Test + public void testReadSheetWithRK() throws IOException { + HSSFWorkbook wb = openSample("rk.xls"); + HSSFSheet s = wb.getSheetAt(0); + HSSFCell c = s.getRow(0).getCell(0); + int a = c.getCellType(); assertEquals(a, Cell.CELL_TYPE_NUMERIC); + + wb.close(); } /** @@ -408,11 +406,11 @@ public final class TestWorkbook extends TestCase { * Last row, first row is tested against the correct values (74,25).
* FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. * HSSFSheet last row or first row is incorrect.
- * */ - public void testWriteModifySheetMerged() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); + @Test + public void testWriteModifySheetMerged() throws IOException { + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet s = wb1.createSheet(); for (int rownum = 0; rownum < 100; rownum++) { HSSFRow r = s.createRow(rownum); @@ -428,15 +426,18 @@ public final class TestWorkbook extends TestCase { } s.addMergedRegion(new CellRangeAddress(0, 10, 0, 10)); s.addMergedRegion(new CellRangeAddress(30, 40, 5, 15)); - sanityChecker.checkHSSFWorkbook(wb); - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sanityChecker.checkHSSFWorkbook(wb1); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - s = wb.getSheetAt(0); + s = wb2.getSheetAt(0); CellRangeAddress r1 = s.getMergedRegion(0); CellRangeAddress r2 = s.getMergedRegion(1); confirmRegion(new CellRangeAddress(0, 10, 0, 10), r1); confirmRegion(new CellRangeAddress(30, 40,5, 15), r2); + + wb2.close(); + wb1.close(); } private static void confirmRegion(CellRangeAddress ra, CellRangeAddress rb) { @@ -450,6 +451,7 @@ public final class TestWorkbook extends TestCase { * Test the backup field gets set as expected. * @throws IOException */ + @Test public void testBackupRecord() throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); wb.createSheet(); @@ -490,6 +492,7 @@ public final class TestWorkbook extends TestCase { * We need to make sure only one LabelSSTRecord is produced. * @throws IOException */ + @Test public void testRepeatingBug() throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Design Variants"); @@ -511,9 +514,10 @@ public final class TestWorkbook extends TestCase { * Test for row indexes beyond {@link Short#MAX_VALUE}. * This bug was first fixed in svn r352609. */ - public void testRowIndexesBeyond32768() { - HSSFWorkbook workbook = new HSSFWorkbook(); - HSSFSheet sheet = workbook.createSheet(); + @Test + public void testRowIndexesBeyond32768() throws IOException { + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet sheet = wb1.createSheet(); HSSFRow row; HSSFCell cell; for (int i = 32700; i < 32771; i++) { @@ -521,59 +525,52 @@ public final class TestWorkbook extends TestCase { cell = row.createCell(0); cell.setCellValue(i); } - sanityChecker.checkHSSFWorkbook(workbook); + sanityChecker.checkHSSFWorkbook(wb1); assertEquals("LAST ROW == 32770", 32770, sheet.getLastRowNum()); cell = sheet.getRow(32770).getCell(0); double lastVal = cell.getNumericCellValue(); - HSSFWorkbook wb = HSSFTestDataSamples.writeOutAndReadBack(workbook); - HSSFSheet s = wb.getSheetAt(0); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + HSSFSheet s = wb2.getSheetAt(0); row = s.getRow(32770); cell = row.getCell(0); assertEquals("Value from last row == 32770", lastVal, cell.getNumericCellValue(), 0); assertEquals("LAST ROW == 32770", 32770, s.getLastRowNum()); + + wb2.close(); + wb1.close(); } /** - * Generate a file to visually/programmatically verify repeating rows and cols made it - * - * Test setRepeatingRowsAndColumns with alternating columns -0 and 1. - * - * This test intentionally tests the deprecated - * {@link HSSFWorkbook#setRepeatingRowsAndColumns(int, int, int, int, int)} function + * Generate a file to verify repeating rows and cols made it */ - @SuppressWarnings("deprecation") - public void testRepeatingColsRows() throws IOException - { - HSSFWorkbook workbook = new HSSFWorkbook(); - HSSFSheet sheet = workbook.createSheet("Test Print Titles"); + @Test + public void testRepeatingColsRows() throws IOException { + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet sheet = wb1.createSheet("Test Print Titles"); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString("hi")); + CellRangeAddress cra = CellRangeAddress.valueOf("A1:B1"); + sheet.setRepeatingColumns(cra); + sheet.setRepeatingRows(cra); - workbook.setRepeatingRowsAndColumns(0, 0, 1, 0, 0); - - File file = TempFile.createTempFile("testPrintTitles",".xls"); - - FileOutputStream fileOut = new FileOutputStream(file); - workbook.write(fileOut); - fileOut.close(); - - assertTrue("file exists",file.exists()); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + sheet = wb2.getSheetAt(0); + assertEquals("A:B", sheet.getRepeatingColumns().formatAsString()); + assertEquals("1:1", sheet.getRepeatingRows().formatAsString()); - workbook.close(); + wb2.close(); + wb1.close(); } /** * Test setRepeatingRowsAndColumns when startRow and startColumn are -1. - * - * This test intentionally tests the deprecated - * {@link HSSFWorkbook#setRepeatingRowsAndColumns(int, int, int, int, int)} function */ - @SuppressWarnings("deprecation") + @Test public void testRepeatingColsRowsMinusOne() throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); @@ -583,24 +580,29 @@ public final class TestWorkbook extends TestCase { HSSFCell cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString("hi")); + CellRangeAddress cra = new CellRangeAddress(-1, 1, -1, 1); + try { + sheet.setRepeatingColumns(cra); + fail("invalid start index is ignored"); + } catch (IllegalArgumentException e) {} + + try { + sheet.setRepeatingRows(cra); + fail("invalid start index is ignored"); + } catch (IllegalArgumentException e) {} - - workbook.setRepeatingRowsAndColumns(0, -1, 1, -1, 0); - - File file = TempFile.createTempFile("testPrintTitlesA",".xls"); - - FileOutputStream fileOut = new FileOutputStream(file); - workbook.write(fileOut); - fileOut.close(); - - assertTrue("file exists",file.exists()); + sheet.setRepeatingColumns(null); + sheet.setRepeatingRows(null); + + HSSFTestDataSamples.writeOutAndReadBack(workbook).close(); workbook.close(); } + @Test public void testAddMergedRegionWithRegion() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); + HSSFWorkbook wb1 = new HSSFWorkbook(); + HSSFSheet s = wb1.createSheet(); for (int rownum = 0; rownum < 100; rownum++) { HSSFRow r = s.createRow(rownum); @@ -616,86 +618,65 @@ public final class TestWorkbook extends TestCase { } s.addMergedRegion(new CellRangeAddress(0, 10, 0, 10)); s.addMergedRegion(new CellRangeAddress(30, 40, 5, 15)); - sanityChecker.checkHSSFWorkbook(wb); - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sanityChecker.checkHSSFWorkbook(wb1); + HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - s = wb.getSheetAt(0); + s = wb2.getSheetAt(0); CellRangeAddress r1 = s.getMergedRegion(0); CellRangeAddress r2 = s.getMergedRegion(1); confirmRegion(new CellRangeAddress(0, 10, 0, 10), r1); confirmRegion(new CellRangeAddress(30, 40,5, 15), r2); - wb.close(); + wb2.close(); + wb1.close(); } - + @Test public void testBug58085RemoveSheetWithNames() throws Exception { - reReadWithRemovedSheetWithName(writeWithRemovedSheetWithName()); - } - - private static HSSFWorkbook writeWithRemovedSheetWithName() throws IOException { - HSSFWorkbook workbook = new HSSFWorkbook(); - Sheet sheet1 = workbook.createSheet("sheet1"); - Sheet sheet2 = workbook.createSheet("sheet2"); - Sheet sheet3 = workbook.createSheet("sheet3"); + HSSFWorkbook wb1 = new HSSFWorkbook(); + Sheet sheet1 = wb1.createSheet("sheet1"); + Sheet sheet2 = wb1.createSheet("sheet2"); + Sheet sheet3 = wb1.createSheet("sheet3"); sheet1.createRow(0).createCell((short) 0).setCellValue("val1"); sheet2.createRow(0).createCell((short) 0).setCellValue("val2"); sheet3.createRow(0).createCell((short) 0).setCellValue("val3"); - Name namedCell1 = workbook.createName(); + Name namedCell1 = wb1.createName(); namedCell1.setNameName("name1"); String reference1 = "sheet1!$A$1"; namedCell1.setRefersToFormula(reference1); - Name namedCell2= workbook.createName(); + Name namedCell2= wb1.createName(); namedCell2.setNameName("name2"); String reference2 = "sheet2!$A$1"; namedCell2.setRefersToFormula(reference2); - Name namedCell3 = workbook.createName(); + Name namedCell3 = wb1.createName(); namedCell3.setNameName("name3"); String reference3 = "sheet3!$A$1"; namedCell3.setRefersToFormula(reference3); - return workbook; - } - - private static void reReadWithRemovedSheetWithName(HSSFWorkbook workbookBefore) throws Exception { - Workbook workbook = HSSFTestDataSamples.writeOutAndReadBack(workbookBefore); + Workbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); + wb1.close(); - System.out.println("Before removing sheet1..."); + Name nameCell = wb2.getName("name1"); + assertEquals("sheet1!$A$1", nameCell.getRefersToFormula()); + nameCell = wb2.getName("name2"); + assertEquals("sheet2!$A$1", nameCell.getRefersToFormula()); + nameCell = wb2.getName("name3"); + assertEquals("sheet3!$A$1", nameCell.getRefersToFormula()); - Name nameCell = workbook.getName("name1"); - System.out.println("name1: " + nameCell.getRefersToFormula()); - - nameCell = workbook.getName("name2"); - System.out.println("name2: " + nameCell.getRefersToFormula()); + wb2.removeSheetAt(wb2.getSheetIndex("sheet1")); - nameCell = workbook.getName("name3"); - System.out.println("name3: " + nameCell.getRefersToFormula()); + nameCell = wb2.getName("name1"); + assertEquals("#REF!$A$1", nameCell.getRefersToFormula()); + nameCell = wb2.getName("name2"); + assertEquals("sheet2!$A$1", nameCell.getRefersToFormula()); + nameCell = wb2.getName("name3"); + assertEquals("sheet3!$A$1", nameCell.getRefersToFormula()); - workbook.removeSheetAt(workbook.getSheetIndex("sheet1")); - - /*FileOutputStream fos = new FileOutputStream(AFTER_FILE); - try { - workbook.write(fos); - } finally { - fos.close(); - }*/ - - System.out.println("\nAfter removing sheet1..."); - - nameCell = workbook.getName("name1"); - System.out.println("name1: " + nameCell.getRefersToFormula()); - - nameCell = workbook.getName("name2"); - System.out.println("name2: " + nameCell.getRefersToFormula()); - - nameCell = workbook.getName("name3"); - System.out.println("name3: " + nameCell.getRefersToFormula()); - - workbook.close(); + wb2.close(); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 2ca742a4a..d153df2ea 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -35,9 +35,6 @@ import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.util.CellRangeAddress; import org.junit.Test; -/** - * @author Yegor Kozlov - */ public abstract class BaseTestWorkbook { private final ITestDataProvider _testDataProvider; @@ -516,26 +513,29 @@ public abstract class BaseTestWorkbook { /** - * Test is kept to ensure stub for deprecated business method passes test. - * - * @Deprecated remove this test when - * {@link Workbook#setRepeatingRowsAndColumns(int, int, int, int, int)} - * is removed + * Test to validate that replacement for removed setRepeatingRowsAnsColumns() methods + * is still working correctly */ - @Deprecated @Test public void setRepeatingRowsAnsColumns() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); + + CellRangeAddress cra = new CellRangeAddress(0, 3, 0, 0); + String expRows = "1:4", expCols = "A:A"; + + Sheet sheet1 = wb.createSheet(); - wb.setRepeatingRowsAndColumns(wb.getSheetIndex(sheet1), 0, 0, 0, 3); - assertEquals("1:4", sheet1.getRepeatingRows().formatAsString()); - assertEquals("A:A", sheet1.getRepeatingColumns().formatAsString()); + sheet1.setRepeatingRows(cra); + sheet1.setRepeatingColumns(cra); + assertEquals(expRows, sheet1.getRepeatingRows().formatAsString()); + assertEquals(expCols, sheet1.getRepeatingColumns().formatAsString()); //must handle sheets with quotas, see Bugzilla #47294 Sheet sheet2 = wb.createSheet("My' Sheet"); - wb.setRepeatingRowsAndColumns(wb.getSheetIndex(sheet2), 0, 0, 0, 3); - assertEquals("1:4", sheet2.getRepeatingRows().formatAsString()); - assertEquals("A:A", sheet1.getRepeatingColumns().formatAsString()); + sheet2.setRepeatingRows(cra); + sheet2.setRepeatingColumns(cra); + assertEquals(expRows, sheet2.getRepeatingRows().formatAsString()); + assertEquals(expCols, sheet2.getRepeatingColumns().formatAsString()); wb.close(); }