From 9c65dbae4a0ca1f6254cc65f9f00e11dbd3f18b1 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 16 May 2008 17:14:02 +0000 Subject: [PATCH] Should have been submitted with r656893 (for bug 44523) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@657136 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/TestHSSFWorkbook.java | 152 ++++++++++++++---- 1 file changed, 124 insertions(+), 28 deletions(-) diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 75ac6923a..e1afb453b 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -17,13 +17,18 @@ package org.apache.poi.hssf.usermodel; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.record.NameRecord; +import org.apache.poi.util.TempFile; /** - * + * */ public final class TestHSSFWorkbook extends TestCase { private static HSSFWorkbook openSample(String sampleFileName) { @@ -93,7 +98,7 @@ public final class TestHSSFWorkbook extends TestCase { b.setSheetName( 3, "name2" ); b.setSheetName( 3, "name2" ); b.setSheetName( 3, "name2" ); - + HSSFWorkbook c = new HSSFWorkbook( ); c.createSheet("Sheet1"); c.createSheet("Sheet2"); @@ -101,49 +106,50 @@ public final class TestHSSFWorkbook extends TestCase { c.createSheet("Sheet4"); } - + public void testWindowOneDefaults() { HSSFWorkbook b = new HSSFWorkbook( ); try { - assertEquals(b.getSelectedTab(), 0); - assertEquals(b.getDisplayedTab(), 0); + assertEquals(b.getActiveSheetIndex(), 0); + assertEquals(b.getFirstVisibleTab(), 0); } catch (NullPointerException npe) { fail("WindowOneRecord in Workbook is probably not initialized"); } } - + public void testSheetSelection() { HSSFWorkbook b = new HSSFWorkbook(); b.createSheet("Sheet One"); b.createSheet("Sheet Two"); - b.setSelectedTab((short) 1); - b.setDisplayedTab((short) 1); - assertEquals(b.getSelectedTab(), 1); - assertEquals(b.getDisplayedTab(), 1); + b.setActiveSheet(1); + b.setSelectedTab(1); + b.setFirstVisibleTab(1); + assertEquals(1, b.getActiveSheetIndex()); + assertEquals(1, b.getFirstVisibleTab()); } - + public void testSheetClone() { // First up, try a simple file HSSFWorkbook b = new HSSFWorkbook(); assertEquals(0, b.getNumberOfSheets()); b.createSheet("Sheet One"); b.createSheet("Sheet Two"); - + assertEquals(2, b.getNumberOfSheets()); b.cloneSheet(0); assertEquals(3, b.getNumberOfSheets()); - + // Now try a problem one with drawing records in it b = openSample("SheetWithDrawing.xls"); assertEquals(1, b.getNumberOfSheets()); b.cloneSheet(0); assertEquals(2, b.getNumberOfSheets()); } - + public void testReadWriteWithCharts() { HSSFWorkbook b; HSSFSheet s; - + // Single chart, two sheets b = openSample("44010-SingleChart.xls"); assertEquals(2, b.getNumberOfSheets()); @@ -151,51 +157,51 @@ public final class TestHSSFWorkbook extends TestCase { s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); - + // Has chart on 1st sheet?? // FIXME assertNotNull(b.getSheetAt(0).getDrawingPatriarch()); assertNull(b.getSheetAt(1).getDrawingPatriarch()); assertFalse(b.getSheetAt(0).getDrawingPatriarch().containsChart()); - - // We've now called getDrawingPatriarch() so + + // We've now called getDrawingPatriarch() so // everything will be all screwy // So, start again b = openSample("44010-SingleChart.xls"); - + b = writeRead(b); assertEquals(2, b.getNumberOfSheets()); s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); - + // Two charts, three sheets b = openSample("44010-TwoCharts.xls"); assertEquals(3, b.getNumberOfSheets()); - + s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); s = b.getSheetAt(2); assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); - + // Has chart on 1st sheet?? // FIXME assertNotNull(b.getSheetAt(0).getDrawingPatriarch()); assertNull(b.getSheetAt(1).getDrawingPatriarch()); assertNull(b.getSheetAt(2).getDrawingPatriarch()); assertFalse(b.getSheetAt(0).getDrawingPatriarch().containsChart()); - - // We've now called getDrawingPatriarch() so + + // We've now called getDrawingPatriarch() so // everything will be all screwy // So, start again b = openSample("44010-TwoCharts.xls"); - + b = writeRead(b); assertEquals(3, b.getNumberOfSheets()); - + s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); @@ -203,8 +209,98 @@ public final class TestHSSFWorkbook extends TestCase { assertEquals(0, s.getFirstRowNum()); assertEquals(8, s.getLastRowNum()); } - + private static HSSFWorkbook writeRead(HSSFWorkbook b) { return HSSFTestDataSamples.writeOutAndReadBack(b); } -} \ No newline at end of file + + + public void testSelectedSheet_bug44523() { + HSSFWorkbook wb=new HSSFWorkbook(); + HSSFSheet sheet1 = wb.createSheet("Sheet1"); + HSSFSheet sheet2 = wb.createSheet("Sheet2"); + HSSFSheet sheet3 = wb.createSheet("Sheet3"); + HSSFSheet sheet4 = wb.createSheet("Sheet4"); + + confirmActiveSelected(sheet1, true); + confirmActiveSelected(sheet2, false); + confirmActiveSelected(sheet3, false); + confirmActiveSelected(sheet4, false); + + wb.setSelectedTab(1); + + // Demonstrate bug 44525: + // Well... not quite, since isActive + isSelected were also added in the same bug fix + if (sheet1.isSelected()) { + throw new AssertionFailedError("Identified bug 44525 a"); + } + wb.setActiveSheet(1); + if (sheet1.isActive()) { + throw new AssertionFailedError("Identified bug 44525 b"); + } + + confirmActiveSelected(sheet1, false); + confirmActiveSelected(sheet2, true); + confirmActiveSelected(sheet3, false); + confirmActiveSelected(sheet4, false); + } + + public void testSelectMultiple() { + HSSFWorkbook wb=new HSSFWorkbook(); + HSSFSheet sheet1 = wb.createSheet("Sheet1"); + HSSFSheet sheet2 = wb.createSheet("Sheet2"); + HSSFSheet sheet3 = wb.createSheet("Sheet3"); + HSSFSheet sheet4 = wb.createSheet("Sheet4"); + HSSFSheet sheet5 = wb.createSheet("Sheet5"); + HSSFSheet sheet6 = wb.createSheet("Sheet6"); + + wb.setSelectedTabs(new int[] { 0, 2, 3}); + + assertEquals(true, sheet1.isSelected()); + assertEquals(false, sheet2.isSelected()); + assertEquals(true, sheet3.isSelected()); + assertEquals(true, sheet4.isSelected()); + assertEquals(false, sheet5.isSelected()); + assertEquals(false, sheet6.isSelected()); + + wb.setSelectedTabs(new int[] { 1, 3, 5}); + + assertEquals(false, sheet1.isSelected()); + assertEquals(true, sheet2.isSelected()); + assertEquals(false, sheet3.isSelected()); + assertEquals(true, sheet4.isSelected()); + assertEquals(false, sheet5.isSelected()); + assertEquals(true, sheet6.isSelected()); + + assertEquals(true, sheet1.isActive()); + assertEquals(false, sheet2.isActive()); + + + assertEquals(true, sheet1.isActive()); + assertEquals(false, sheet3.isActive()); + wb.setActiveSheet(2); + assertEquals(false, sheet1.isActive()); + assertEquals(true, sheet3.isActive()); + + if (false) { // helpful if viewing this workbook in excel: + sheet1.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Sheet1")); + sheet2.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Sheet2")); + sheet3.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Sheet3")); + sheet4.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Sheet4")); + + try { + File fOut = TempFile.createTempFile("sheetMultiSelect", ".xls"); + FileOutputStream os = new FileOutputStream(fOut); + wb.write(os); + os.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private static void confirmActiveSelected(HSSFSheet sheet, boolean expected) { + assertEquals(expected, sheet.isActive()); + assertEquals(expected, sheet.isSelected()); + } +}