diff options
Diffstat (limited to 'src/ooxml/testcases')
6 files changed, 158 insertions, 270 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java b/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java index 3e570e2b51..1a4940c051 100644 --- a/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java +++ b/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -42,14 +43,11 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.poifs.filesystem.FileMagic; -import org.apache.poi.poifs.filesystem.NotOLE2FileException; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFExcelExtractor; import org.apache.xmlbeans.XmlException; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; /** * Test that the extractor factory plays nicely @@ -129,9 +127,6 @@ public class TestExtractorFactory { R apply(T t) throws IOException, OpenXML4JException, XmlException; } - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testFile() throws Exception { for (int i = 0; i < TEST_SET.length; i += 4) { @@ -142,11 +137,12 @@ public class TestExtractorFactory { } @Test - public void testFileInvalid() throws Exception { - thrown.expectMessage("Can't create extractor - unsupported file type: UNKNOWN"); - thrown.expect(IOException.class); - // Text - ExtractorFactory.createExtractor(txt); + public void testFileInvalid() { + IOException ex = assertThrows( + IOException.class, + () -> ExtractorFactory.createExtractor(txt) + ); + assertEquals("Can't create extractor - unsupported file type: UNKNOWN", ex.getMessage()); } @Test @@ -155,10 +151,11 @@ public class TestExtractorFactory { } @Test - public void testInputStreamInvalid() throws Exception { - thrown.expectMessage("Can't create extractor - unsupported file type: UNKNOWN"); - thrown.expect(IOException.class); - testInvalid(ExtractorFactory::createExtractor); + public void testInputStreamInvalid() throws IOException { + try (FileInputStream fis = new FileInputStream(txt)) { + IOException ex = assertThrows(IOException.class, () -> ExtractorFactory.createExtractor(fis)); + assertTrue(ex.getMessage().contains(FileMagic.UNKNOWN.name())); + } } @Test @@ -167,10 +164,12 @@ public class TestExtractorFactory { } @Test - public void testPOIFSInvalid() throws Exception { - thrown.expectMessage("Invalid header signature; read 0x3D20726F68747541, expected 0xE11AB1A1E011CFD0"); - thrown.expect(NotOLE2FileException.class); - testInvalid((f) -> ExtractorFactory.createExtractor(new POIFSFileSystem(f))); + public void testPOIFSInvalid() { + IOException ex = assertThrows( + IOException.class, + () -> ExtractorFactory.createExtractor(new POIFSFileSystem(txt)) + ); + assertTrue(ex.getMessage().contains("Invalid header signature; read 0x3D20726F68747541, expected 0xE11AB1A1E011CFD0")); } private void testStream(final FunctionEx<FileInputStream, POITextExtractor> poifsIS, final boolean loadOOXML) @@ -199,17 +198,6 @@ public class TestExtractorFactory { } } - private void testInvalid(FunctionEx<FileInputStream, POITextExtractor> poifs) throws IOException, OpenXML4JException, XmlException { - // Text - try (FileInputStream fis = new FileInputStream(txt); - POITextExtractor ignored = poifs.apply(fis)) { - fail("extracting from invalid package"); - } catch (IllegalArgumentException e) { - assertTrue("Had: " + e, e.getMessage().contains(FileMagic.UNKNOWN.name())); - throw e; - } - } - @Test public void testPackage() throws Exception { for (int i = 0; i < TEST_SET.length; i += 4) { @@ -273,6 +261,7 @@ public class TestExtractorFactory { } try (POITextExtractor extractor = xmlFactory.create(OPCPackage.open(xlsx.toString(), PackageAccess.READ))) { + assertNotNull(extractor); assertTrue(extractor.getText().length() > 200); } } finally { @@ -296,6 +285,7 @@ public class TestExtractorFactory { assertTrue(extractor instanceof XSSFExcelExtractor); } try (POITextExtractor extractor = xmlFactory.create(OPCPackage.open(xlsx.toString()))) { + assertNotNull(extractor); assertTrue(extractor.getText().length() > 200); } } diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java index 3858bc688d..4995fcb2ef 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java @@ -19,6 +19,7 @@ package org.apache.poi.openxml4j.opc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -29,9 +30,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.internal.ContentType; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; /** * Tests for content type (ContentType class). @@ -40,9 +39,6 @@ public final class TestContentType { private static final String FEATURE_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; - @Rule - public ExpectedException exception = ExpectedException.none(); - /** * Check rule M1.13: Package implementers shall only create and only * recognize parts with a content type; format designers shall specify a @@ -158,12 +154,13 @@ public final class TestContentType { */ @Test public void testFileWithContentTypeEntities() throws Exception { - if (!isOldXercesActive()) { - exception.expect(InvalidFormatException.class); + try (InputStream is = OpenXML4JTestDataSamples.openSampleStream("ContentTypeHasEntities.ooxml")) { + if (isOldXercesActive()) { + OPCPackage.open(is); + } else { + assertThrows(InvalidFormatException.class, () -> OPCPackage.open(is)); + } } - - InputStream is = OpenXML4JTestDataSamples.openSampleStream("ContentTypeHasEntities.ooxml"); - OPCPackage.open(is); } /** diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index d8fc1b7b84..5f95abd1ca 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -22,11 +22,11 @@ import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFile; import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFileName; import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.openSampleStream; import static org.apache.poi.openxml4j.opc.PackagingURIHelper.createPartName; -import static org.hamcrest.core.StringContains.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -50,6 +50,7 @@ import java.util.List; import java.util.TreeMap; import java.util.function.BiConsumer; import java.util.regex.Pattern; +import java.util.stream.Stream; import com.google.common.hash.Hashing; import com.google.common.io.Files; @@ -88,13 +89,9 @@ import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFRelation; import org.apache.xmlbeans.XmlException; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; -import org.hamcrest.core.AllOf; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -107,9 +104,6 @@ public final class TestPackage { private static final String CONTENT_EXT_PROPS = "application/vnd.openxmlformats-officedocument.extended-properties+xml"; private static final POIDataSamples xlsSamples = POIDataSamples.getSpreadSheetInstance(); - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - /** * Test that just opening and closing the file doesn't alter the document. */ @@ -645,115 +639,46 @@ public final class TestPackage { } } - - @Test - public void NonOOXML_OLE2Stream() throws Exception { - expectedEx.expect(OLE2NotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be in the OLE2 Format"), - containsString("You are calling the part of POI that deals with OOXML") - )); - try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.xls"); - OPCPackage p = OPCPackage.open(stream)) { - assertNotNull(p); - fail("Shouldn't be able to open OLE2"); - } - } - @Test - public void NonOOXML_OLE2File() throws Exception { - expectedEx.expect(OLE2NotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be in the OLE2 Format"), - containsString("You are calling the part of POI that deals with OOXML") - )); - try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.xls"))) { - assertNotNull(p); - fail("Shouldn't be able to open OLE2"); - } - } - - @Test - public void NonOOXML_RawXmlStream() throws Exception { - expectedEx.expect(NotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be a raw XML file"), - containsString("Formats such as Office 2003 XML") - )); - try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.xml"); - OPCPackage p = OPCPackage.open(stream)) { - assertNotNull(p); - fail("Shouldn't be able to open XML"); - } - } - - @Test - public void NonOOXML_RawXmlFile() throws Exception { - expectedEx.expect(NotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be a raw XML file"), - containsString("Formats such as Office 2003 XML") - )); - try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.xml"))) { - assertNotNull(p); - fail("Shouldn't be able to open XML"); - } - } - - @Test - public void NonOOXML_ODFStream() throws Exception { - expectedEx.expect(ODFNotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be in ODF"), - containsString("Formats like these (eg ODS") - )); - try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.ods"); - OPCPackage p = OPCPackage.open(stream)) { - assertNotNull(p); - fail("Shouldn't be able to open ODS"); - } - } - - @Test - public void NonOOXML_ODFFile() throws Exception { - expectedEx.expect(ODFNotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("The supplied data appears to be in ODF"), - containsString("Formats like these (eg ODS") - )); - try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.ods"))) { - assertNotNull(p); - fail("Shouldn't be able to open ODS"); - } + public void NonOOXML_File() throws Exception { + handleNonOOXML( + "SampleSS.xls", OLE2NotOfficeXmlFileException.class, + "The supplied data appears to be in the OLE2 Format", + "You are calling the part of POI that deals with OOXML" + ); + + handleNonOOXML( + "SampleSS.xml", NotOfficeXmlFileException.class, + "The supplied data appears to be a raw XML file", + "Formats such as Office 2003 XML" + ); + + handleNonOOXML( + "SampleSS.ods", ODFNotOfficeXmlFileException.class, + "The supplied data appears to be in ODF", + "Formats like these (eg ODS" + ); + + handleNonOOXML( + "SampleSS.txt", NotOfficeXmlFileException.class, + "No valid entries or contents found", + "not a valid OOXML" + ); } - @Test - public void NonOOXML_TextStream() throws Exception { - expectedEx.expect(NotOfficeXmlFileException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("No valid entries or contents found"), - containsString("not a valid OOXML") - )); - try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.txt"); - OPCPackage p = OPCPackage.open(stream)) { - assertNotNull(p); - fail("Shouldn't be able to open Plain Text"); + private void handleNonOOXML(String file, Class<? extends UnsupportedFileFormatException> exception, String... messageParts) throws IOException { + try (InputStream stream = xlsSamples.openResourceAsStream(file)) { + ThrowingRunnable[] trs = { + () -> OPCPackage.open(stream), + () -> OPCPackage.open(xlsSamples.getFile(file)) + }; + for (ThrowingRunnable tr : trs) { + Exception ex = assertThrows("Shouldn't be able to open "+file, exception, tr); + Stream.of(messageParts).forEach(mp -> assertTrue(ex.getMessage().contains(mp))); + } } } - @Test - public void NonOOXML_TextFile() throws Exception { - // Unhelpful low-level error, sorry - expectedEx.expect(UnsupportedFileFormatException.class); - expectedEx.expectMessage(AllOf.allOf( - containsString("No valid entries or contents found"), - containsString("not a valid OOXML") - )); - try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.txt"))) { - assertNotNull(p); - fail("Shouldn't be able to open Plain Text"); - } - } /** * Zip bomb handling test @@ -805,29 +730,32 @@ public final class TestPackage { } } - expectedEx.expect(IOException.class); - expectedEx.expectMessage("Zip bomb detected!"); - - try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bos.toByteArray()))) { - wb.getSheetAt(0); - } + IOException ex = assertThrows( + IOException.class, + () -> WorkbookFactory.create(new ByteArrayInputStream(bos.toByteArray())) + ); + assertTrue(ex.getMessage().contains("Zip bomb detected!")); } @Test - public void testZipEntityExpansionTerminates() throws IOException, OpenXML4JException, XmlException { - expectedEx.expect(IllegalStateException.class); - expectedEx.expectMessage("The text would exceed the max allowed overall size of extracted text."); - openXmlBombFile("poc-shared-strings.xlsx"); + public void testZipEntityExpansionTerminates() { + IllegalStateException ex = assertThrows( + IllegalStateException.class, + () -> openXmlBombFile("poc-shared-strings.xlsx") + ); + assertTrue(ex.getMessage().contains("The text would exceed the max allowed overall size of extracted text.")); } @Test - public void testZipEntityExpansionSharedStringTableEvents() throws IOException, OpenXML4JException, XmlException { + public void testZipEntityExpansionSharedStringTableEvents() { boolean before = ExtractorFactory.getThreadPrefersEventExtractors(); ExtractorFactory.setThreadPrefersEventExtractors(true); try { - expectedEx.expect(IllegalStateException.class); - expectedEx.expectMessage("The text would exceed the max allowed overall size of extracted text."); - openXmlBombFile("poc-shared-strings.xlsx"); + IllegalStateException ex = assertThrows( + IllegalStateException.class, + () -> openXmlBombFile("poc-shared-strings.xlsx") + ); + assertTrue(ex.getMessage().contains("The text would exceed the max allowed overall size of extracted text.")); } finally { ExtractorFactory.setThreadPrefersEventExtractors(before); } @@ -835,19 +763,33 @@ public final class TestPackage { @Test - public void testZipEntityExpansionExceedsMemory() throws IOException, OpenXML4JException, XmlException { - expectedEx.expect(IOException.class); - expectedEx.expectMessage("unable to parse shared strings table"); - expectedEx.expectCause(getCauseMatcher(SAXParseException.class, "The parser has encountered more than")); - openXmlBombFile("poc-xmlbomb.xlsx"); + public void testZipEntityExpansionExceedsMemory() { + IOException ex = assertThrows( + IOException.class, + () -> openXmlBombFile("poc-xmlbomb.xlsx") + ); + assertTrue(ex.getMessage().contains("unable to parse shared strings table")); + assertTrue(matchSAXEx(ex)); } @Test - public void testZipEntityExpansionExceedsMemory2() throws IOException, OpenXML4JException, XmlException { - expectedEx.expect(IOException.class); - expectedEx.expectMessage("unable to parse shared strings table"); - expectedEx.expectCause(getCauseMatcher(SAXParseException.class, "The parser has encountered more than")); - openXmlBombFile("poc-xmlbomb-empty.xlsx"); + public void testZipEntityExpansionExceedsMemory2() { + IOException ex = assertThrows( + IOException.class, + () -> openXmlBombFile("poc-xmlbomb-empty.xlsx") + ); + assertTrue(ex.getMessage().contains("unable to parse shared strings table")); + assertTrue(matchSAXEx(ex)); + } + + private static boolean matchSAXEx(Exception root) { + for (Throwable t = root; t != null; t = t.getCause()) { + if (t.getClass().isAssignableFrom(SAXParseException.class) && + t.getMessage().contains("The parser has encountered more than")) { + return true; + } + } + return false; } private void openXmlBombFile(String file) throws IOException, OpenXML4JException, XmlException { @@ -873,24 +815,28 @@ public final class TestPackage { } @Test - public void zipBombCheckSizesRatioTooSmall() throws IOException, EncryptedDocumentException { - expectedEx.expect(POIXMLException.class); - expectedEx.expectMessage("You can adjust this limit via ZipSecureFile.setMinInflateRatio()"); - getZipStatsAndConsume((max_size, min_ratio) -> { - // check ratio out of bounds - ZipSecureFile.setMinInflateRatio(min_ratio+0.002); - }); + public void zipBombCheckSizesRatioTooSmall() { + POIXMLException ex = assertThrows( + POIXMLException.class, + () -> getZipStatsAndConsume((max_size, min_ratio) -> { + // check ratio out of bounds + ZipSecureFile.setMinInflateRatio(min_ratio+0.002); + }) + ); + assertTrue(ex.getMessage().contains("You can adjust this limit via ZipSecureFile.setMinInflateRatio()")); } @Test public void zipBombCheckSizesSizeTooBig() throws IOException, EncryptedDocumentException { - expectedEx.expect(POIXMLException.class); - expectedEx.expectMessage("You can adjust this limit via ZipSecureFile.setMaxEntrySize()"); - getZipStatsAndConsume((max_size, min_ratio) -> { - // check max entry size ouf of bounds - ZipSecureFile.setMinInflateRatio(min_ratio-0.002); - ZipSecureFile.setMaxEntrySize(max_size-200); - }); + POIXMLException ex = assertThrows( + POIXMLException.class, + () -> getZipStatsAndConsume((max_size, min_ratio) -> { + // check max entry size ouf of bounds + ZipSecureFile.setMinInflateRatio(min_ratio-0.002); + ZipSecureFile.setMaxEntrySize(max_size-200); + }) + ); + assertTrue(ex.getMessage().contains("You can adjust this limit via ZipSecureFile.setMaxEntrySize()")); } private void getZipStatsAndConsume(BiConsumer<Long,Double> ratioCon) throws IOException { @@ -1121,42 +1067,6 @@ public final class TestPackage { } } - @SuppressWarnings("SameParameterValue") - private static <T extends Throwable> AnyCauseMatcher<T> getCauseMatcher(Class<T> cause, String message) { - // junit is only using hamcrest-core, so instead of adding hamcrest-beans, we provide the throwable - // search with the basics... - // see https://stackoverflow.com/a/47703937/2066598 - return new AnyCauseMatcher<>(cause, message); - } - - private static class AnyCauseMatcher<T extends Throwable> extends TypeSafeMatcher<T> { - private final Class<T> expectedType; - private final String expectedMessage; - - AnyCauseMatcher(Class<T> expectedType, String expectedMessage) { - this.expectedType = expectedType; - this.expectedMessage = expectedMessage; - } - - @Override - protected boolean matchesSafely(final Throwable root) { - for (Throwable t = root; t != null; t = t.getCause()) { - if (t.getClass().isAssignableFrom(expectedType) && t.getMessage().contains(expectedMessage)) { - return true; - } - } - return false; - } - - @Override - public void describeTo(Description description) { - description.appendText("expects type ") - .appendValue(expectedType) - .appendText(" and a message ") - .appendValue(expectedMessage); - } - } - @SuppressWarnings("UnstableApiUsage") @Test public void testBug63029() throws Exception { diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java index a4e977c148..988024907a 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java @@ -145,9 +145,7 @@ import org.junit.AfterClass; import org.junit.Assume; import org.junit.BeforeClass; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.w3.x2000.x09.xmldsig.ReferenceType; import org.w3.x2000.x09.xmldsig.SignatureDocument; import org.w3c.dom.Document; @@ -160,9 +158,6 @@ public class TestSignatureInfo { private KeyPair keyPair; private X509Certificate x509; - @Rule - public ExpectedException thrown = ExpectedException.none(); - @AfterClass public static void removeUserLocale() { LocaleUtil.resetUserLocale(); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlideShowFactory.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlideShowFactory.java index b02995fcdd..b4fb08fef0 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlideShowFactory.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlideShowFactory.java @@ -17,6 +17,10 @@ package org.apache.poi.xslf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -33,30 +37,25 @@ import org.apache.poi.sl.usermodel.BaseTestSlideShowFactory; import org.apache.poi.util.IOUtils; import org.apache.poi.util.TempFile; import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.ExpectedException; public final class TestXSLFSlideShowFactory extends BaseTestSlideShowFactory { - private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); + private static final POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); private static final String filename = "SampleShow.pptx"; private static final String password = "opensesame"; private static final String removeExpectedExceptionMsg = "This functionality this unit test is trying to test is now passing. " + "The unit test needs to be updated by deleting the expected exception code. Status and close any related bugs."; - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test - public void testFactoryFromFile() throws Exception { + public void testFactoryFromFile() { // Remove thrown.* when bug 58779 is resolved // In the mean time, this function will modify SampleShow.pptx on disk. - thrown.expect(AssertionError.class); - // thrown.expectCause(Matcher<ArrayComparisonFailure>); - thrown.expectMessage("SampleShow.pptx sample file was modified as a result of closing the slideshow"); - thrown.reportMissingExceptionWithMessage("Bug 58779: " + removeExpectedExceptionMsg); - - testFactoryFromFile(filename); + AssertionError ex = assertThrows( + "Bug 58779: " + removeExpectedExceptionMsg, + AssertionError.class, + () -> testFactoryFromFile(filename) + ); + assertTrue(ex.getMessage().contains("SampleShow.pptx sample file was modified as a result of closing the slideshow")); } @Test @@ -65,13 +64,14 @@ public final class TestXSLFSlideShowFactory extends BaseTestSlideShowFactory { } @Test - public void testFactoryFromNative() throws Exception { + public void testFactoryFromNative() { // Remove thrown.* when unit test for XSLF SlideShowFactory.create(OPCPackage) is implemented - thrown.expect(UnsupportedOperationException.class); - thrown.expectMessage("Test not implemented"); - thrown.reportMissingExceptionWithMessage(removeExpectedExceptionMsg); - - testFactoryFromNative(filename); + UnsupportedOperationException ex = assertThrows( + removeExpectedExceptionMsg, + UnsupportedOperationException.class, + () -> testFactoryFromNative(filename) + ); + assertEquals("Test not implemented", ex.getMessage()); } @Test diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java index 15dcf326a0..8a3ffe433f 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.streaming; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import java.io.IOException; @@ -57,18 +58,16 @@ public final class TestSXSSFSheet extends BaseTestXSheet { */ @Override @Test - public void cloneSheet() throws IOException { - thrown.expect(RuntimeException.class); - thrown.expectMessage("Not Implemented"); - super.cloneSheet(); + public void cloneSheet() { + RuntimeException ex = assertThrows(RuntimeException.class, super::cloneSheet); + assertEquals("Not Implemented", ex.getMessage()); } @Override @Test - public void cloneSheetMultipleTimes() throws IOException { - thrown.expect(RuntimeException.class); - thrown.expectMessage("Not Implemented"); - super.cloneSheetMultipleTimes(); + public void cloneSheetMultipleTimes() { + RuntimeException ex = assertThrows(RuntimeException.class, super::cloneSheetMultipleTimes); + assertEquals("Not Implemented", ex.getMessage()); } /** @@ -76,10 +75,9 @@ public final class TestSXSSFSheet extends BaseTestXSheet { */ @Override @Test - public void shiftMerged() throws IOException { - thrown.expect(RuntimeException.class); - thrown.expectMessage("Not Implemented"); - super.shiftMerged(); + public void shiftMerged() { + RuntimeException ex = assertThrows(RuntimeException.class, super::shiftMerged); + assertEquals("Not Implemented", ex.getMessage()); } /** @@ -89,15 +87,14 @@ public final class TestSXSSFSheet extends BaseTestXSheet { */ @Override @Test - public void bug35084() throws IOException { - thrown.expect(RuntimeException.class); - thrown.expectMessage("Not Implemented"); - super.bug35084(); + public void bug35084() { + RuntimeException ex = assertThrows(RuntimeException.class, super::bug35084); + assertEquals("Not Implemented", ex.getMessage()); } @Override @Test - public void getCellComment() throws IOException { + public void getCellComment() { // TODO: reading cell comments via Sheet does not work currently as it tries // to access the underlying sheet for this, but comments are stored as // properties on Cells... @@ -119,9 +116,8 @@ public final class TestSXSSFSheet extends BaseTestXSheet { sheet.createRow(3); sheet.createRow(4); - thrown.expect(Throwable.class); - thrown.expectMessage("Attempting to write a row[1] in the range [0,1] that is already written to disk."); - sheet.createRow(1); + Throwable ex = assertThrows(Throwable.class, () -> sheet.createRow(1)); + assertEquals("Attempting to write a row[1] in the range [0,1] that is already written to disk.", ex.getMessage()); } } |