Skip to content

Commit

Permalink
Issue checkstyle#217: resolve SonarCloud violations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Muehlbachler committed May 14, 2019
1 parent 9e0532b commit f3e4a1a
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 23 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<!--<quiet>true</quiet>-->
<formats>
<format>xml</format>
<format>html</format>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void addError(AuditEvent event) {
final NewIssue issue = context.newIssue();
final ActiveRule rule = ruleFinder.find(
RuleKey.of(CheckstyleConstants.REPOSITORY_KEY, ruleKey));
if (Objects.nonNull(issue) && Objects.nonNull(rule)) {
if (Objects.nonNull(issue) && Objects.nonNull(rule) && Objects.nonNull(message)) {
final NewIssueLocation location = issue.newLocation()
.on(currentResource)
.at(currentResource.selectLine(getLineId(event)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -51,7 +52,7 @@
@ExtensionPoint
@ScannerSide
public class CheckstyleExecutor {
public static final String PROPERTIES_PATH =
public static final String PROPERTIES_LOCATION =
"/org/sonar/plugins/checkstyle/checkstyle-plugin.properties";

private static final Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class);
Expand Down Expand Up @@ -103,15 +104,16 @@ private void executeWithClassLoader(URLClassLoader projectClassloader) {
if (xmlReport != null) {
LOG.info("Checkstyle output report: {}", xmlReport.getAbsolutePath());
xmlOutput = FileUtils.openOutputStream(xmlReport);
checker.addListener(new XMLLogger(xmlOutput, true));
checker.addListener(new XMLLogger(xmlOutput, AutomaticBean.OutputStreamOptions.CLOSE));
}

checker.setCharset(configuration.getCharset().name());
checker.configure(configuration.getCheckstyleConfiguration());
checker.process(configuration
.getSourceFiles()
.stream()
.map(InputFile::file)
.map(InputFile::uri)
.map(File::new)
.collect(Collectors.toList()));
}
catch (Exception ex) {
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import java.util.Arrays;
import java.util.List;

import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.PropertyType;
import org.sonar.api.SonarPlugin;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;

public final class CheckstylePlugin extends SonarPlugin {
public final class CheckstylePlugin implements Plugin {

private static final String CHECKSTYLE_SUB_CATEGORY_NAME = "Checkstyle";

Expand Down Expand Up @@ -67,44 +66,46 @@ public final class CheckstylePlugin extends SonarPlugin {
+ "Checkstyle</a> "
+ "configuration for more information.";

@SuppressWarnings("rawtypes")
@Override
public List getExtensions() {
public void define(Context context) {
context.addExtensions(getCheckstyleExtensions());
}

@SuppressWarnings("rawtypes")
public List getCheckstyleExtensions() {
return Arrays
.asList(PropertyDefinition.builder(CheckstyleConstants.CHECKER_FILTERS_KEY)
.defaultValue(CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE)
.category(CoreProperties.CATEGORY_JAVA)
.category(CheckstyleConstants.JAVA_KEY)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Checker Filters")
.description(CHECKER_FILTERS_DESCRIPTION)
.type(PropertyType.TEXT)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
PropertyDefinition.builder(CheckstyleConstants.TREEWALKER_FILTERS_KEY)
.defaultValue(CheckstyleConstants.TREEWALKER_FILTERS_DEFAULT_VALUE)
.category(CoreProperties.CATEGORY_JAVA)
.category(CheckstyleConstants.JAVA_KEY)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Treewalker Filters")
.description(TREEWALKER_FILTERS_DESCRIPTION)
.type(PropertyType.TEXT)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
PropertyDefinition.builder(CheckstyleConstants.CHECKER_TAB_WIDTH)
.category(CoreProperties.CATEGORY_JAVA)
.category(CheckstyleConstants.JAVA_KEY)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Tab Width")
.description(CHECKER_TAB_WIDTH_DESCRIPTION)
.type(PropertyType.INTEGER)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.build(),
PropertyDefinition.builder(CheckstyleConfiguration.PROPERTY_GENERATE_XML)
.defaultValue("false").category(CoreProperties.CATEGORY_JAVA)
.defaultValue("false").category(CheckstyleConstants.JAVA_KEY)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Generate XML Report").type(PropertyType.BOOLEAN).hidden()
.build(),

CheckstyleSensor.class, CheckstyleConfiguration.class,
CheckstyleExecutor.class, CheckstyleAuditListener.class,
CheckstyleProfileExporter.class, CheckstyleProfileImporter.class,
CheckstyleRulesDefinition.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void appendCustomFilters(Writer writer) throws IOException {

private void appendTabWidth(Writer writer) throws IOException {
final String tabWidth = configuration.get(CheckstyleConstants.CHECKER_TAB_WIDTH)
.orElse(null);
.orElse("");
appendModuleProperty(writer, "tabWidth", tabWidth);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;
import java.util.Properties;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;
import org.sonar.squidbridge.rules.ExternalDescriptionLoader;
import org.sonar.squidbridge.rules.SqaleXmlLoader;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -61,14 +63,30 @@ static void extractRulesData(NewRepository repository, String xmlRulesFilePath,
.getResourceAsStream(xmlRulesFilePath)) {
ruleLoader.load(repository, resource, "UTF-8");
}
ExternalDescriptionLoader.loadHtmlDescriptions(repository, htmlDescriptionFolder);
loadHtmlDescriptions(repository, htmlDescriptionFolder);
try (InputStream resource = CheckstyleRulesDefinition.class
.getResourceAsStream("/org/sonar/l10n/checkstyle.properties")) {
loadNames(repository, resource);
}
SqaleXmlLoader.load(repository, "/com/sonar/sqale/checkstyle-model.xml");
}

private static void loadHtmlDescriptions(NewRepository repository, String htmlDescriptionFolder) {
// code adapted from:
// https://github.com/SonarSource/sslr-squid-bridge/blob/2.7.0.377/
// src/main/java/org/sonar/squidbridge/rules/ExternalDescriptionLoader.java
for (NewRule rule : repository.rules()) {
final URL resource = CheckstyleRulesDefinition.class.getResource(htmlDescriptionFolder
+ "/" + rule.key() + ".html");
try {
rule.setHtmlDescription(Resources.toString(resource, Charsets.UTF_8));
}
catch(IOException e) {
throw new IllegalStateException("Failed to read: " + resource, e);
}
}
}

private static void loadNames(NewRepository repository, InputStream stream) {
// code taken from:
// https://github.com/SonarSource/sslr-squid-bridge/blob/2.5.2/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void writeConfigurationToWorkingDir() throws IOException {
public void getCheckstyleConfiguration() throws Exception {
fileSystem.setEncoding(StandardCharsets.UTF_8);
final MapSettings mapSettings = new MapSettings(new PropertyDefinitions(
new CheckstylePlugin().getExtensions()));
new CheckstylePlugin().getCheckstyleExtensions()));
mapSettings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE);
final org.sonar.api.config.Configuration settings = new ConfigurationBridge(mapSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.mockito.Mockito;
import org.sonar.api.Plugin;
import org.sonar.api.SonarRuntime;

public class CheckstylePluginTest {

@Test
public void testGetExtensions() {
final SonarRuntime sonarRuntime = Mockito.mock(SonarRuntime.class);
final CheckstylePlugin plugin = new CheckstylePlugin();
assertThat(plugin.getExtensions().size()).isGreaterThan(1);
final Plugin.Context context = new Plugin.Context(sonarRuntime);
plugin.define(context);
assertThat(context.getExtensions().size()).isGreaterThan(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void addTabWidthProperty() {
@SuppressWarnings("unchecked")
private void initSettings(@Nullable String key, @Nullable String property) {
final MapSettings mapSettings = new MapSettings(
new PropertyDefinitions(new CheckstylePlugin().getExtensions()));
new PropertyDefinitions(new CheckstylePlugin().getCheckstyleExtensions()));
if (Objects.nonNull(key)) {
mapSettings.setProperty(key, property);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CheckstyleVersionTest {

@Test
public void getCheckstyleVersion() {
final String version = CheckstyleExecutor.PROPERTIES_PATH;
final String version = CheckstyleExecutor.PROPERTIES_LOCATION;
assertThat(new CheckstyleVersion().getVersion(version).length()).isGreaterThan(1);
}

Expand Down

0 comments on commit f3e4a1a

Please sign in to comment.