add unit test for HSSFWorkbook.getSelectedTabs

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1748899 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-18 02:37:24 +00:00
parent acac716a64
commit d8d821b1f2
2 changed files with 72 additions and 33 deletions

View File

@ -34,11 +34,13 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
@ -542,27 +544,40 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
/**
* Selects multiple sheets as a group. This is distinct from
* the 'active' sheet (which is the sheet with focus).
* Unselects sheets that are not in <code>indexes</code>.
*
* @param indexes
*/
public void setSelectedTabs(int[] indexes) {
Collection<Integer> list = new ArrayList<Integer>(indexes.length);
for (int index : indexes) {
list.add(index);
}
setSelectedTabs(list);
}
/**
* Selects multiple sheets as a group. This is distinct from
* the 'active' sheet (which is the sheet with focus).
* Unselects sheets that are not in <code>indexes</code>.
*
* @param indexes
*/
public void setSelectedTabs(Collection<Integer> indexes) {
for (int index : indexes) {
validateSheetIndex(index);
}
// ignore duplicates
Set<Integer> set = new HashSet<Integer>(indexes);
int nSheets = _sheets.size();
for (int i=0; i<nSheets; i++) {
boolean bSelect = false;
for (int index : indexes) {
if (index == i) {
bSelect = true;
break;
}
}
getSheetAt(i).setSelected(bSelect);
boolean bSelect = set.contains(i);
getSheetAt(i).setSelected(bSelect);
}
workbook.getWindowOne().setNumSelectedTabs((short)indexes.length);
// this is true only if all values in set were valid sheet indexes (between 0 and nSheets-1, inclusive)
short nSelected = (short) set.size();
workbook.getWindowOne().setNumSelectedTabs(nSelected);
}
/**

View File

@ -33,6 +33,10 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.poi.POIDataSamples;
@ -210,50 +214,70 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.close();
}
private static List<Integer> arrayToList(int[] array) {
List<Integer> list = new ArrayList<Integer>(array.length);
for ( Integer element : array ) {
list.add(element);
}
return list;
}
private static void assertCollectionsEquals(Collection<Integer> expected, Collection<Integer> actual) {
assertEquals("size", expected.size(), actual.size());
for (int e : expected) {
assertTrue(actual.contains(e));
}
for (int a : actual) {
assertTrue(expected.contains(a));
}
}
@Test
public void selectMultiple() throws IOException {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet0 = wb.createSheet("Sheet0");
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});
List<Integer> selected = arrayToList(new int[] { 0, 2, 3 });
wb.setSelectedTabs(selected);
assertCollectionsEquals(selected, wb.getSelectedTabs());
assertEquals(true, sheet0.isSelected());
assertEquals(false, sheet1.isSelected());
assertEquals(true, sheet2.isSelected());
assertEquals(true, sheet3.isSelected());
assertEquals(false, sheet4.isSelected());
assertEquals(false, sheet5.isSelected());
selected = arrayToList(new int[] { 1, 3, 5 });
wb.setSelectedTabs(selected);
// previous selection should be cleared
assertCollectionsEquals(selected, wb.getSelectedTabs());
assertEquals(false, sheet0.isSelected());
assertEquals(true, sheet1.isSelected());
assertEquals(false, sheet2.isSelected());
assertEquals(true, sheet3.isSelected());
assertEquals(true, sheet4.isSelected());
assertEquals(false, sheet5.isSelected());
assertEquals(false, sheet6.isSelected());
assertEquals(false, sheet4.isSelected());
assertEquals(true, sheet5.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(true, sheet0.isActive());
assertEquals(false, sheet2.isActive());
assertEquals(true, sheet1.isActive());
assertEquals(false, sheet3.isActive());
wb.setActiveSheet(2);
assertEquals(false, sheet1.isActive());
assertEquals(true, sheet3.isActive());
assertEquals(false, sheet0.isActive());
assertEquals(true, sheet2.isActive());
/*{ // helpful if viewing this workbook in excel:
sheet0.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet0"));
sheet1.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet1"));
sheet2.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet2"));
sheet3.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet3"));
sheet4.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet4"));
try {
File fOut = TempFile.createTempFile("sheetMultiSelect", ".xls");