Fix from Florian Hopf from bug #54564 - Fix error message text

for a workbook with no sheets when a sheet operation is performed

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1496516 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-06-25 15:19:56 +00:00
parent ed5588d9af
commit a7bfec9271
3 changed files with 12 additions and 2 deletions

View File

@ -429,8 +429,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
private void validateSheetIndex(int index) {
int lastSheetIx = _sheets.size() - 1;
if (index < 0 || index > lastSheetIx) {
String range = "(0.." + lastSheetIx + ")";
if (lastSheetIx == -1) {
range = "(no sheets)";
}
throw new IllegalArgumentException("Sheet index ("
+ index +") is out of range (0.." + lastSheetIx + ")");
+ index +") is out of range " + range);
}
}

View File

@ -1040,8 +1040,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
private void validateSheetIndex(int index) {
int lastSheetIx = sheets.size() - 1;
if (index < 0 || index > lastSheetIx) {
String range = "(0.." + lastSheetIx + ")";
if (lastSheetIx == -1) {
range = "(no sheets)";
}
throw new IllegalArgumentException("Sheet index ("
+ index +") is out of range (0.." + lastSheetIx + ")");
+ index +") is out of range " + range);
}
}

View File

@ -45,6 +45,8 @@ public abstract class BaseTestWorkbook extends TestCase {
fail("should have thrown exceptiuon due to invalid sheet index");
} catch (IllegalArgumentException e) {
// expected during successful test
// no negative index in the range message
assertFalse(e.getMessage().contains("-1"));
}
Sheet sheet0 = wb.createSheet();