diff options
author | Nick Burch <nick@apache.org> | 2015-09-17 11:10:11 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2015-09-17 11:10:11 +0000 |
commit | 64c5d60d33551ec0c9acaa2f0c3028ac78e3bc16 (patch) | |
tree | 167e46ab1731d6a8eeec97579601691d25cb22fb /src/testcases | |
parent | 37c45103431afc1748d419fe1c1b81fb4c467d2a (diff) | |
download | poi-64c5d60d33551ec0c9acaa2f0c3028ac78e3bc16.tar.gz poi-64c5d60d33551ec0c9acaa2f0c3028ac78e3bc16.zip |
Patch from Javen ONeal from bug #58245 - Make Workbook support iterating over Sheets
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1703573 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index d51f8e0f4c..4a9dd65c09 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -26,6 +26,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; +import java.util.ConcurrentModificationException; +import java.util.Iterator; import junit.framework.AssertionFailedError; @@ -43,6 +45,79 @@ public abstract class BaseTestWorkbook { protected BaseTestWorkbook(ITestDataProvider testDataProvider) { _testDataProvider = testDataProvider; } + + @Test + public void sheetIterator_forEach() { + final Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("Sheet0"); + wb.createSheet("Sheet1"); + wb.createSheet("Sheet2"); + int i = 0; + for (Sheet sh : wb) { + assertEquals("Sheet"+i, sh.getSheetName()); + i++; + } + } + + @Test + public void sheetIterator_sheetsReordered() { + final Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("Sheet0"); + wb.createSheet("Sheet1"); + wb.createSheet("Sheet2"); + + Iterator<Sheet> it = wb.sheetIterator(); + it.next(); + wb.setSheetOrder("Sheet2", 1); + + // Iterator order should be fixed when iterator is created + try { + assertEquals("Sheet1", it.next().getSheetName()); + fail("Expected ConcurrentModificationException: "+ + "should not be able to advance an iterator when the "+ + "underlying data has been reordered"); + } catch (final ConcurrentModificationException e) { + // expected + } + } + + @Test + public void sheetIterator_sheetRemoved() { + final Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("Sheet0"); + wb.createSheet("Sheet1"); + wb.createSheet("Sheet2"); + + Iterator<Sheet> it = wb.sheetIterator(); + wb.removeSheetAt(1); + + // Iterator order should be fixed when iterator is created + try { + it.next(); + fail("Expected ConcurrentModificationException: "+ + "should not be able to advance an iterator when the "+ + "underlying data has been reordered"); + } catch (final ConcurrentModificationException e) { + // expected + } + } + + @Test + public void sheetIterator_remove() { + final Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("Sheet0"); + + Iterator<Sheet> it = wb.sheetIterator(); + it.next(); //Sheet0 + try { + it.remove(); + fail("Expected UnsupportedOperationException: "+ + "should not be able to remove sheets from the sheet iterator"); + } catch (final UnsupportedOperationException e) { + // expected + } + } + @Test public void createSheet() { |