diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 96eaef5719..ec6ab23745 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -37,7 +37,9 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -494,4 +496,36 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works } + + + public void testBug48495() { + try { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("48495.xlsx"); + + assertSheetOrder(wb, "Sheet1"); + + Sheet sheet = wb.getSheetAt(0); + sheet.shiftRows(2, sheet.getLastRowNum(), 1, true, false); + Row newRow = sheet.getRow(2); + if (newRow == null) newRow = sheet.createRow(2); + newRow.createCell(0).setCellValue(" Another Header"); + wb.cloneSheet(0); + + assertSheetOrder(wb, "Sheet1", "Sheet1 (2)"); + + // FileOutputStream fileOut = new FileOutputStream("/tmp/bug48495.xlsx"); +// try { +// wb.write(fileOut); +// } finally { +// fileOut.close(); +// } + + Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb); + assertNotNull(read); + assertSheetOrder(read, "Sheet1", "Sheet1 (2)"); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("Done"); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 6d49827bf6..1927727e42 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -965,17 +965,4 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { HSSFWorkbook read = HSSFTestDataSamples.writeOutAndReadBack(wb); assertSheetOrder(read, "Invoice", "Deferred", "Received", "Digest"); } - - private void assertSheetOrder(HSSFWorkbook wb, String... sheets) { - StringBuilder sheetNames = new StringBuilder(); - for(int i = 0;i < wb.getNumberOfSheets();i++) { - sheetNames.append(wb.getSheetAt(i).getSheetName()).append(","); - } - assertEquals("Had: " + sheetNames.toString(), - sheets.length, wb.getNumberOfSheets()); - for(int i = 0;i < wb.getNumberOfSheets();i++) { - assertEquals("Had: " + sheetNames.toString(), - sheets[i], wb.getSheetAt(i).getSheetName()); - } - } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index b2357f947e..ce96c3fe17 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -604,4 +604,17 @@ public abstract class BaseTestWorkbook extends TestCase { assertEquals(cellB.getStringCellValue(), evaluator.evaluate(cellA).getStringValue()); } } + + protected void assertSheetOrder(Workbook wb, String... sheets) { + StringBuilder sheetNames = new StringBuilder(); + for(int i = 0;i < wb.getNumberOfSheets();i++) { + sheetNames.append(wb.getSheetAt(i).getSheetName()).append(","); + } + assertEquals("Had: " + sheetNames.toString(), + sheets.length, wb.getNumberOfSheets()); + for(int i = 0;i < wb.getNumberOfSheets();i++) { + assertEquals("Had: " + sheetNames.toString(), + sheets[i], wb.getSheetAt(i).getSheetName()); + } + } } diff --git a/test-data/spreadsheet/48495.xlsx b/test-data/spreadsheet/48495.xlsx new file mode 100644 index 0000000000..73bc28b8e5 Binary files /dev/null and b/test-data/spreadsheet/48495.xlsx differ