From 7067e87b8b8df8f786e53d29b6d5d7418cbde8fc Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 25 Jun 2013 15:19:56 +0000 Subject: [PATCH] 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 --- src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java | 6 +++++- .../java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java | 6 +++++- .../org/apache/poi/ss/usermodel/BaseTestWorkbook.java | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 4b2bbdb948..25363f00ed 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index ab23bbf36f..e6d33ebeb0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -1040,8 +1040,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable 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); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 15181245bc..b2357f947e 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -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(); -- 2.39.5