From a9ec0770f0d4226d5de4773e55045949227d922f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 19 Sep 2017 21:04:30 +0000 Subject: [PATCH] tidy up some test code git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808944 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/extractor/TestExtractorFactory.java | 17 +++---- .../usermodel/BaseTestXSSFPivotTable.java | 44 ++++--------------- .../org/apache/poi/hsmf/TestBasics.java | 13 +++--- .../poi/hssf/usermodel/TestHSSFCell.java | 10 ++--- .../poi/hssf/util/TestAreaReference.java | 6 +-- 5 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java b/src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java index dfae64107..fb378df0d 100644 --- a/src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java +++ b/src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java @@ -332,7 +332,7 @@ public class TestExtractorFactory { // Text try { ExtractorFactory.createExtractor(txt); - fail(); + fail("expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // Good } @@ -480,14 +480,9 @@ public class TestExtractorFactory { extractor.close(); // Text - try { - FileInputStream stream = new FileInputStream(txt); - try { - ExtractorFactory.createExtractor(stream); - fail(); - } finally { - IOUtils.closeQuietly(stream); - } + try (FileInputStream stream = new FileInputStream(txt)) { + ExtractorFactory.createExtractor(stream); + fail("expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // Good } @@ -568,7 +563,7 @@ public class TestExtractorFactory { // Text try { ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(txt))); - fail(); + fail("expected IllegalArgumentException"); } catch(IOException e) { // Good } @@ -650,7 +645,7 @@ public class TestExtractorFactory { // Text try { ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(txt))); - fail(); + fail("expected IllegalArgumentException"); } catch(IOException e) { // Good } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java index 21b7b01bc..c711c83e4 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java @@ -86,16 +86,9 @@ public abstract class BaseTestXSSFPivotTable { /** * Verify that it's not possible to create a row label outside of the referenced area. */ - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testAddRowLabelOutOfRangeThrowsException() { - int columnIndex = 5; - - try { - pivotTable.addRowLabel(columnIndex); - } catch(IndexOutOfBoundsException e) { - return; - } - fail(); + pivotTable.addRowLabel(5); } /** @@ -197,16 +190,9 @@ public abstract class BaseTestXSSFPivotTable { /** * Verify that it's not possible to create a column label outside of the referenced area. */ - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testAddColumnLabelOutOfRangeThrowsException() { - int columnIndex = 5; - - try { - pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex); - } catch(IndexOutOfBoundsException e) { - return; - } - fail(); + pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 5); } /** @@ -226,17 +212,9 @@ public abstract class BaseTestXSSFPivotTable { /** * Verify that it's not possible to create a data column outside of the referenced area. */ - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testAddDataColumnOutOfRangeThrowsException() { - int columnIndex = 5; - boolean isDataField = true; - - try { - pivotTable.addDataColumn(columnIndex, isDataField); - } catch(IndexOutOfBoundsException e) { - return; - } - fail(); + pivotTable.addDataColumn(5, true); } /** @@ -257,15 +235,9 @@ public abstract class BaseTestXSSFPivotTable { /** * Verify that it's not possible to create a new filter outside of the referenced area. */ - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testAddReportFilterOutOfRangeThrowsException() { - int columnIndex = 5; - try { - pivotTable.addReportFilter(columnIndex); - } catch(IndexOutOfBoundsException e) { - return; - } - fail(); + pivotTable.addReportFilter(5); } /** diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java index 207d3fdcb..860f1f5e6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java @@ -19,6 +19,7 @@ package org.apache.poi.hsmf; import static org.apache.poi.POITestCase.assertContains; import static org.apache.poi.POITestCase.assertStartsWith; +import static org.junit.Assert.fail; import java.io.IOException; @@ -99,13 +100,13 @@ public final class TestBasics extends TestCase { // Quick doesn't have them try { quick.getHeaders(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) {} // Attachments doesn't have them try { attachments.getHeaders(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) {} // Outlook30 has some @@ -157,7 +158,7 @@ public final class TestBasics extends TestCase { try { attachments.getHtmlBody(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) { // Good } @@ -170,7 +171,7 @@ public final class TestBasics extends TestCase { try { attachments.getHtmlBody(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) { // Good } @@ -185,13 +186,13 @@ public final class TestBasics extends TestCase { try { noRecipientAddress.getRecipientEmailAddress(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) { // Good } try { noRecipientAddress.getRecipientEmailAddressList(); - fail(); + fail("expected ChunkNotFoundException"); } catch(ChunkNotFoundException e) { // Good } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index a9cc347be..7fe5f0922 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -301,13 +301,13 @@ public final class TestHSSFCell extends BaseTestCell { styB.verifyBelongsToWorkbook(wbB); try { styA.verifyBelongsToWorkbook(wbB); - fail(); + fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected during successful test } try { styB.verifyBelongsToWorkbook(wbA); - fail(); + fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected during successful test } @@ -319,13 +319,13 @@ public final class TestHSSFCell extends BaseTestCell { cellB.setCellStyle(styB); try { cellA.setCellStyle(styB); - fail(); + fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected during successful test } try { cellB.setCellStyle(styA); - fail(); + fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected during successful test } @@ -352,7 +352,7 @@ public final class TestHSSFCell extends BaseTestCell { Record[] recs = RecordInspector.getRecords(sheet, 0); if (recs.length == 28 && recs[23] instanceof StringRecord) { wb.close(); - throw new AssertionFailedError("Identified bug - leftover StringRecord"); + fail("Identified bug - leftover StringRecord"); } confirmStringRecord(sheet, false); diff --git a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java index 2819289ea..c14ca44e4 100644 --- a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java +++ b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java @@ -126,19 +126,19 @@ public final class TestAreaReference extends TestCase { new AreaReference(ref2D, SpreadsheetVersion.EXCEL97); try { new AreaReference(refDCSimple, SpreadsheetVersion.EXCEL97); - fail(); + fail("expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // expected during successful test } try { new AreaReference(refDC2D, SpreadsheetVersion.EXCEL97); - fail(); + fail("expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // expected during successful test } try { new AreaReference(refDC3D, SpreadsheetVersion.EXCEL97); - fail(); + fail("expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // expected during successful test }