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
This commit is contained in:
Josh Micich 2008-05-16 17:14:02 +00:00
parent 2619dbf569
commit a9842ea17c
1 changed files with 124 additions and 28 deletions

View File

@ -17,11 +17,16 @@
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;
/**
*
*/
@ -105,8 +110,8 @@ public final class TestHSSFWorkbook extends TestCase {
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");
}
@ -116,10 +121,11 @@ public final class TestHSSFWorkbook extends TestCase {
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() {
@ -207,4 +213,94 @@ public final class TestHSSFWorkbook extends TestCase {
private static HSSFWorkbook writeRead(HSSFWorkbook b) {
return HSSFTestDataSamples.writeOutAndReadBack(b);
}
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());
}
}