From 86ac5492068ec286d884d0a1b880e0b3880860a6 Mon Sep 17 00:00:00 2001 From: Jarno Elovirta Date: Thu, 9 May 2024 09:58:05 +0300 Subject: [PATCH] Refactor failure test Signed-off-by: Jarno Elovirta --- .../dita/markdown/MDitaReaderCoreTest.java | 18 ++++++++++++------ .../dita/markdown/MDitaReaderExtendedTest.java | 18 ++++++++++++------ .../dita/markdown/MarkdownReaderTest.java | 16 ++++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/elovirta/dita/markdown/MDitaReaderCoreTest.java b/src/test/java/com/elovirta/dita/markdown/MDitaReaderCoreTest.java index f713af5..10cabd1 100644 --- a/src/test/java/com/elovirta/dita/markdown/MDitaReaderCoreTest.java +++ b/src/test/java/com/elovirta/dita/markdown/MDitaReaderCoreTest.java @@ -3,9 +3,11 @@ import static org.junit.jupiter.api.Assertions.*; import com.elovirta.dita.utils.AbstractReaderTest; +import java.io.InputStream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.opentest4j.AssertionFailedError; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class MDitaReaderCoreTest extends AbstractReaderTest { @@ -81,11 +83,15 @@ public void test_unsupported(String file) { @ParameterizedTest @ValueSource(strings = { "header.md", "invalid_header.md", "invalid_header_third.md" }) public void test_fail(String file) { - try { - run(file); - fail(); - } catch (Exception e) { - assertEquals(SAXException.class, e.getCause().getClass()); - } + assertThrows( + SAXException.class, + () -> { + final String input = "/" + getSrc() + file; + try (final InputStream in = getClass().getResourceAsStream(input)) { + final InputSource i = new InputSource(in); + reader.parse(i); + } + } + ); } } diff --git a/src/test/java/com/elovirta/dita/markdown/MDitaReaderExtendedTest.java b/src/test/java/com/elovirta/dita/markdown/MDitaReaderExtendedTest.java index 7f8124e..be6ff7a 100644 --- a/src/test/java/com/elovirta/dita/markdown/MDitaReaderExtendedTest.java +++ b/src/test/java/com/elovirta/dita/markdown/MDitaReaderExtendedTest.java @@ -4,10 +4,12 @@ import com.elovirta.dita.utils.AbstractReaderTest; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class MDitaReaderExtendedTest extends AbstractReaderTest { @@ -80,12 +82,16 @@ public void test(String file) throws Exception { @ParameterizedTest @ValueSource(strings = { "header.md", "invalid_header.md", "invalid_header_third.md" }) public void test_fail(String file) { - try { - run(file); - fail(); - } catch (Exception e) { - assertEquals(SAXException.class, e.getCause().getClass()); - } + assertThrows( + SAXException.class, + () -> { + final String input = "/" + getSrc() + file; + try (final InputStream in = getClass().getResourceAsStream(input)) { + final InputSource i = new InputSource(in); + reader.parse(i); + } + } + ); } @Test diff --git a/src/test/java/com/elovirta/dita/markdown/MarkdownReaderTest.java b/src/test/java/com/elovirta/dita/markdown/MarkdownReaderTest.java index 3b8e3c3..9a5d6e3 100644 --- a/src/test/java/com/elovirta/dita/markdown/MarkdownReaderTest.java +++ b/src/test/java/com/elovirta/dita/markdown/MarkdownReaderTest.java @@ -187,12 +187,16 @@ public void test_schemaParseFailure_withoutErrorHandler() throws Exception { @ParameterizedTest @ValueSource(strings = { "invalid_header.md" }) public void test_fail(String file) { - try { - run(file); - fail(); - } catch (Exception e) { - assertEquals(SAXException.class, e.getCause().getClass()); - } + assertThrows( + SAXException.class, + () -> { + final String input = "/" + getSrc() + file; + try (final InputStream in = getClass().getResourceAsStream(input)) { + final InputSource i = new InputSource(in); + reader.parse(i); + } + } + ); } @ParameterizedTest