From a15f94d8284971665f1a3443be9a144e9afcda00 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 9 Jul 2008 14:58:06 +0000 Subject: [PATCH] Fix for bug 45367 - fixed boundary case when row zero is the last row removed from the sheet git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@675218 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/hssf/usermodel/HSSFSheet.java | 15 ++++++++------- .../apache/poi/hssf/usermodel/TestHSSFSheet.java | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 2507fafbea..6756dcc360 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45367 - Fixed bug when last row removed from sheet is row zero 45348 - Tweaks to RVA formula logic 45354 - Fixed recognition of named ranges within formulas 45338 - Fix HSSFWorkbook to give you the same HSSFFont every time, and then fix it to find newly added fonts diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 013b3b3bd1..bbf536e73c 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45367 - Fixed bug when last row removed from sheet is row zero 45348 - Tweaks to RVA formula logic 45354 - Fixed recognition of named ranges within formulas 45338 - Fix HSSFWorkbook to give you the same HSSFFont every time, and then fix it to find newly added fonts diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index cf4cff86d7..ea2b8132d0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -262,18 +262,19 @@ public final class HSSFSheet { /** * used internally to refresh the "last row" when the last row is removed. */ - - private int findLastRow(int lastrow) - { + private int findLastRow(int lastrow) { + if (lastrow < 1) { + return -1; + } int rownum = lastrow - 1; HSSFRow r = getRow(rownum); - while (r == null && rownum > 0) - { + while (r == null && rownum > 0) { r = getRow(--rownum); } - if (r == null) - return -1; + if (r == null) { + return -1; + } return rownum; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 4a5f33c4b9..7fa84b853a 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -169,6 +169,20 @@ public final class TestHSSFSheet extends TestCase { sheet.removeRow(row); } + public void testRemoveZeroRow() { + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet("Sheet1"); + HSSFRow row = sheet.createRow(0); + try { + sheet.removeRow(row); + } catch (IllegalArgumentException e) { + if (e.getMessage().equals("Invalid row number (-1) outside allowable range (0..65535)")) { + throw new AssertionFailedError("Identified bug 45367"); + } + throw e; + } + } + public void testCloneSheet() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Test Clone"); -- 2.39.5