From 81768ff9f947968ca49971c0c73e79fafacf10fc Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 11:23:48 +0100 Subject: [PATCH 1/2] builder: CheckstyleWriter: avoid throwing raw exception types Instead of throwing RuntimeException or IOException, throw CheckstyleWriterException or TemplateNotFoundException. --- .../ruleset/builder/CheckstyleWriter.java | 4 +- .../builder/CheckstyleWriterException.java | 39 ++++++++++++++++++ .../builder/TemplateNotFoundException.java | 41 +++++++++++++++++++ .../CheckstyleWriterExceptionTest.java | 19 +++++++++ .../ruleset/builder/CheckstyleWriterTest.java | 24 ++++++----- 5 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterException.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateNotFoundException.java create mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java index 0619a7d..3e676e7 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriter.java @@ -91,10 +91,10 @@ class CheckstyleWriter implements CommandLineRunner { log.info("Writing xmlFile: {}", xmlFile); Files.write(xmlFile, output, StandardCharsets.UTF_8); } else { - throw new IOException("Missing template: " + checkstyleXmlTemplate.toString()); + throw new TemplateNotFoundException(checkstyleXmlTemplate); } } catch (IOException e) { - throw new RuntimeException(e); + throw new CheckstyleWriterException(e); } } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterException.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterException.java new file mode 100644 index 0000000..641d96e --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterException.java @@ -0,0 +1,39 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +/** + * Raised when there was an error writing a Checkstyle ruleset. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +class CheckstyleWriterException extends RuntimeException { + + /** + * Constructor. + * + * @param cause the cause + */ + CheckstyleWriterException(final Throwable cause) { + super(cause); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateNotFoundException.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateNotFoundException.java new file mode 100644 index 0000000..08d1521 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateNotFoundException.java @@ -0,0 +1,41 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.ruleset.builder; + +import java.nio.file.Path; + +/** + * Raised when a rule template is not found. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +class TemplateNotFoundException extends RuntimeException { + + /** + * Constructor. + * + * @param template the missing template + */ + TemplateNotFoundException(final Path template) { + super("Missing template: " + template.toString()); + } +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java new file mode 100644 index 0000000..ee44130 --- /dev/null +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java @@ -0,0 +1,19 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import org.junit.Test; + +/** + * Tests for {@link CheckstyleWriterException}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class CheckstyleWriterExceptionTest { + + @Test + public void canCreateException() { + //given + //when + //then + } + +} diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java index da7d873..733908a 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java @@ -2,9 +2,9 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; import me.andrz.builder.map.MapBuilder; +import org.assertj.core.api.ThrowableAssert; import org.junit.Before; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.mockito.MockitoAnnotations; @@ -20,6 +20,7 @@ import java.util.Map; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link CheckstyleWriter}. @@ -48,9 +49,6 @@ public class CheckstyleWriterTest { private Path checkstyleTemplate; - @org.junit.Rule - public ExpectedException exception = ExpectedException.none(); - @org.junit.Rule public TemporaryFolder folder = new TemporaryFolder(); @@ -173,10 +171,11 @@ public class CheckstyleWriterTest { public void throwRteIfTemplateNotFound() throws Exception { //given templateProperties.setCheckstyleXml(Paths.get("garbage")); - exception.expect(RuntimeException.class); - exception.expectMessage("Missing template: garbage"); //when - checkstyleWriter.run(); + final ThrowableAssert.ThrowingCallable action = () -> checkstyleWriter.run(); + //then + assertThatThrownBy(action).isInstanceOf(TemplateNotFoundException.class) + .hasMessage("Missing template: garbage"); } // throw RTE if error writing file @@ -185,11 +184,14 @@ public class CheckstyleWriterTest { //given final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary"); outputProperties.setDirectory(Paths.get(imaginary)); - exception.expect(RuntimeException.class); - exception.expectMessage( - "java.nio.file.NoSuchFileException: " + imaginary + FILE_SEPARATOR + "checkstyle-LAYOUT.xml"); //when - checkstyleWriter.run(); + final ThrowableAssert.ThrowingCallable action = () -> checkstyleWriter.run(); + //then + assertThatThrownBy(action).isInstanceOf(CheckstyleWriterException.class) + .hasMessage( + String.format("java.nio.file.NoSuchFileException: %scheckstyle-LAYOUT.xml", + imaginary + FILE_SEPARATOR + )); } private Map.Entry getOutputFile(final RuleLevel level) throws IOException { From 9ac59206a0c0a85de8e224d88a59078c8b4235c6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 29 May 2017 11:26:24 +0100 Subject: [PATCH 2/2] KCR28: builder: remove empty test class --- .../CheckstyleWriterExceptionTest.java | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java deleted file mode 100644 index ee44130..0000000 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterExceptionTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.kemitix.checkstyle.ruleset.builder; - -import org.junit.Test; - -/** - * Tests for {@link CheckstyleWriterException}. - * - * @author Paul Campbell (pcampbell@kemitix.net) - */ -public class CheckstyleWriterExceptionTest { - - @Test - public void canCreateException() { - //given - //when - //then - } - -}