builder: CheckstyleWriter: avoid throwing raw exception types
Instead of throwing RuntimeException or IOException, throw CheckstyleWriterException or TemplateNotFoundException.
This commit is contained in:
parent
5876d723e3
commit
81768ff9f9
5 changed files with 114 additions and 13 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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<RuleLevel, String> getOutputFile(final RuleLevel level) throws IOException {
|
||||
|
|
Loading…
Reference in a new issue