From 7b29da18a05fc6c9fe26da4c1e4647d1e13f38ac Mon Sep 17 00:00:00 2001 From: Daniel Muehlbachler Date: Tue, 14 May 2019 13:45:10 +0200 Subject: [PATCH] Issue #217: resolve SonarCloud violations --- pom.xml | 1 - .../checkstyle/CheckstyleAuditListener.java | 2 +- .../checkstyle/CheckstyleExecutor.java | 8 ++++--- .../plugins/checkstyle/CheckstylePlugin.java | 23 ++++++++++--------- .../checkstyle/CheckstyleProfileExporter.java | 2 +- .../checkstyle/CheckstyleRulesDefinition.java | 22 ++++++++++++++++-- .../CheckstyleConfigurationTest.java | 2 +- .../checkstyle/CheckstylePluginTest.java | 8 ++++++- .../CheckstyleProfileExporterTest.java | 2 +- .../checkstyle/CheckstyleVersionTest.java | 2 +- 10 files changed, 49 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index da200d26..52001ad8 100644 --- a/pom.xml +++ b/pom.xml @@ -430,7 +430,6 @@ cobertura-maven-plugin 2.7 - xml html diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java index c3c7d857..5991a468 100644 --- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java +++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java @@ -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))) diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java index 7e8a03a2..c9428041 100644 --- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java +++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java @@ -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; @@ -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); @@ -103,7 +104,7 @@ 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()); @@ -111,7 +112,8 @@ private void executeWithClassLoader(URLClassLoader projectClassloader) { checker.process(configuration .getSourceFiles() .stream() - .map(InputFile::file) + .map(InputFile::uri) + .map(File::new) .collect(Collectors.toList())); } catch (Exception ex) { diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java index feccf902..0f071b71 100644 --- a/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java +++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java @@ -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"; @@ -67,13 +66,17 @@ public final class CheckstylePlugin extends SonarPlugin { + "Checkstyle " + "configuration for more information."; - @SuppressWarnings("rawtypes") @Override - public List getExtensions() { + public void define(Context context) { + context.addExtensions(this.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) @@ -81,14 +84,14 @@ public List getExtensions() { .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) @@ -96,15 +99,13 @@ public List getExtensions() { .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); } - } diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java index 4b405819..bda5742e 100644 --- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java +++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java @@ -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); } diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java index 2503b91c..ece6771d 100644 --- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java +++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java @@ -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; @@ -61,7 +63,7 @@ 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); @@ -69,6 +71,22 @@ static void extractRulesData(NewRepository repository, String xmlRulesFilePath, 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/ diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java index 65bba216..73db5a41 100644 --- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java +++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java @@ -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); diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java index 162f7fb7..5243842e 100644 --- a/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java +++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java @@ -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); } } diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java index 49a2c80c..2f9f1732 100644 --- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java +++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java @@ -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); } diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleVersionTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleVersionTest.java index 2fe046a0..72124207 100644 --- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleVersionTest.java +++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleVersionTest.java @@ -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); }