modules = new ArrayList<>();
- private String name;
- }
-}
diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java
index 5b5b6cff..93aad5a7 100644
--- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java
+++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRulesDefinition.java
@@ -20,7 +20,7 @@
package org.sonar.plugins.checkstyle;
import org.sonar.api.ExtensionPoint;
-import org.sonar.api.batch.ScannerSide;
+import org.sonar.api.scanner.ScannerSide;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.plugins.checkstyle.metadata.CheckstyleMetadata;
diff --git a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java
index b815d6c2..d9f8ee08 100644
--- a/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java
+++ b/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java
@@ -19,24 +19,14 @@
package org.sonar.plugins.checkstyle;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.rules.RulePriority;
-
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
public final class CheckstyleSeverityUtils {
- private static final Logger LOG = LoggerFactory.getLogger(CheckstyleSeverityUtils.class);
-
private CheckstyleSeverityUtils() {
// only static methods
}
- public static String toSeverity(RulePriority priority) {
- return toSeverity(priority.name());
- }
-
public static String toSeverity(String priority) {
final String result;
@@ -58,31 +48,4 @@ public static String toSeverity(String priority) {
return result;
}
-
- public static RulePriority fromSeverity(String severity) {
- RulePriority result = null;
-
- try {
- final SeverityLevel severityLevel = SeverityLevel.getInstance(severity);
-
- switch (severityLevel) {
- case ERROR:
- result = RulePriority.BLOCKER;
- break;
- case WARNING:
- result = RulePriority.MAJOR;
- break;
- case INFO:
- case IGNORE:
- result = RulePriority.INFO;
- break;
- default:
- }
- }
- catch (Exception exc) {
- LOG.warn("Smth wrong severity", exc);
- }
-
- return result;
- }
}
diff --git a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapper.java b/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapper.java
deleted file mode 100644
index 28512868..00000000
--- a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapper.java
+++ /dev/null
@@ -1,45 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-// checkstyle: Checks Java source code for adherence to a set of rules.
-// Copyright (C) 2001-2024 the original author or authors.
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-////////////////////////////////////////////////////////////////////////////////
-
-package org.sonar.plugins.checkstyle.rule;
-
-import java.util.Map;
-
-/**
- * Due to our usage of {@link org.sonar.plugins.checkstyle.CheckstyleProfileExporter}
- * on the {@link org.sonar.api.batch.ScannerSide} for generating CheckStyle configurations,
- * we must make it compatible for the new {@link org.sonar.api.batch.rule.ActiveRules}.
- *
- *
- * Hence, to gain compatibility with server and scanner side in the exporter,
- * we have to wrap either {@link org.sonar.api.batch.rule.ActiveRule} or
- * {@link org.sonar.api.rules.ActiveRule} for usage in the exporter.
- *
- */
-public interface ActiveRuleWrapper {
- String getInternalKey();
-
- String getRuleKey();
-
- String getTemplateRuleKey();
-
- String getSeverity();
-
- Map getParams();
-}
diff --git a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperScannerImpl.java b/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperScannerImpl.java
deleted file mode 100644
index f8e0c44e..00000000
--- a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperScannerImpl.java
+++ /dev/null
@@ -1,61 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-// checkstyle: Checks Java source code for adherence to a set of rules.
-// Copyright (C) 2001-2024 the original author or authors.
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-////////////////////////////////////////////////////////////////////////////////
-
-package org.sonar.plugins.checkstyle.rule;
-
-import java.util.Map;
-
-import org.sonar.api.batch.rule.ActiveRule;
-import org.sonar.plugins.checkstyle.CheckstyleSeverityUtils;
-
-/**
- * Wrapper as per {@link ActiveRuleWrapper} for the new scanner side {@link ActiveRule}.
- */
-public class ActiveRuleWrapperScannerImpl implements ActiveRuleWrapper {
- private final ActiveRule activeRule;
-
- public ActiveRuleWrapperScannerImpl(ActiveRule activeRule) {
- this.activeRule = activeRule;
- }
-
- @Override
- public String getInternalKey() {
- return activeRule.internalKey();
- }
-
- @Override
- public String getRuleKey() {
- return activeRule.ruleKey().rule();
- }
-
- @Override
- public String getTemplateRuleKey() {
- return activeRule.templateRuleKey();
- }
-
- @Override
- public String getSeverity() {
- return CheckstyleSeverityUtils.toSeverity(activeRule.severity());
- }
-
- @Override
- public Map getParams() {
- return activeRule.params();
- }
-}
diff --git a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperServerImpl.java b/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperServerImpl.java
deleted file mode 100644
index 38534534..00000000
--- a/src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperServerImpl.java
+++ /dev/null
@@ -1,76 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-// checkstyle: Checks Java source code for adherence to a set of rules.
-// Copyright (C) 2001-2024 the original author or authors.
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-////////////////////////////////////////////////////////////////////////////////
-
-package org.sonar.plugins.checkstyle.rule;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.RuleParam;
-import org.sonar.plugins.checkstyle.CheckstyleSeverityUtils;
-
-/**
- * Wrapper as per {@link ActiveRuleWrapper} for the server side {@link ActiveRule}.
- */
-public class ActiveRuleWrapperServerImpl implements ActiveRuleWrapper {
- private final ActiveRule activeRule;
-
- public ActiveRuleWrapperServerImpl(ActiveRule activeRule) {
- this.activeRule = activeRule;
- }
-
- @Override
- public String getInternalKey() {
- return activeRule.getConfigKey();
- }
-
- @Override
- public String getRuleKey() {
- return activeRule.getRuleKey();
- }
-
- @Override
- public String getTemplateRuleKey() {
- String result = null;
- if (Objects.nonNull(activeRule.getRule().getTemplate())) {
- result = activeRule.getRule().getTemplate().getKey();
- }
- return result;
- }
-
- @Override
- public String getSeverity() {
- return CheckstyleSeverityUtils.toSeverity(activeRule.getSeverity());
- }
-
- @Override
- public Map getParams() {
- final Map result = new HashMap<>();
- for (RuleParam param : activeRule.getRule().getParams()) {
- final String value = activeRule.getParameter(param.getKey());
- if (StringUtils.isNotBlank(value)) {
- result.put(param.getKey(), value);
- }
- }
- return result;
- }
-}
diff --git a/src/main/java/org/sonar/plugins/checkstyle/rule/package-info.java b/src/main/java/org/sonar/plugins/checkstyle/rule/package-info.java
deleted file mode 100644
index 007648a1..00000000
--- a/src/main/java/org/sonar/plugins/checkstyle/rule/package-info.java
+++ /dev/null
@@ -1,27 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-// checkstyle: Checks Java source code for adherence to a set of rules.
-// Copyright (C) 2001-2024 the original author or authors.
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-////////////////////////////////////////////////////////////////////////////////
-/**
- * Sonar rule wrapper classes.
- */
-@ParametersAreNonnullByDefault
-
-package org.sonar.plugins.checkstyle.rule;
-
-import javax.annotation.ParametersAreNonnullByDefault;
-
diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java
index 7feab365..df0e5de2 100644
--- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java
+++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java
@@ -79,6 +79,15 @@ public void beforeClass() {
fileSystem.add(testInputFile);
}
+ @Test
+ public void getSourceFilesWithDefaultExporter() {
+ final CheckstyleConfiguration configuration = new CheckstyleConfiguration(null,
+ null, fileSystem);
+ assertThat(configuration.getSourceFiles()).hasSize(1);
+ assertThat(configuration.getSourceFiles().iterator().next().toString())
+ .contains("mainFile");
+ }
+
@Test
public void getSourceFiles() {
final CheckstyleProfileExporter exporter = new FakeExporter();
@@ -140,7 +149,7 @@ public void getCheckstyleConfiguration() throws Exception {
new DefaultActiveRules(Collections.emptyList()), fileSystem);
final Configuration checkstyleConfiguration = configuration.getCheckstyleConfiguration();
assertThat(checkstyleConfiguration).isNotNull();
- assertThat(checkstyleConfiguration.getAttribute("charset")).isEqualTo("UTF-8");
+ assertThat(checkstyleConfiguration.getProperty("charset")).isEqualTo("UTF-8");
final File xmlFile = new File("checkstyle.xml");
assertThat(xmlFile.exists()).isTrue();
diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java
index b40f91fd..2cd4410e 100644
--- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java
+++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java
@@ -20,6 +20,7 @@
package org.sonar.plugins.checkstyle;
import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
@@ -31,16 +32,13 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
-import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
@@ -53,8 +51,7 @@
public class CheckstyleExecutorTest {
- @Rule
- public final ExpectedException thrown = ExpectedException.none();
+ // Remove the 'thrown' field
private final SensorContext context = mock(SensorContext.class);
@@ -87,20 +84,20 @@ public void execute() throws CheckstyleException {
@Test
public void executeException() throws CheckstyleException {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Can not execute Checkstyle");
final CheckstyleConfiguration conf = mockConf();
final CheckstyleExecutor executor = new CheckstyleExecutor(conf, null);
- executor.execute(context);
+ assertThrows("Can not execute Checkstyle",
+ IllegalStateException.class,
+ () -> executor.execute(context));
}
@Test
- public void getUrlException() throws URISyntaxException {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Fail to create the project classloader. "
- + "Classpath element is invalid: htp://aa");
+ public void getUrlException() {
final CheckstyleExecutor executor = new CheckstyleExecutor(null, mockListener());
- executor.getUrl(new URI("htp://aa"));
+ assertThrows("Fail to create the project classloader. "
+ + "Classpath element is invalid: htp://aa",
+ IllegalStateException.class,
+ () -> executor.getUrl(new URI("htp://aa")));
}
/**
@@ -126,7 +123,8 @@ public void generateXmlReportInEnglish() throws Exception {
Assert.assertTrue("Report should exists", report.exists());
- final String reportContents = FileUtils.readFileToString(report);
+ final String reportContents = FileUtils.readFileToString(report,
+ Charset.defaultCharset());
assertThat(reportContents).contains(" CheckstyleExecutor.close(closeable));
}
private static CheckstyleAuditListener mockListener() {
diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java
index e58dcc2d..d6e9eba6 100644
--- a/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java
+++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstylePluginTest.java
@@ -32,7 +32,7 @@ public class CheckstylePluginTest {
@Test
public void testGetExtensions() {
- assertThat(CheckstylePlugin.getExtensions().size()).isEqualTo(11);
+ assertThat(CheckstylePlugin.getExtensions().size()).isEqualTo(9);
}
@Test
diff --git a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
index 10f8b3cc..612a6d9b 100644
--- a/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
+++ b/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
@@ -22,8 +22,10 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -44,14 +46,16 @@
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.ConfigurationBridge;
import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RulePriority;
import org.sonar.api.utils.System2;
public class CheckstyleProfileExporterTest {
+ private final ActiveRule testActiveRule = new TestActiveRule(
+ RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
+ "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"),
+ "Checker/JavadocPackage", "TEMPLATE", "abcde");
+
private Configuration settings;
@Before
@@ -67,140 +71,119 @@ public void tearDown() {
}
@Test
- public void alwaysSetSuppressionCommentFilter() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
+ public void noCheckstyleActiveRulesToExport() {
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.emptyList());
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "alwaysSetSuppressionCommentFilter.xml",
+ + "noCheckstyleActiveRulesToExport.xml",
sanitizeForTests(writer.toString()));
}
@Test
- public void noCheckstyleRulesToExport() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
-
- // this is a PMD rule
- profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
+ public void singleCheckstyleActiveRulesToExport() {
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.singletonList(testActiveRule));
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "noCheckstyleRulesToExport.xml", sanitizeForTests(writer.toString()));
+ + "singleCheckstyleActiveRulesToExport.xml",
+ sanitizeForTests(writer.toString()));
}
@Test
- public void singleCheckstyleRulesToExport() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
- profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
- profile.activateRule(
- Rule.create("checkstyle",
- "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
- "Javadoc").setConfigKey("Checker/JavadocPackage"), RulePriority.MAJOR);
- profile.activateRule(
- Rule.create(
- "checkstyle",
- "com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck",
- "Local Variable").setConfigKey(
- "Checker/TreeWalker/Checker/TreeWalker/LocalFinalVariableName"),
- RulePriority.MINOR);
+ public void treewalkerCheckstyleActiveRulesToExport() {
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.singletonList(new TestActiveRule(
+ RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
+ "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"),
+ "Checker/TreeWalker/JavadocPackage", "TEMPLATE", null)));
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "singleCheckstyleRulesToExport.xml", sanitizeForTests(writer.toString()));
- }
-
- @Test
- public void ruleThrowsException() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
- try {
- new CheckstyleProfileExporter(settings).exportProfile(profile, new IoExceptionWriter());
- Assert.fail("IOException while writing should not be ignored");
- }
- catch (IllegalStateException ex) {
- Assertions.assertThat(ex.getMessage())
- .isEqualTo("Fail to export the profile " + profile);
- }
+ + "treewalkerCheckstyleActiveRulesToExport.xml",
+ sanitizeForTests(writer.toString()));
}
@Test
- public void singleCheckstyleActiveRulesToExport() {
+ public void sameCheckstyleActiveRulesToExport() {
+ final List rules = new ArrayList<>();
+ rules.add(testActiveRule);
+ rules.add(new TestActiveRule(RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
+ "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"),
+ "Checker/JavadocPackage", "TEMPLATE", "fghij"));
final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
- .thenReturn(Collections.singletonList(new TestActiveRule()));
+ .thenReturn(rules);
final StringWriter writer = new StringWriter();
new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "singleCheckstyleActiveRulesToExport.xml",
+ + "sameCheckstyleActiveRulesToExport.xml",
sanitizeForTests(writer.toString()));
}
@Test
- public void activeRulesThrowsException() {
- try {
- new CheckstyleProfileExporter(settings).exportProfile(
- new DefaultActiveRules(Collections.emptyList()), new IoExceptionWriter());
- Assert.fail("IOException while writing should not be ignored");
- }
- catch (IllegalStateException ex) {
- Assertions.assertThat(ex.getMessage()).isEqualTo("Fail to export active rules.");
- }
- }
-
- @Test
- public void addTheIdPropertyWhenManyInstancesWithTheSameConfigKey() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
- final Rule rule1 = Rule.create("checkstyle",
- "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
- .setConfigKey("Checker/JavadocPackage");
- final Rule rule2 = Rule
- .create("checkstyle",
- "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345",
- "Javadoc").setConfigKey("Checker/JavadocPackage").setParent(rule1);
-
- profile.activateRule(rule1, RulePriority.MAJOR);
- profile.activateRule(rule2, RulePriority.CRITICAL);
+ public void noCheckstyleTemplateActiveRulesToExport() {
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.singletonList(new TestActiveRule(
+ RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
+ "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"),
+ "Checker/JavadocPackage", null, "fghij")));
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml",
+ + "noCheckstyleTemplateActiveRulesToExport.xml",
sanitizeForTests(writer.toString()));
}
@Test
- public void exportParameters() {
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
- final Rule rule = Rule.create("checkstyle",
- "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
- .setConfigKey("Checker/JavadocPackage");
- rule.createParameter("format");
- // not set in the profile and no default value => not exported in
- // checkstyle
- rule.createParameter("message");
- rule.createParameter("ignore");
-
- profile.activateRule(rule, RulePriority.MAJOR).setParameter("format", "abcde");
+ public void blankParamCheckstyleActiveRulesToExport() {
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.singletonList(new TestActiveRule(
+ RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
+ "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"),
+ "Checker/JavadocPackage", "TEMPLATE", "")));
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
- + "exportParameters.xml", sanitizeForTests(writer.toString()));
+ + "blankParamCheckstyleActiveRulesToExport.xml",
+ sanitizeForTests(writer.toString()));
+ }
+
+ @Test
+ public void activeRulesThrowsException() {
+ try {
+ new CheckstyleProfileExporter(settings).exportProfile(
+ new DefaultActiveRules(Collections.emptyList()), new IoExceptionWriter());
+ Assert.fail("IOException while writing should not be ignored");
+ }
+ catch (IllegalStateException ex) {
+ Assertions.assertThat(ex.getMessage()).isEqualTo("Fail to export active rules.");
+ }
}
@Test
@@ -215,9 +198,11 @@ public void addCustomCheckerFilters() {
+ ""
+ "" + "");
- final RulesProfile profile = RulesProfile.create("sonar way", "java");
+ final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
+ Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
+ .thenReturn(Collections.singletonList(testActiveRule));
final StringWriter writer = new StringWriter();
- new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
+ new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);
CheckstyleTestUtils.assertSimilarXmlWithResource(
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
@@ -229,9 +214,11 @@ public void addCustomTreewalkerFilters() {
initSettings(CheckstyleConstants.TREEWALKER_FILTERS_KEY,
"