Merge branch 'develop' into upgrade-checkstyle

* develop:
  wercker.yml: rewritten
  wercker.yml: added
  travis-ci: remove defensive checks
  shippable.yml: added
  travis-ci: only deploy from Travis-CI
  KCR37+38: MoveVariableInsideIf: avoid unused private methods
  KCR39: regressions: MoveVariableInsideIf: method(): use parameter
  KCR38: regressions: MoveVariableInsideIf: valid(): use explicit scope
  KCR37: regressions: MoveVariableInsideIf: invalid(): use explicit scope
  KXR36: plugin: DefaultCheckstyleExecutorTest: artifactFile: make local variable
  builder: ReadmeWriterTest: templateProperties: make local variable
  KCR35: builder: ReadmeWriterTest: fragments: make local variable
  KCR34: builder: ReadmeWriterTest: template: make local variable
  KCR33: builder: ReadmeWriterTest: outputProperties: make local
  KCR31: builder: DefaultRuleReadmeLoaderTest: make field local
  KCr30: CheckstyleWriterTest: checkstyleTemplate: make field local variable
  KCR28: builder: remove empty test class
  builder: CheckstyleWriter: avoid throwing raw exception types
This commit is contained in:
Paul Campbell 2017-05-30 12:38:38 +01:00
commit 0503156bd0
10 changed files with 121 additions and 47 deletions

View file

@ -93,10 +93,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);
}
}

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,6 +2,7 @@ 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;
@ -20,6 +21,7 @@ import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
@ -56,8 +58,6 @@ public class CheckstyleWriterTest {
private Path outputDirectory;
private Path checkstyleTemplate;
@Mock
private RuleClassLocator ruleClassLocator;
@ -78,7 +78,7 @@ public class CheckstyleWriterTest {
.toPath();
outputProperties.setDirectory(outputDirectory);
templateProperties = new TemplateProperties();
checkstyleTemplate = folder.newFile("checkstyle-template.xml")
val checkstyleTemplate = folder.newFile("checkstyle-template.xml")
.toPath();
Files.write(
checkstyleTemplate, TEMPLATE.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
@ -204,10 +204,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
@ -216,10 +217,13 @@ 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
));
}
}

View file

@ -23,12 +23,8 @@ public class DefaultRuleReadmeLoaderTest {
private RuleReadmeLoader loader;
private TemplateProperties templateProperties;
private Rule rule;
private Path fragment;
private Path fragments;
@org.junit.Rule
@ -39,7 +35,7 @@ public class DefaultRuleReadmeLoaderTest {
@Before
public void setUp() throws Exception {
templateProperties = new TemplateProperties();
final TemplateProperties templateProperties = new TemplateProperties();
fragments = folder.newFolder("fragments")
.toPath();
templateProperties.setReadmeFragments(fragments);
@ -53,8 +49,8 @@ public class DefaultRuleReadmeLoaderTest {
public void loadEnabledOkay() throws IOException {
//given
rule.setEnabled(true);
fragment = fragments.resolve("name.md");
Files.write(fragment, Arrays.asList("", "body"));
final Path fragment1 = fragments.resolve("name.md");
Files.write(fragment1, Arrays.asList("", "body"));
//when
val fragment = loader.load(rule);
//then

View file

@ -25,10 +25,6 @@ public class ReadmeWriterTest {
private ReadmeWriter readmeWriter;
private TemplateProperties templateProperties;
private OutputProperties outputProperties;
private RulesProperties rulesProperties;
@Mock
@ -37,10 +33,6 @@ public class ReadmeWriterTest {
@Mock
private ReadmeIndexBuilder indexBuilder;
private Path template;
private Path fragments;
private Path readme;
@org.junit.Rule
@ -49,17 +41,17 @@ public class ReadmeWriterTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
template = folder.newFile("README-template.md")
final Path template = folder.newFile("README-template.md")
.toPath();
Files.write(template, Arrays.asList("i:%s", "ce:%s", "se:%s", "cd:%s", "sd:%s"));
fragments = folder.newFolder("fragments")
final TemplateProperties templateProperties = new TemplateProperties();
templateProperties.setReadmeTemplate(template);
final Path fragments = folder.newFolder("fragments")
.toPath();
templateProperties.setReadmeFragments(fragments);
final OutputProperties outputProperties = new OutputProperties();
readme = folder.newFile("README.md")
.toPath();
templateProperties = new TemplateProperties();
templateProperties.setReadmeTemplate(template);
templateProperties.setReadmeFragments(fragments);
outputProperties = new OutputProperties();
outputProperties.setReadme(readme);
rulesProperties = new RulesProperties();
readmeWriter =

View file

@ -72,8 +72,6 @@ public class DefaultCheckstyleExecutorTest {
@Mock
private Artifact artifact;
private File artifactFile;
@Mock
private MavenXpp3Reader mavenXpp3Reader;
@ -122,8 +120,8 @@ public class DefaultCheckstyleExecutorTest {
.level(level)
.build();
artifactFile = folder.newFile("pom.xml");
given(artifactRepository.find(any())).willReturn(artifact);
final File artifactFile = folder.newFile("pom.xml");
given(artifact.getFile()).willReturn(artifactFile);
given(mavenXpp3Reader.read(any(FileReader.class))).willReturn(pomModel);
final Properties properties = new Properties();

View file

@ -33,7 +33,7 @@ class MoveVariableInsideIf {
private boolean condition;
private String method(final String variable) {
return "value";
return "value: " + variable;
}
/**
@ -42,7 +42,7 @@ class MoveVariableInsideIf {
* @return value
*/
@SuppressWarnings("movevariableinsideif")
String invalid() {
protected String invalid() {
String variable = input.substring(1);
if (condition) {
return method(variable);
@ -55,7 +55,7 @@ class MoveVariableInsideIf {
*
* @return value
*/
String valid() {
protected String valid() {
if (condition) {
String variable = input.substring(1);
return method(variable);

8
shippable.yml Normal file
View file

@ -0,0 +1,8 @@
language: java
jdk:
- oraclejdk8
cache:
directories:
- "$HOME/.m2"
install: true
script: "./mvnw clean install"

View file

@ -1,7 +1,3 @@
#!/usr/bin/env bash
## Only send coveralls reports from Travis-CI. Some CIs, like Shippable, lie by setting TRAVIS=true.
## Currently, Shippable, does not set TRAVIS_LANGUAGE, but Travis-CI does.
if [ "$TRAVIS_LANGUAGE" = "java" ];then
./mvnw --projects builder,plugin test jacoco:report coveralls:report
fi
./mvnw --projects builder,plugin test jacoco:report coveralls:report