Skip to content

Commit

Permalink
Improve coverage of ParsingException.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Oct 26, 2024
1 parent 3a6a988 commit 3e9037e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
30 changes: 13 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@
[![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)
- [Error Prone](https://errorprone.info)

❗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)
Expand All @@ -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)
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/edu/hm/hafner/util/SecureXmlParserFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 3e9037e

Please sign in to comment.