diff options
Diffstat (limited to 'sonar-plugin-api/src/test/java/org')
4 files changed, 72 insertions, 69 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java index 98442f40957..45e6bee0be0 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/AnnotationProfileDefinitionTest.java @@ -20,6 +20,10 @@ package org.sonar.api.profiles; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.ValidationMessages; import org.sonar.check.BelongsToProfile; @@ -32,15 +36,25 @@ import java.util.Collection; import static org.hamcrest.Matchers.is; 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 AnnotationProfileDefinitionTest { @Test public void importProfile() { - ProfileDefinition definition = new FakeDefinition(); + 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]); + } + }); + + ProfileDefinition definition = new FakeDefinition(ruleFinder); ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype profile = definition.createPrototype(validation); - assertThat(profile.getRule("squid", "fake").getPriority(), is(RulePriority.BLOCKER)); + RulesProfile profile = definition.createProfile(validation); + assertThat(profile.getActiveRule("squid", "fake").getPriority(), is(RulePriority.BLOCKER)); assertThat(validation.hasErrors(), is(false)); } } @@ -54,7 +68,7 @@ class FakeRule { class FakeDefinition extends AnnotationProfileDefinition { - public FakeDefinition() { - super("squid", "sonar way", "java", Arrays.<Class>asList(FakeRule.class)); + public FakeDefinition(RuleFinder ruleFinder) { + super("squid", "sonar way", "java", Arrays.<Class>asList(FakeRule.class), ruleFinder); } }
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java index 13e5eb7ea5c..f13af1eaae2 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfileImporterTest.java @@ -30,10 +30,10 @@ import static org.junit.Assert.assertThat; public class ProfileImporterTest { @Test - public void testSupportedRepositories() { + public void testSupportedLanguages() { ProfileImporter inmporter = new ProfileImporter("all", "All") { @Override - public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { + public RulesProfile importProfile(Reader reader, ValidationMessages messages) { return null; } }; @@ -45,10 +45,10 @@ public class ProfileImporterTest { } @Test - public void supportAllRepositories() { + public void supportAllLanguages() { ProfileImporter importer = new ProfileImporter("all", "All") { @Override - public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { + public RulesProfile importProfile(Reader reader, ValidationMessages messages) { return null; } }; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfilePrototypeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfilePrototypeTest.java deleted file mode 100644 index b8f87759d7c..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/ProfilePrototypeTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.api.profiles; - -import org.junit.Test; -import org.sonar.api.rules.RulePriority; - -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNull.nullValue; -import static org.junit.Assert.assertThat; - -public class ProfilePrototypeTest { - - @Test - public void addRuleByKey() { - ProfilePrototype profile = ProfilePrototype.create(); - profile.activateRule("checkstyle", "JavadocCheck", RulePriority.MINOR); - profile.activateRule("checkstyle", "EqualsHashCodeCheck", RulePriority.BLOCKER); - profile.activateRule("findbugs", "DetectNullPointer", RulePriority.BLOCKER); - - assertThat(profile.getRules().size(), is(3)); - assertThat(profile.getRulesByRepositoryKey("checkstyle").size(), is(2)); - assertThat(profile.getRulesByRepositoryKey("pmd").size(), is(0)); - assertThat(profile.getRule("findbugs", "DetectNullPointer"), not(nullValue())); - assertThat(profile.getRule("findbugs", "DetectNullPointer").getPriority(), is(RulePriority.BLOCKER)); - } - - @Test - public void addRuleByConfigKey() { - ProfilePrototype profile = ProfilePrototype.create(); - profile.activateRule(ProfilePrototype.RulePrototype.createByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode")); - - assertThat(profile.getRules().size(), is(1)); - assertThat(profile.getRule("checkstyle", "Checker/TreeWalker/EqualsHashCode"), nullValue()); - assertThat(profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"), not(nullValue())); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java index 50b9919fef9..3fe4ef39902 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java @@ -22,6 +22,11 @@ package org.sonar.api.profiles; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharEncoding; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.ValidationMessages; @@ -29,9 +34,13 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertNotNull; 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 XMLProfileImporterTest { @@ -40,11 +49,14 @@ public class XMLProfileImporterTest { Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfile.xml"), CharEncoding.UTF_8); try { ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype profile = XMLProfileImporter.create().importProfile(reader, validation); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); + assertThat(profile.getLanguage(), is("java")); + assertThat(profile.getName(), is("sonar way")); assertThat(validation.hasErrors(), is(false)); assertNotNull(profile); - assertThat(profile.getRule("checkstyle", "IllegalRegexp").getPriority(), is(RulePriority.CRITICAL)); + assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getPriority(), is(RulePriority.CRITICAL)); } finally { IOUtils.closeQuietly(reader); @@ -56,10 +68,12 @@ public class XMLProfileImporterTest { Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithRuleParameters.xml"), CharEncoding.UTF_8); try { ValidationMessages validation = ValidationMessages.create(); - ProfilePrototype profile = XMLProfileImporter.create().importProfile(reader, validation); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); assertThat(validation.hasErrors(), is(false)); - ProfilePrototype.RulePrototype rule = profile.getRule("checkstyle", "IllegalRegexp"); + assertThat(validation.hasWarnings(), is(false)); + ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); assertThat(rule.getParameter("format"), is("foo")); assertThat(rule.getParameter("message"), is("with special characters < > &")); @@ -67,4 +81,34 @@ public class XMLProfileImporterTest { IOUtils.closeQuietly(reader); } } -} + + @Test + public void importProfileWithUnknownRuleParameter() throws UnsupportedEncodingException { + Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithUnknownRuleParameter.xml"), CharEncoding.UTF_8); + try { + ValidationMessages validation = ValidationMessages.create(); + RuleFinder ruleFinder = newRuleFinder(); + RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation); + + assertThat(validation.getWarnings().size(), is(1)); + ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); + assertThat(rule.getParameter("unknown"), nullValue()); + + } finally { + IOUtils.closeQuietly(reader); + } + } + + private RuleFinder newRuleFinder() { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>(){ + public Rule answer(InvocationOnMock iom) throws Throwable { + Rule rule = Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); + rule.createParameter("format"); + rule.createParameter("message"); + return rule; + } + }); + return ruleFinder; + } +}
\ No newline at end of file |