diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-13 17:49:40 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-13 17:49:40 +0000 |
commit | cdd05f32ec75a1594f8f86e1fdb0bd4101598654 (patch) | |
tree | 54391e2d3ead800eb44b40feaa7d7068c80ddc02 /plugins/sonar-checkstyle-plugin | |
parent | ed770f36d376af5ef614f793865eb76713b4ff93 (diff) | |
download | sonarqube-cdd05f32ec75a1594f8f86e1fdb0bd4101598654.tar.gz sonarqube-cdd05f32ec75a1594f8f86e1fdb0bd4101598654.zip |
SONAR-1229 simplify the profiles API
Diffstat (limited to 'plugins/sonar-checkstyle-plugin')
12 files changed, 184 insertions, 70 deletions
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java index d5ea971a55e..b18a198b40a 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java @@ -146,14 +146,8 @@ public class CheckstyleProfileExporter extends ProfileExporter { private void appendRuleParameters(Writer writer, ActiveRule activeRule) throws IOException { for (RuleParam ruleParam : activeRule.getRule().getParams()) { - ActiveRuleParam activeParam = activeRule.getParameter(ruleParam.getKey()); - String value; - if (activeParam == null) { - value = ruleParam.getDefaultValue(); - } else { - value = activeParam.getValue(true); - } - if (value != null) { + String value = activeRule.getParameter(ruleParam.getKey(), true); + if (StringUtils.isNotBlank(value)) { appendModuleProperty(writer, ruleParam.getKey(), value); } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java index 34c9ebf9301..891f6c7d91d 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java @@ -25,8 +25,12 @@ import org.codehaus.staxmate.SMInputFactory; import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.profiles.ProfileImporter; -import org.sonar.api.profiles.ProfilePrototype; +import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RuleQuery; import org.sonar.api.utils.ValidationMessages; import javax.xml.stream.XMLInputFactory; @@ -38,16 +42,18 @@ public class CheckstyleProfileImporter extends ProfileImporter { private static final String CHECKER_MODULE = "Checker"; private static final String TREEWALKER_MODULE = "TreeWalker"; private static final String MODULE_NODE = "module"; + private final RuleFinder ruleFinder; - public CheckstyleProfileImporter() { + public CheckstyleProfileImporter(RuleFinder ruleFinder) { super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME); setSupportedLanguages(Java.KEY); + this.ruleFinder = ruleFinder; } @Override - public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { + public RulesProfile importProfile(Reader reader, ValidationMessages messages) { SMInputFactory inputFactory = initStax(); - ProfilePrototype profile = ProfilePrototype.create(); + RulesProfile profile = RulesProfile.create(); try { SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader); rootC.advance(); // <module name="Checker"> @@ -79,17 +85,23 @@ public class CheckstyleProfileImporter extends ProfileImporter { return inputFactory; } - private void processModule(ProfilePrototype profile, String path, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { + private void processModule(RulesProfile profile, String path, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { String configKey = moduleCursor.getAttrValue("name"); if (isFilter(configKey)) { messages.addWarningText("Checkstyle filters are not imported: " + configKey); } else if (isIgnored(configKey)) { + // ignore ! } else { - ProfilePrototype.RulePrototype rule = ProfilePrototype.RulePrototype.createByConfigKey(CheckstyleConstants.REPOSITORY_KEY, path + configKey); - processProperties(moduleCursor, messages, rule); - profile.activateRule(rule); + Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withConfigKey(path + configKey)); + if (rule == null) { + messages.addWarningText("Checkstyle rule not found: " + path + configKey); + + } else { + ActiveRule activeRule = profile.activateRule(rule, null); + processProperties(moduleCursor, messages, activeRule); + } } } @@ -104,24 +116,24 @@ public class CheckstyleProfileImporter extends ProfileImporter { StringUtils.equals(configKey, "SuppressWithNearbyCommentFilter"); } - private void processProperties(SMInputCursor moduleCursor, ValidationMessages messages, ProfilePrototype.RulePrototype rule) throws XMLStreamException { + private void processProperties(SMInputCursor moduleCursor, ValidationMessages messages, ActiveRule activeRule) throws XMLStreamException { SMInputCursor propertyCursor = moduleCursor.childElementCursor("property"); while (propertyCursor.getNext() != null) { - processProperty(rule, propertyCursor, messages); + processProperty(activeRule, propertyCursor, messages); } } - private void processProperty(ProfilePrototype.RulePrototype rule, SMInputCursor propertyCursor, ValidationMessages messages) throws XMLStreamException { + private void processProperty(ActiveRule activeRule, SMInputCursor propertyCursor, ValidationMessages messages) throws XMLStreamException { String key = propertyCursor.getAttrValue("name"); String value = propertyCursor.getAttrValue("value"); if (StringUtils.equals("id", key)) { - messages.addWarningText("The checkstyle property 'id' is not supported in the rule: " + rule.getConfigKey()); + messages.addWarningText("The property 'id' is not supported in the Checkstyle rule: " + activeRule.getConfigKey()); } else if (StringUtils.equals("severity", key)) { - rule.setPriority(CheckstyleSeverityUtils.fromSeverity(value)); + activeRule.setPriority(CheckstyleSeverityUtils.fromSeverity(value)); } else { - rule.setParameter(key, value); + activeRule.setParameter(key, value); } } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayProfile.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayProfile.java index 476ed1d7bd9..a189560a695 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayProfile.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayProfile.java @@ -22,10 +22,11 @@ package org.sonar.plugins.checkstyle; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.profiles.XMLProfileDefinition; import org.sonar.api.resources.Java; +import org.sonar.api.rules.RuleFinder; public final class SonarWayProfile extends XMLProfileDefinition { - public SonarWayProfile() { - super(RulesProfile.SONAR_WAY_NAME, Java.KEY, SunConventionsProfile.class.getClassLoader(), "org/sonar/plugins/checkstyle/profile-sonar-way.xml"); + public SonarWayProfile(RuleFinder ruleFinder) { + super(SunConventionsProfile.class.getClassLoader(), "org/sonar/plugins/checkstyle/profile-sonar-way.xml", ruleFinder); } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfile.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfile.java index bfa5b2a2213..398f137c451 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfile.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfile.java @@ -19,14 +19,27 @@ */ package org.sonar.plugins.checkstyle; +import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.profiles.XMLProfileDefinition; import org.sonar.api.resources.Java; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.utils.ValidationMessages; -public class SonarWayWithFindbugsProfile extends XMLProfileDefinition { +public class SonarWayWithFindbugsProfile extends ProfileDefinition { - public SonarWayWithFindbugsProfile() { - super(RulesProfile.SONAR_WAY_FINDBUGS_NAME, Java.KEY, SonarWayWithFindbugsProfile.class.getClassLoader(), "org/sonar/plugins/checkstyle/profile-sonar-way.xml"); + private SonarWayProfile sonarWay; + + public SonarWayWithFindbugsProfile(SonarWayProfile sonarWay) { + this.sonarWay = sonarWay; + } + + + @Override + public RulesProfile createProfile(ValidationMessages validationMessages) { + RulesProfile profile = sonarWay.createProfile(validationMessages); + profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME); + return profile; } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SunConventionsProfile.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SunConventionsProfile.java index fce303d924d..6351afed31c 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SunConventionsProfile.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/SunConventionsProfile.java @@ -22,10 +22,11 @@ package org.sonar.plugins.checkstyle; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.profiles.XMLProfileDefinition; import org.sonar.api.resources.Java; +import org.sonar.api.rules.RuleFinder; public final class SunConventionsProfile extends XMLProfileDefinition { - public SunConventionsProfile() { - super(RulesProfile.SUN_CONVENTIONS_NAME, Java.KEY, SunConventionsProfile.class.getClassLoader(), "org/sonar/plugins/checkstyle/profile-sun-conventions.xml"); + public SunConventionsProfile(RuleFinder ruleFinder) { + super(SunConventionsProfile.class.getClassLoader(), "org/sonar/plugins/checkstyle/profile-sun-conventions.xml", ruleFinder); } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sonar-way.xml b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sonar-way.xml index 429d236fcde..0f7bc465582 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sonar-way.xml +++ b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sonar-way.xml @@ -1,4 +1,6 @@ <profile>
+ <name>Sonar way</name>
+ <language>java</language>
<rules>
<rule>
<repositoryKey>checkstyle</repositoryKey>
diff --git a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sun-conventions.xml b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sun-conventions.xml index d6fe70f3ff5..9ec3209ab48 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sun-conventions.xml +++ b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/profile-sun-conventions.xml @@ -1,4 +1,6 @@ <profile>
+ <name>Sun checks</name>
+ <language>java</language>
<rules>
<rule>
<repositoryKey>checkstyle</repositoryKey>
diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java index 1e53bba97ab..8ad1a8c6b54 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java @@ -21,8 +21,10 @@ package org.sonar.plugins.checkstyle; import org.junit.Before; import org.junit.Test; -import org.sonar.api.profiles.ProfilePrototype; -import org.sonar.api.rules.RulePriority; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.*; import org.sonar.api.utils.ValidationMessages; import org.sonar.test.TestUtils; @@ -34,6 +36,10 @@ import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class CheckstyleProfileImporterTest { @@ -43,27 +49,36 @@ public class CheckstyleProfileImporterTest { @Before public void before() { messages = ValidationMessages.create(); - importer = new CheckstyleProfileImporter(); + + /* + + The mocked rule finder defines 2 rules : + + - JavadocCheck with 2 paramters format and ignore, default priority is MAJOR + - EqualsHashCodeCheck without parameters, default priority is BLOCKER + + */ + importer = new CheckstyleProfileImporter(newRuleFinder()); } @Test public void importSimpleProfile() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - assertThat(profile.getRules().size(), is(2)); - assertNotNull(profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode")); - assertNotNull(profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); + assertThat(profile.getActiveRules().size(), is(2)); + assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode")); + assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); assertThat(messages.hasErrors(), is(false)); } @Test public void importParameters() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - ProfilePrototype.RulePrototype javadocCheck = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); - assertThat(javadocCheck.getParameters().size(), is(2)); + ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + assertThat(javadocCheck.getActiveRuleParams().size(), is(2)); assertThat(javadocCheck.getParameter("format"), is("abcde")); assertThat(javadocCheck.getParameter("ignore"), is("true")); assertThat(javadocCheck.getParameter("severity"), nullValue()); // checkstyle internal parameter @@ -72,27 +87,27 @@ public class CheckstyleProfileImporterTest { @Test public void importPriorities() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - ProfilePrototype.RulePrototype javadocCheck = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); assertThat(javadocCheck.getPriority(), is(RulePriority.BLOCKER)); } @Test public void priorityIsOptional() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - ProfilePrototype.RulePrototype check = profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"); - assertThat(check.getPriority(), nullValue()); + ActiveRule activeRule= profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"); + assertThat(activeRule.getPriority(), is(RulePriority.BLOCKER)); // reuse the rule default priority } @Test public void idPropertyIsNotSupported() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idProperty.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - ProfilePrototype.RulePrototype check = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + ActiveRule check = profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); assertThat(check.getParameter("id"), nullValue()); assertThat(messages.getWarnings().size(), is(1)); } @@ -107,12 +122,36 @@ public class CheckstyleProfileImporterTest { @Test public void importingFiltersIsNotSupported() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml")); - ProfilePrototype profile = importer.importProfile(reader, messages); + RulesProfile profile = importer.importProfile(reader, messages); - assertNull(profile.getRuleByConfigKey("checkstyle", "Checker/SuppressionCommentFilter")); - assertNull(profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/FileContentsHolder")); - assertThat(profile.getRules().size(), is(2)); + assertNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/SuppressionCommentFilter")); + assertNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/FileContentsHolder")); + assertThat(profile.getActiveRules().size(), is(2)); assertThat(messages.getWarnings().size(), is(1)); // no warning for FileContentsHolder } + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.find((RuleQuery)anyObject())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + RuleQuery query = (RuleQuery)iom.getArguments()[0]; + Rule rule = null; + if (query.getConfigKey().equals("Checker/JavadocPackage")) { + rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc Package") + .setConfigKey("Checker/JavadocPackage") + .setPriority(RulePriority.MAJOR); + rule.createParameter("format"); + rule.createParameter("ignore"); + + } else if (query.getConfigKey().equals("Checker/TreeWalker/EqualsHashCode")) { + rule = Rule.create(query.getRepositoryKey(), "com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck", "Equals HashCode") + .setConfigKey("Checker/TreeWalker/EqualsHashCode") + .setPriority(RulePriority.BLOCKER); + } + return rule; + } + }); + return ruleFinder; + } + } diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayProfileTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayProfileTest.java index 697a9c6685c..268f94fe15b 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayProfileTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayProfileTest.java @@ -20,23 +20,39 @@ package org.sonar.plugins.checkstyle; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.sonar.api.profiles.ProfileDefinition; -import org.sonar.api.profiles.ProfilePrototype; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.utils.ValidationMessages; import static org.hamcrest.core.Is.is; import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SonarWayProfileTest { @Test public void create() { - ProfileDefinition sonarWay = new SonarWayProfile(); + ProfileDefinition sonarWay = new SonarWayProfile(newRuleFinder()); ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype prototype = sonarWay.createPrototype(validation); - assertThat(prototype.getRulesByRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).size(), greaterThan(1)); + RulesProfile profile = sonarWay.createProfile(validation); + assertThat(profile.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY).size(), greaterThan(1)); assertThat(validation.hasErrors(), is(false)); } + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>(){ + public Rule answer(InvocationOnMock iom) throws Throwable { + return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + } + }); + return ruleFinder; + } } diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfileTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfileTest.java index ae6513bbfa2..6ed6bbca83c 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfileTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SonarWayWithFindbugsProfileTest.java @@ -19,20 +19,40 @@ */ package org.sonar.plugins.checkstyle; -import org.hamcrest.core.Is; import org.junit.Test; -import org.sonar.api.profiles.ProfilePrototype; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.utils.ValidationMessages; +import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SonarWayWithFindbugsProfileTest { @Test public void sameAsSonarWay() { - ProfilePrototype withFindbugs = new SonarWayWithFindbugsProfile().createPrototype(ValidationMessages.create()); - ProfilePrototype withoutFindbugs = new SonarWayProfile().createPrototype(ValidationMessages.create()); - assertThat(withFindbugs.getRules().size(), is(withoutFindbugs.getRules().size())); + RuleFinder ruleFinder = newRuleFinder(); + SonarWayProfile sonarWay = new SonarWayProfile(ruleFinder); + RulesProfile withoutFindbugs = sonarWay.createProfile(ValidationMessages.create()); + RulesProfile withFindbugs = new SonarWayWithFindbugsProfile(sonarWay).createProfile(ValidationMessages.create()); + assertThat(withFindbugs.getActiveRules().size(), is(withoutFindbugs.getActiveRules().size())); + assertThat(withFindbugs.getName(), is(RulesProfile.SONAR_WAY_FINDBUGS_NAME)); } + + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + } + }); + return ruleFinder; + } + } diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SunConventionsProfileTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SunConventionsProfileTest.java index 73eaa38e8e8..ff80d97a02e 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SunConventionsProfileTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/SunConventionsProfileTest.java @@ -21,24 +21,38 @@ package org.sonar.plugins.checkstyle; import org.hamcrest.core.Is; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.sonar.api.profiles.ProfileDefinition; -import org.sonar.api.profiles.ProfilePrototype; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.utils.ValidationMessages; -import static org.hamcrest.Matchers.is; import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SunConventionsProfileTest { @Test public void create() { - ProfileDefinition sunConventionsProfile = new SunConventionsProfile(); + ProfileDefinition definition = new SunConventionsProfile(newRuleFinder()); ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype prototype = sunConventionsProfile.createPrototype(validation); - assertThat(prototype.getRulesByRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).size(), greaterThan(1)); - assertThat( - prototype.getRule(CheckstyleConstants.REPOSITORY_KEY, "com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck").getParameter("lineSeparator"), - is("system")); + RulesProfile sunProfile = definition.createProfile(validation); + assertThat(sunProfile.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY).size(), greaterThan(1)); assertThat(validation.hasErrors(), Is.is(false)); } + + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { + public Rule answer(InvocationOnMock iom) throws Throwable { + return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + } + }); + return ruleFinder; + } + } diff --git a/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml index 806ec61fae0..55a7faa374e 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml +++ b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/importingFiltersIsNotSupported.xml @@ -1,9 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <module name="Checker"> <module name="SuppressionCommentFilter"/> - <module name="NewlineAtEndOfFile"/> + <module name="JavadocPackage"/> <module name="TreeWalker"> <module name="FileContentsHolder"/> - <module name="InterfaceIsType"/> + <module name="EqualsHashCode"/> </module> </module>
\ No newline at end of file |