From 3e9037e27cb9eb8ddcf2960a862e68e6bcd49c3b Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Sat, 26 Oct 2024 18:37:31 +0200 Subject: [PATCH] Improve coverage of ParsingException. --- README.md | 30 ++++++++----------- .../hafner/util/SecureXmlParserFactory.java | 2 ++ .../util/SecureXmlParserFactoryTest.java | 17 +++++++++++ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c80f9c09..c98f0c39 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,12 @@ [![Warnings](https://raw.githubusercontent.com/uhafner/codingstyle/main/badges/style.svg)](https://github.com/uhafner/codingstyle/actions/workflows/quality-monitor.yml) [![Bugs](https://raw.githubusercontent.com/uhafner/codingstyle/main/badges/bugs.svg)](https://github.com/uhafner/codingstyle/actions/workflows/quality-monitor.yml) -Each Java project should follow a given coding style. I.e., all contributions to the source code should use the same -formatting rules, design principles, code patterns, idioms, etc. This coding style provides the set of rules that I -am using in my lectures about software development at Munich University of Applied Sciences. +Each Java project should follow a given coding style. +I.e., all contributions to the source code should use the same formatting rules, design principles, code patterns, idioms, etc. +This coding style provides the set of rules that I am using in my lectures about software development at Munich University of Applied Sciences. This project describes the coding style in detail (currently only available in German) and serves as a template project. -It provides all necessary resources for a Java project to enforce this coding style using the following -static analysis tools via Maven (and partly in IntelliJ): +It provides all necessary resources for a Java project to enforce this coding style using the following static analysis tools via Maven (and partly in IntelliJ): - [Checkstyle](https://checkstyle.org) - [PMD](https://pmd.github.io/) - [SpotBugs](https://spotbugs.github.io) @@ -20,17 +19,14 @@ static analysis tools via Maven (and partly in IntelliJ): ❗This project requires a JDK version of 17 or higher.❗ -Moreover, this project provides some sample classes that already use this style guide. These classes can be used -as such but are not required in this project. These classes also use some additional libraries that are included -using the Maven dependency mechanism. If the sample classes are deleted, then the dependencies can be safely -deleted, too. +Moreover, this project provides some sample classes that already use this style guide. +These classes can be used as such but are not required in this project. +These classes also use some additional libraries that are included using the Maven dependency mechanism. +If the sample classes are deleted, then the dependencies can be safely deleted, too. -This project and the associated static analysis tools are already running in continuous integration: an example -CI pipeline is active for GitHub Actions. For [Jenkins](https://jenkins.io/) a full CI pipeline has been -configured that includes stages to compile, test, run static code analysis, run code coverage analysis, -and run mutation coverage analysis, see section [Continuous Integration](doc/Continuous-Integration.md) for details. -Additionally, some development tools are configured in this GitHub project to evaluate the quality of pull requests, -see section [integration of external tools](doc/Externe-Tool-Integration.md). +This project and the associated static analysis tools are already running in continuous integration: an example CI pipeline is active for GitHub Actions. +For [Jenkins](https://jenkins.io/) a full CI pipeline has been configured that includes stages to compile, test, run static code analysis, run code coverage analysis, and run mutation coverage analysis, see section [Continuous Integration](doc/Continuous-Integration.md) for details. +Additionally, some development tools are configured in this GitHub project to evaluate the quality of pull requests, see section [integration of external tools](doc/Externe-Tool-Integration.md). Content of the style guide (only in German): - [Formatierung](doc/Formatierung.md) @@ -53,9 +49,9 @@ A lot of ideas in this style are based on the following path-breaking books abou - [6] "Refactoring: Improving the Design of Existing Code", Martin Fowler, Addison Wesley, 1999 - [7] "Java by Comparison", Simon Harrer, Jörg Lenhard, Linus Dietz, Pragmatic Programmers, 2018 -All documents in this project use the -[Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/). +All documents in this project use the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/). Source code (snippets, examples, and classes) are using the [MIT license](https://en.wikipedia.org/wiki/MIT_License). [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg)](https://en.wikipedia.org/wiki/MIT_License) [![License: CC BY 4.0](https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by/4.0/) +![JDK17](https://img.shields.io/badge/jdk-17-blue.svg) diff --git a/src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java b/src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java index c54e54e2..a7720335 100644 --- a/src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java +++ b/src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.Reader; +import java.io.Serial; import java.nio.charset.Charset; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -341,6 +342,7 @@ TransformerFactory createTransformerFactory() { * Indicates that during parsing a non-recoverable error has been occurred. */ public static class ParsingException extends RuntimeException { + @Serial private static final long serialVersionUID = -9016364685084958944L; /** diff --git a/src/test/java/edu/hm/hafner/util/SecureXmlParserFactoryTest.java b/src/test/java/edu/hm/hafner/util/SecureXmlParserFactoryTest.java index 251013f4..12428b2f 100644 --- a/src/test/java/edu/hm/hafner/util/SecureXmlParserFactoryTest.java +++ b/src/test/java/edu/hm/hafner/util/SecureXmlParserFactoryTest.java @@ -28,6 +28,23 @@ class SecureXmlParserFactoryTest { private static final String EXPECTED_EXCEPTION = "EXPECTED EXCEPTION"; + @Test + void shouldFormatMessage() { + var cause = new IllegalArgumentException("IAE"); + + assertThat(new ParsingException(cause)) + .hasMessageContaining("Exception occurred during parsing") + .hasMessageContaining("IllegalArgumentException: IAE") + .hasCause(cause); + assertThat(new ParsingException(cause, "Message %s, Value: %d", "Hello World", 42)) + .hasMessageContaining("Message Hello World, Value: 42") + .hasMessageContaining("IllegalArgumentException: IAE") + .hasCause(cause); + assertThat(new ParsingException("Message %s, Value: %d", "Hello World", 42)) + .hasMessageContaining("Message Hello World, Value: 42") + .hasNoCause(); + } + @Test void shouldCreateDocumentBuilder() throws ParserConfigurationException { var factory = spy(new SecureXmlParserFactory());