Merge pull request #40 from kemitix/kcr-28-avoid-throwing-raw-exception-types

KCR 28 avoid throwing raw exception types
This commit is contained in:
Paul Campbell 2017-05-29 12:22:10 +01:00 committed by GitHub
commit d7454a1155
4 changed files with 95 additions and 13 deletions

View file

@ -91,10 +91,10 @@ class CheckstyleWriter implements CommandLineRunner {
log.info("Writing xmlFile: {}", xmlFile); log.info("Writing xmlFile: {}", xmlFile);
Files.write(xmlFile, output, StandardCharsets.UTF_8); Files.write(xmlFile, output, StandardCharsets.UTF_8);
} else { } else {
throw new IOException("Missing template: " + checkstyleXmlTemplate.toString()); throw new TemplateNotFoundException(checkstyleXmlTemplate);
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new CheckstyleWriterException(e);
} }
} }

View file

@ -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);
}
}

View file

@ -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());
}
}

View file

@ -2,9 +2,9 @@ package net.kemitix.checkstyle.ruleset.builder;
import lombok.val; import lombok.val;
import me.andrz.builder.map.MapBuilder; import me.andrz.builder.map.MapBuilder;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -20,6 +20,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Tests for {@link CheckstyleWriter}. * Tests for {@link CheckstyleWriter}.
@ -48,9 +49,6 @@ public class CheckstyleWriterTest {
private Path checkstyleTemplate; private Path checkstyleTemplate;
@org.junit.Rule
public ExpectedException exception = ExpectedException.none();
@org.junit.Rule @org.junit.Rule
public TemporaryFolder folder = new TemporaryFolder(); public TemporaryFolder folder = new TemporaryFolder();
@ -173,10 +171,11 @@ public class CheckstyleWriterTest {
public void throwRteIfTemplateNotFound() throws Exception { public void throwRteIfTemplateNotFound() throws Exception {
//given //given
templateProperties.setCheckstyleXml(Paths.get("garbage")); templateProperties.setCheckstyleXml(Paths.get("garbage"));
exception.expect(RuntimeException.class);
exception.expectMessage("Missing template: garbage");
//when //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 // throw RTE if error writing file
@ -185,11 +184,14 @@ public class CheckstyleWriterTest {
//given //given
final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary"); final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary");
outputProperties.setDirectory(Paths.get(imaginary)); outputProperties.setDirectory(Paths.get(imaginary));
exception.expect(RuntimeException.class);
exception.expectMessage(
"java.nio.file.NoSuchFileException: " + imaginary + FILE_SEPARATOR + "checkstyle-LAYOUT.xml");
//when //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<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException { private Map.Entry<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException {