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/src/test | |
parent | ed770f36d376af5ef614f793865eb76713b4ff93 (diff) | |
download | sonarqube-cdd05f32ec75a1594f8f86e1fdb0bd4101598654.tar.gz sonarqube-cdd05f32ec75a1594f8f86e1fdb0bd4101598654.zip |
SONAR-1229 simplify the profiles API
Diffstat (limited to 'plugins/sonar-checkstyle-plugin/src/test')
5 files changed, 130 insertions, 41 deletions
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 |