diff options
author | fmallet <freddy.mallet@gmail.com> | 2010-09-17 11:51:49 +0000 |
---|---|---|
committer | fmallet <freddy.mallet@gmail.com> | 2010-09-17 11:51:49 +0000 |
commit | b4ba514185fae1283073f3cc9c0f7cffd6e69a04 (patch) | |
tree | 753ac64e9c595f6eb334c764d28f72323db0fea2 /plugins/sonar-findbugs-plugin/src/test | |
parent | ee5682e886da8b45bdc5966ac01cde393823342b (diff) | |
download | sonarqube-b4ba514185fae1283073f3cc9c0f7cffd6e69a04.tar.gz sonarqube-b4ba514185fae1283073f3cc9c0f7cffd6e69a04.zip |
fix SONAR-1480 : No way to export Findbugs rules in a XML file and then to reimport this file to create a new Quality profile
Moreover, the Findbugs plugin now uses the new Rule API
Diffstat (limited to 'plugins/sonar-findbugs-plugin/src/test')
19 files changed, 495 insertions, 604 deletions
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenPluginHandlerTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenPluginHandlerTest.java index 9b443c0c2ec..30bbd30d199 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenPluginHandlerTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenPluginHandlerTest.java @@ -52,7 +52,6 @@ public class FindbugsMavenPluginHandlerTest { private Project project;
private ProjectFileSystem fs;
private File fakeSonarConfig;
- private FindbugsRulesRepository repo;
private MavenPlugin plugin;
private FindbugsMavenPluginHandler handler;
@@ -61,7 +60,6 @@ public class FindbugsMavenPluginHandlerTest { project = mock(Project.class);
fs = mock(ProjectFileSystem.class);
fakeSonarConfig = mock(File.class);
- repo = mock(FindbugsRulesRepository.class);
plugin = mock(MavenPlugin.class);
handler = createMavenPluginHandler();
}
@@ -173,8 +171,7 @@ public class FindbugsMavenPluginHandlerTest { }
private FindbugsMavenPluginHandler createMavenPluginHandler() {
- when(repo.exportConfiguration((RulesProfile) anyObject())).thenReturn("<test/>");
- return new FindbugsMavenPluginHandler(new RulesProfile(), repo);
+ return new FindbugsMavenPluginHandler(RulesProfile.create(), new FindbugsProfileExporter());
}
private void mockProject(String effort) throws URISyntaxException, IOException {
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/xml/FindBugsFilterTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java index 384dc56882f..52b7c246781 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/xml/FindBugsFilterTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java @@ -1,124 +1,131 @@ -/*
- * 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.plugins.findbugs.xml;
-
-import org.apache.commons.io.IOUtils;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RulePriority;
-import org.sonar.plugins.findbugs.FindbugsRulePriorityMapper;
-import org.xml.sax.SAXException;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-public class FindBugsFilterTest extends FindBugsXmlTests {
-
- @Test
- public void shouldBuilXmlFromModuleTree() throws IOException, SAXException {
- FindBugsFilter root = buildModuleTreeFixture();
-
- String xml = root.toXml();
-
- assertXmlAreSimilar(xml, "test_module_tree.xml");
- }
-
- @Test
- public void shouldBuilModuleTreeFromXml() throws IOException {
- InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/test_module_tree.xml");
-
- FindBugsFilter module = FindBugsFilter.fromXml(IOUtils.toString(input));
-
- List<Match> matches = module.getMatchs();
- assertThat(matches.size(), is(2));
- assertChild(matches.get(0), "DLS_DEAD_LOCAL_STORE");
- assertChild(matches.get(1), "URF_UNREAD_FIELD");
- }
-
- private static FindBugsFilter buildModuleTreeFixture() {
- FindBugsFilter findBugsFilter = new FindBugsFilter();
- findBugsFilter.addMatch(new Match(new Bug("DLS_DEAD_LOCAL_STORE")));
- findBugsFilter.addMatch(new Match(new Bug("URF_UNREAD_FIELD")));
- return findBugsFilter;
- }
-
- private static final String DLS_DEAD_LOCAL_STORE = "DLS_DEAD_LOCAL_STORE";
- private static final String SS_SHOULD_BE_STATIC = "SS_SHOULD_BE_STATIC";
-
- @Test
- public void shouldBuildModuleWithProperties() {
- ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE);
- FindBugsFilter filter = FindBugsFilter.fromActiveRules(Arrays.asList(activeRule));
-
- assertThat(filter.getMatchs().size(), is(1));
- assertChild(filter.getMatchs().get(0), DLS_DEAD_LOCAL_STORE);
- }
-
- @Test
- public void shouldBuildOnlyOneModuleWhenNoActiveRules() {
- FindBugsFilter filter = FindBugsFilter.fromActiveRules(Collections.<ActiveRule>emptyList());
- assertThat(filter.getMatchs().size(), is(0));
- }
-
- @Test
- public void shouldBuildTwoModulesEvenIfSameTwoRulesActivated() {
- ActiveRule activeRule1 = anActiveRule(DLS_DEAD_LOCAL_STORE);
- ActiveRule activeRule2 = anActiveRule(SS_SHOULD_BE_STATIC);
- FindBugsFilter filter = FindBugsFilter.fromActiveRules(Arrays.asList(activeRule1, activeRule2));
-
- List<Match> matches = filter.getMatchs();
- assertThat(matches.size(), is(2));
-
- assertChild(matches.get(0), DLS_DEAD_LOCAL_STORE);
- assertChild(matches.get(1), SS_SHOULD_BE_STATIC);
- }
-
- @Test
- public void shouldBuildOnlyOneModuleWhenNoFindbugsActiveRules() {
- ActiveRule activeRule1 = anActiveRuleFromAnotherPlugin();
- ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin();
-
- FindBugsFilter filter = FindBugsFilter.fromActiveRules(Arrays.asList(activeRule1, activeRule2));
- assertThat(filter.getMatchs().size(), is(0));
- }
-
- private static ActiveRule anActiveRule(String configKey) {
- Rule rule = new Rule();
- rule.setConfigKey(configKey);
- rule.setPluginName(CoreProperties.FINDBUGS_PLUGIN);
- ActiveRule activeRule = new ActiveRule(null, rule, RulePriority.CRITICAL);
- return activeRule;
- }
-
- private static ActiveRule anActiveRuleFromAnotherPlugin() {
- Rule rule1 = new Rule();
- rule1.setPluginName("not-a-findbugs-plugin");
- ActiveRule activeRule1 = new ActiveRule(null, rule1, RulePriority.CRITICAL);
- return activeRule1;
- }
-
-}
+/* + * 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.plugins.findbugs; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RulePriority; +import org.sonar.plugins.findbugs.xml.Bug; +import org.sonar.plugins.findbugs.xml.FindBugsFilter; +import org.sonar.plugins.findbugs.xml.Match; +import org.xml.sax.SAXException; + +public class FindbugsProfileExporterTest extends FindbugsTests { + + private FindbugsProfileExporter exporter = new FindbugsProfileExporter(); + + @Test + public void shouldAddHeaderToExportedXml() throws IOException, SAXException { + RulesProfile profile = RulesProfile.create(); + + StringWriter xml = new StringWriter(); + exporter.exportProfile(profile, xml); + assertXmlAreSimilar(xml.toString(), "test_header.xml"); + } + + @Test + public void shouldExportConfiguration() throws IOException, SAXException { + List<Rule> rules = buildRulesFixture(); + List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(rules); + RulesProfile profile = RulesProfile.create(); + profile.setActiveRules(activeRulesExpected); + + StringWriter xml = new StringWriter(); + exporter.exportProfile(profile, xml); + assertXmlAreSimilar(xml.toString(), "test_xml_complete.xml"); + } + + @Test + public void shouldBuildOnlyOneModuleWhenNoActiveRules() { + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Collections.<ActiveRule> emptyList()); + assertThat(filter.getMatchs().size(), is(0)); + } + + @Test + public void shouldBuildTwoModulesEvenIfSameTwoRulesActivated() { + ActiveRule activeRule1 = anActiveRule(DLS_DEAD_LOCAL_STORE); + ActiveRule activeRule2 = anActiveRule(SS_SHOULD_BE_STATIC); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2)); + + List<Match> matches = filter.getMatchs(); + assertThat(matches.size(), is(2)); + + assertThat(matches.get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE")); + assertThat(matches.get(1).getBug().getPattern(), is("SS_SHOULD_BE_STATIC")); + } + + @Test + public void shouldBuildOnlyOneModuleWhenNoFindbugsActiveRules() { + ActiveRule activeRule1 = anActiveRuleFromAnotherPlugin(); + ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin(); + + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2)); + assertThat(filter.getMatchs().size(), is(0)); + } + + @Test + public void shouldBuildModuleWithProperties() { + ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule)); + + assertThat(filter.getMatchs().size(), is(1)); + assertThat(filter.getMatchs().get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE")); + } + + @Test + public void shouldBuilXmlFromModuleTree() throws IOException, SAXException { + FindBugsFilter findBugsFilter = new FindBugsFilter(); + findBugsFilter.addMatch(new Match(new Bug("DLS_DEAD_LOCAL_STORE"))); + findBugsFilter.addMatch(new Match(new Bug("URF_UNREAD_FIELD"))); + + String xml = findBugsFilter.toXml(); + + assertXmlAreSimilar(xml, "test_module_tree.xml"); + } + + private static final String DLS_DEAD_LOCAL_STORE = "DLS_DEAD_LOCAL_STORE"; + private static final String SS_SHOULD_BE_STATIC = "SS_SHOULD_BE_STATIC"; + + private static ActiveRule anActiveRule(String configKey) { + Rule rule = Rule.create(); + rule.setConfigKey(configKey); + rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); + ActiveRule activeRule = RulesProfile.create().activateRule(rule, RulePriority.CRITICAL); + return activeRule; + } + + private static ActiveRule anActiveRuleFromAnotherPlugin() { + Rule rule = Rule.create(); + rule.setPluginName("not-a-findbugs-plugin"); + ActiveRule activeRule = RulesProfile.create().activateRule(rule, RulePriority.CRITICAL); + return activeRule; + } +} diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java new file mode 100644 index 00000000000..b829ce9abf1 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java @@ -0,0 +1,144 @@ +/* + * 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.plugins.findbugs; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.plugins.findbugs.xml.FindBugsFilter; +import org.sonar.plugins.findbugs.xml.Match; +import org.sonar.test.TestUtils; + +import com.thoughtworks.xstream.XStream; + +public class FindbugsProfileImporterTest { + + private FindbugsProfileImporter importer = new FindbugsProfileImporter(new FindbugsRuleFinder()); + + @Test + public void shouldImportPatterns() throws IOException { + String findbugsConf = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/shouldImportPatterns.xml"); + RulesProfile profile = importer.importProfile(new StringReader(findbugsConf), ValidationMessages.create()); + + assertThat(profile.getActiveRules().size(), is(2)); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "NP_CLOSING_NULL"), is(notNullValue())); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE"), is(notNullValue())); + } + + @Test + public void shouldImportCodes() throws IOException { + InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/shouldImportCodes.xml"); + RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create()); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(18)); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "EC_INCOMPATIBLE_ARRAY_COMPARE"), is(notNullValue())); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY"), is(notNullValue())); + } + + @Test + public void shouldImportCategories() throws IOException { + InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/shouldImportCategories.xml"); + RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create()); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(183)); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE"), is(notNullValue())); + } + + @Test + public void shouldImportConfigurationBugInclude() throws IOException { + InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/findbugs-include.xml"); + RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create()); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(11)); + assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE"), is(notNullValue())); + } + + @Test + public void shouldBuilModuleTreeFromXml() throws IOException { + InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/test_module_tree.xml"); + + XStream xStream = FindBugsFilter.createXStream(); + FindBugsFilter filter = (FindBugsFilter) xStream.fromXML(IOUtils.toString(input)); + + List<Match> matches = filter.getMatchs(); + assertThat(matches.size(), is(2)); + assertThat(matches.get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE")); + assertThat(matches.get(1).getBug().getPattern(), is("URF_UNREAD_FIELD")); + } + + @Test + public void testImportingUncorrectXmlFile() throws IOException { + String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml"); + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(0)); + assertThat(messages.getErrors().size(), is(1)); + } + + @Test + public void testImportingXmlFileWithUnknownRule() throws IOException { + String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownRule.xml"); + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(1)); + assertThat(messages.getWarnings().size(), is(1)); + } + + @Test + public void testImportingXmlFileWithUnknownCategory() throws IOException { + String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml"); + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(142)); + assertThat(messages.getWarnings().size(), is(1)); + } + + @Test + public void testImportingXmlFileWithUnknownCode() throws IOException { + String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml"); + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages); + List<ActiveRule> results = profile.getActiveRules(); + + assertThat(results.size(), is(9)); + assertThat(messages.getWarnings().size(), is(1)); + } +} diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java new file mode 100644 index 00000000000..44c279b4f6e --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java @@ -0,0 +1,57 @@ +/* + * 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.plugins.findbugs; + +import java.util.Collection; +import java.util.List; + +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RuleQuery; + +public class FindbugsRuleFinder implements RuleFinder { + + private final List<Rule> findbugsRules; + + public FindbugsRuleFinder() { + FindbugsRuleRepository repo = new FindbugsRuleRepository(); + findbugsRules = repo.createRules(); + for(Rule rule : findbugsRules){ + rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); + } + } + + public Rule findByKey(String repositoryKey, String key) { + for (Rule rule : findbugsRules) { + if (rule.getKey().equals(key)) { + return rule; + } + } + return null; + } + + public Rule find(RuleQuery query) { + throw new UnsupportedOperationException(); + } + + public Collection<Rule> findAll(RuleQuery query) { + return findbugsRules; + } +}
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/xml/FindBugsXmlTests.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java index 907679f880a..d8c9ff8a32c 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/xml/FindBugsXmlTests.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java @@ -1,33 +1,45 @@ -/*
- * 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.plugins.findbugs.xml;
-
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.*;
-import org.sonar.plugins.findbugs.FindbugsTests;
-
-public class FindBugsXmlTests extends FindbugsTests {
-
- protected static void assertChild(Match child, String configKey) {
- Bug bug = child.getBug();
- assertNotNull(bug);
- assertThat(bug.getPattern(), is(configKey));
- }
-}
\ No newline at end of file +/* + * 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.plugins.findbugs; + +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.sonar.api.rules.Rule; + +public class FindbugsRuleRepositoryTest { + + @Test + public void testLoadRepositoryFromXml() { + FindbugsRuleRepository repository = new FindbugsRuleRepository(); + List<Rule> rules = repository.createRules(); + assertThat(rules.size(), greaterThan(300)); + for (Rule rule : rules) { + assertNotNull(rule.getKey()); + assertNotNull(rule.getDescription()); + assertNotNull(rule.getConfigKey()); + assertNotNull(rule.getName()); + } + } +} diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest.java deleted file mode 100644 index 2033a209f43..00000000000 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest.java +++ /dev/null @@ -1,186 +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.plugins.findbugs; - -import org.apache.commons.io.IOUtils; -import org.hamcrest.BaseMatcher; -import static org.hamcrest.CoreMatchers.is; -import org.hamcrest.Description; -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import org.sonar.api.CoreProperties; -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.RulePriority; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class FindbugsRulesRepositoryTest extends FindbugsTests { - - private FindbugsRulesRepository repository; - - @Before - public void setup() { - repository = new FindbugsRulesRepository(new Java()); - } - - @Test - public void rulesAreDefinedWithTheDefaultSonarXmlFormat() { - List<Rule> rules = repository.getInitialReferential(); - assertTrue(rules.size() > 0); - for (Rule rule : rules) { - assertNotNull(rule.getKey()); - assertNotNull(rule.getDescription()); - assertNotNull(rule.getConfigKey()); - assertNotNull(rule.getName()); - } - } - - @Test - @Ignore - public void shouldProvideProfiles() { - List<RulesProfile> profiles = repository.getProvidedProfiles(); - assertThat(profiles.size(), is(1)); - - RulesProfile profile1 = profiles.get(0); - assertThat(profile1.getName(), is(RulesProfile.SONAR_WAY_FINDBUGS_NAME)); - assertEquals(profile1.getActiveRules().size(), 344); - } - - @Test - public void shouldAddHeaderToExportedXml() throws IOException, SAXException { - RulesProfile rulesProfile = mock(RulesProfile.class); - when(rulesProfile.getActiveRulesByPlugin(CoreProperties.FINDBUGS_PLUGIN)).thenReturn(Collections.<ActiveRule>emptyList()); - - assertXmlAreSimilar(repository.exportConfiguration(rulesProfile), "test_header.xml"); - } - - @Test - public void shouldExportConfiguration() throws IOException, SAXException { - List<Rule> rules = buildRulesFixture(); - List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(rules); - RulesProfile activeProfile = new RulesProfile(); - activeProfile.setActiveRules(activeRulesExpected); - - assertXmlAreSimilar(repository.exportConfiguration(activeProfile), "test_xml_complete.xml"); - } - - @Test - public void shouldImportPatterns() throws IOException { - InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportPatterns.xml"); - List<ActiveRule> results = repository.importConfiguration(IOUtils.toString(input), buildRulesFixtureImport()); - - assertThat(results.size(), is(2)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_1", RulePriority.MAJOR)); - assertThat(results, new ContainsActiveRule("FB2_IMPORT_TEST_4", RulePriority.MAJOR)); - } - - @Test - public void shouldImportCodes() throws IOException { - InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCodes.xml"); - List<ActiveRule> results = repository.importConfiguration(IOUtils.toString(input), buildRulesFixtureImport()); - - assertThat(results.size(), is(4)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_1", RulePriority.MAJOR)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_2", RulePriority.MAJOR)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_3", RulePriority.MAJOR)); - assertThat(results, new ContainsActiveRule("FB3_IMPORT_TEST_5", RulePriority.MAJOR)); - } - - @Test - public void shouldImportCategories() throws IOException { - InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCategories.xml"); - List<ActiveRule> results = repository.importConfiguration(IOUtils.toString(input), buildRulesFixtureImport()); - - assertThat(results.size(), is(4)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_1", RulePriority.INFO)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_2", RulePriority.INFO)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_3", RulePriority.INFO)); - assertThat(results, new ContainsActiveRule("FB2_IMPORT_TEST_4", RulePriority.INFO)); - } - - @Test - public void shouldImportConfigurationBugInclude() throws IOException { - InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/findbugs-include.xml"); - List<ActiveRule> results = repository.importConfiguration(IOUtils.toString(input), buildRulesFixtureImport()); - - assertThat(results.size(), is(4)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_1", null)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_2", null)); - assertThat(results, new ContainsActiveRule("FB1_IMPORT_TEST_3", null)); - assertThat(results, new ContainsActiveRule("FB2_IMPORT_TEST_4", null)); - } - - private static List<Rule> buildRulesFixtureImport() { - Rule rule1 = new Rule("Correctness - Import test 1 group 1", "FB1_IMPORT_TEST_1", - "FB1_IMPORT_TEST_1", null, CoreProperties.FINDBUGS_PLUGIN, null); - - Rule rule2 = new Rule("Multithreaded correctness - Import test 2 group 1", "FB1_IMPORT_TEST_2", - "FB1_IMPORT_TEST_2", null, CoreProperties.FINDBUGS_PLUGIN, null); - - Rule rule3 = new Rule("Multithreaded correctness - Import test 3 group 1", "FB1_IMPORT_TEST_3", - "FB1_IMPORT_TEST_3", null, CoreProperties.FINDBUGS_PLUGIN, null); - - Rule rule4 = new Rule("Multithreaded correctness - Import test 4 group 2", "FB2_IMPORT_TEST_4", - "FB2_IMPORT_TEST_4", null, CoreProperties.FINDBUGS_PLUGIN, null); - - Rule rule5 = new Rule("Style - Import test 5 group 3", "FB3_IMPORT_TEST_5", - "FB3_IMPORT_TEST_5", null, CoreProperties.FINDBUGS_PLUGIN, null); - - return Arrays.asList(rule1, rule2, rule3, rule4, rule5); - } -} - -class ContainsActiveRule extends BaseMatcher<List<ActiveRule>> { - private String key; - private RulePriority priority; - - ContainsActiveRule(String key, RulePriority priority) { - this.key = key; - this.priority = priority; - } - - public boolean matches(Object o) { - List<ActiveRule> rules = (List<ActiveRule>) o; - for (ActiveRule rule : rules) { - if (rule.getRule().getKey().equals(key)) { - if (priority == null) { - return true; - } - return rule.getPriority().equals(priority); - } - } - return false; - } - - public void describeTo(Description description) { - } -} diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java index feed2b855a9..0e74508fee9 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java @@ -38,6 +38,7 @@ import org.apache.maven.project.MavenProject; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.batch.SensorContext; +import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.DefaultProjectFileSystem; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; @@ -49,22 +50,21 @@ public class FindbugsSensorTest extends FindbugsTests { @Test public void shouldExecuteWhenSomeRulesAreActive() throws Exception { - FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), createRulesManager(), null); + FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); Project project = createProject(); assertTrue(sensor.shouldExecuteOnProject(project)); } @Test public void shouldNotExecuteWhenNoRulesAreActive() throws Exception { - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithoutActiveRules(), createRulesManager(), null); + FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), null); Project pom = createProject(); assertFalse(analyser.shouldExecuteOnProject(pom)); } @Test public void testGetMavenPluginHandlerWhenFindbugsReportPathExists() throws Exception { - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithoutActiveRules(), createRulesManager(), - mock(FindbugsMavenPluginHandler.class)); + FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), mock(FindbugsMavenPluginHandler.class)); Project pom = createProject(); Configuration conf = mock(Configuration.class); when(conf.getString(CoreProperties.FINDBUGS_REPORT_PATH)).thenReturn("pathToFindbugsReport"); @@ -74,8 +74,7 @@ public class FindbugsSensorTest extends FindbugsTests { @Test public void testGetFindbugsReport() { - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), createRulesManager(), - null); + FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); Project pom = createProject(); Configuration conf = mock(Configuration.class); when(pom.getConfiguration()).thenReturn(conf); @@ -89,7 +88,7 @@ public class FindbugsSensorTest extends FindbugsTests { public void shouldNotExecuteOnEar() { Project project = createProject(); when(project.getPom().getPackaging()).thenReturn("ear"); - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), createRulesManager(), null); + FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); assertFalse(analyser.shouldExecuteOnProject(project)); } @@ -104,21 +103,18 @@ public class FindbugsSensorTest extends FindbugsTests { when(project.getConfiguration()).thenReturn(conf); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithoutActiveRules(), createRulesManager(), - null); + FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), null); analyser.analyse(project, context); verify(context, times(3)).saveViolation(any(Violation.class)); - Violation wanted = new Violation(null, new JavaFile("org.sonar.commons.ZipUtils")) - .setMessage("Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)") - .setLineId(107); + Violation wanted = new Violation(null, new JavaFile("org.sonar.commons.ZipUtils")).setMessage( + "Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)").setLineId(107); verify(context).saveViolation(argThat(new IsViolation(wanted))); - wanted = new Violation(null, new JavaFile("org.sonar.commons.resources.MeasuresDao")) - .setMessage("The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class") - .setLineId(56); + wanted = new Violation(null, new JavaFile("org.sonar.commons.resources.MeasuresDao")).setMessage( + "The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class").setLineId(56); verify(context).saveViolation(argThat(new IsViolation(wanted))); } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java index 88afed2cdcd..e051e7c52a7 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java @@ -19,45 +19,44 @@ */
package org.sonar.plugins.findbugs;
-import org.apache.commons.io.IOUtils;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.CoreProperties;
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.RulePriority;
+import org.sonar.api.rules.RuleQuery;
import org.sonar.api.rules.RulesManager;
import org.sonar.test.TestUtils;
import org.xml.sax.SAXException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
public abstract class FindbugsTests {
protected void assertXmlAreSimilar(String actualContent, String expectedFileName) throws IOException, SAXException {
- InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/" + expectedFileName);
- String expectedContent = IOUtils.toString(input);
+ String expectedContent = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/" + expectedFileName);
TestUtils.assertSimilarXml(expectedContent, actualContent);
}
protected List<Rule> buildRulesFixture() {
List<Rule> rules = new ArrayList<Rule>();
- Rule rule1 = new Rule("DLS: Dead store to local variable", "DLS_DEAD_LOCAL_STORE",
- "DLS_DEAD_LOCAL_STORE", null, CoreProperties.FINDBUGS_PLUGIN, null);
+ Rule rule1 = new Rule("DLS: Dead store to local variable", "DLS_DEAD_LOCAL_STORE", "DLS_DEAD_LOCAL_STORE", null,
+ CoreProperties.FINDBUGS_PLUGIN, null);
- Rule rule2 = new Rule("UrF: Unread field", "URF_UNREAD_FIELD",
- "URF_UNREAD_FIELD", null, CoreProperties.FINDBUGS_PLUGIN, null);
+ Rule rule2 = new Rule("UrF: Unread field", "URF_UNREAD_FIELD", "URF_UNREAD_FIELD", null, CoreProperties.FINDBUGS_PLUGIN, null);
rules.add(rule1);
rules.add(rule2);
@@ -74,11 +73,11 @@ public abstract class FindbugsTests { return activeRules;
}
-
protected RulesManager createRulesManager() {
RulesManager rulesManager = mock(RulesManager.class);
when(rulesManager.getPluginRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer<Rule>() {
+
public Rule answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
Rule rule = new Rule();
@@ -91,25 +90,13 @@ public abstract class FindbugsTests { }
protected RulesProfile createRulesProfileWithActiveRules() {
- RulesProfile rulesProfile = mock(RulesProfile.class);
- when(rulesProfile.getActiveRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer<ActiveRule>() {
- public ActiveRule answer(InvocationOnMock invocationOnMock) throws Throwable {
- Object[] args = invocationOnMock.getArguments();
- ActiveRule activeRule = mock(ActiveRule.class);
- when(activeRule.getPluginName()).thenReturn((String) args[0]);
- when(activeRule.getRuleKey()).thenReturn((String) args[1]);
- when(activeRule.getPriority()).thenReturn(RulePriority.CRITICAL);
- return activeRule;
- }
- });
- when(rulesProfile.getActiveRulesByPlugin(CoreProperties.FINDBUGS_PLUGIN)).thenReturn(Arrays.asList(new ActiveRule()));
- return rulesProfile;
- }
-
- protected RulesProfile createRulesProfileWithoutActiveRules() {
- RulesProfile rulesProfile = new RulesProfile();
- List<ActiveRule> list = new ArrayList<ActiveRule>();
- rulesProfile.setActiveRules(list);
- return rulesProfile;
+ RulesProfile profile = RulesProfile.create();
+ profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME);
+ profile.setLanguage(Java.KEY);
+ for (Rule rule : new FindbugsRuleRepository().createRules()) {
+ rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
+ profile.activateRule(rule, null);
+ }
+ return profile;
}
}
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java new file mode 100644 index 00000000000..0d1e3b73112 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java @@ -0,0 +1,41 @@ +/* + * 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.plugins.findbugs; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.OrderingComparisons.greaterThan; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.utils.ValidationMessages; + +public class SonarWayWithFindbugsProfileTest { + + @Test + public void create() { + FindbugsProfileImporter importer = new FindbugsProfileImporter(new FindbugsRuleFinder()); + SonarWayWithFindbugsProfile sonarWayWithFindbugs = new SonarWayWithFindbugsProfile(importer); + ValidationMessages validation = ValidationMessages.create(); + RulesProfile profile = sonarWayWithFindbugs.createProfile(validation); + assertThat(profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY).size(), greaterThan(300)); + assertThat(validation.hasErrors(), is(false)); + } +} diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/tools/RulesGenerator.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/tools/RulesGenerator.java deleted file mode 100644 index d3701ed66cc..00000000000 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/tools/RulesGenerator.java +++ /dev/null @@ -1,190 +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.plugins.findbugs.tools; - -import org.apache.commons.lang.StringUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.staxmate.in.SMHierarchicCursor; -import org.codehaus.staxmate.in.SMInputCursor; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RulesCategory; -import org.sonar.api.rules.StandardRulesXmlParser; -import org.sonar.api.utils.StaxParser; - -import javax.xml.stream.XMLStreamException; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class RulesGenerator { - - private static final String FOR_VERSION = "1.3.8"; - - - private final static Map<String, String> BUG_CATEGS = new HashMap<String, String>(); - - static { - BUG_CATEGS.put("STYLE", "Usability"); - BUG_CATEGS.put("NOISE", "Reliability"); - BUG_CATEGS.put("CORRECTNESS", "Reliability"); - BUG_CATEGS.put("SECURITY", "Reliability"); - BUG_CATEGS.put("BAD_PRACTICE", "Maintainability"); - BUG_CATEGS.put("MT_CORRECTNESS", "Reliability"); - BUG_CATEGS.put("PERFORMANCE", "Efficiency"); - BUG_CATEGS.put("I18N", "Portability"); - BUG_CATEGS.put("MALICIOUS_CODE", "Reliability"); - } - - public static void main(String[] args) throws Exception { - List<FindBugsBug> bugs = getBugsToImport(); - String generatedXML = parseMessages(bugs); - File out = new File(".", "rules.xml"); - IOUtil.copy(generatedXML.getBytes(), new FileOutputStream(out)); - System.out.println("Written to " + out.getPath()); - } - - private static List<FindBugsBug> getBugsToImport() throws MalformedURLException, IOException, XMLStreamException { - URL messages = new URL("http://findbugs.googlecode.com/svn/branches/" + FOR_VERSION + "/findbugs/etc/findbugs.xml"); - InputStream in = messages.openStream(); - final List<FindBugsBug> bugs = new ArrayList<FindBugsBug>(); - StaxParser p = new StaxParser(new StaxParser.XmlStreamHandler() { - - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); - SMInputCursor bugPatterns = rootCursor.descendantElementCursor("BugPattern"); - collectBugDefs(bugs, bugPatterns); - } - - private void collectBugDefs(final List<FindBugsBug> bugs, SMInputCursor bugPatterns) throws XMLStreamException { - while (bugPatterns.getNext() != null) { - if (bugPatterns.asEvent().isEndElement()) continue; - - String experimental = bugPatterns.getAttrValue("experimental"); - boolean isExperimental = (StringUtils.isNotEmpty(experimental) && Boolean.valueOf(experimental)) || bugPatterns.getAttrValue("category").equals("EXPERIMENTAL"); - String deprecated = bugPatterns.getAttrValue("deprecated"); - boolean isDeprecated = StringUtils.isNotEmpty(deprecated) && Boolean.valueOf(deprecated); - if (!isExperimental && !isDeprecated) { - bugs.add(new FindBugsBug(bugPatterns.getAttrValue("category"), bugPatterns.getAttrValue("type"))); - } - } - } - }); - p.parse(in); - in.close(); - return bugs; - } - - private static String parseMessages(final List<FindBugsBug> bugs) throws MalformedURLException, IOException, XMLStreamException { - URL messages = new URL("http://findbugs.googlecode.com/svn/branches/" + FOR_VERSION + "/findbugs/etc/messages.xml"); - - InputStream in = messages.openStream(); - final List<Rule> rules = new ArrayList<Rule>(); - StaxParser p = new StaxParser(new StaxParser.XmlStreamHandler() { - - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); - Map<String, String> bugCategoriesDecr = new HashMap<String, String>(); - SMInputCursor childrens = rootCursor.childElementCursor(); - while (childrens.getNext() != null) { - if (childrens.asEvent().isEndElement()) continue; - if (childrens.getLocalName().equals("BugCategory")) { - String bugCateg = childrens.getAttrValue("category"); - bugCategoriesDecr.put(bugCateg, childrens.childElementCursor("Description").advance().collectDescendantText()); - } else if (childrens.getLocalName().equals("BugPattern")) { - String bugType = childrens.getAttrValue("type"); - FindBugsBug bug = getFindBugsBugByType(bugType, bugs); - if (bug == null) continue; - - rules.add(getRuleForBug(bugType, bug, bugCategoriesDecr, childrens)); - } - } - } - }); - - p.parse(in); - in.close(); - StandardRulesXmlParser parser = new StandardRulesXmlParser(); - return parser.toXml(rules); - } - - private static Rule getRuleForBug(String bugType, FindBugsBug bug, - Map<String, String> bugCategoriesDecr, SMInputCursor childrens) throws XMLStreamException { - Rule rule = new Rule(); - rule.setKey(bugType); - rule.setConfigKey(bugType); - - String rulesCateg = BUG_CATEGS.get(bug.getCategory()); - if (StringUtils.isEmpty(rulesCateg)) { - throw new RuntimeException("Rules cat not found " + bug.getCategory()); - } - rule.setRulesCategory(new RulesCategory(rulesCateg)); - - SMInputCursor descendents = childrens.childElementCursor(); - while (descendents.getNext() != null) { - if (descendents.asEvent().isStartElement()) { - if (descendents.getLocalName().equals("ShortDescription")) { - String categName = bugCategoriesDecr.get(bug.getCategory()); - if (StringUtils.isEmpty(categName)) throw new RuntimeException("Cat not found " + bug.getCategory()); - rule.setName(categName + " - " + descendents.collectDescendantText()); - } else if (descendents.getLocalName().equals("Details")) { - rule.setDescription(descendents.collectDescendantText()); - } - } - } - return rule; - } - - private static FindBugsBug getFindBugsBugByType(String type, List<FindBugsBug> bugs) { - for (FindBugsBug findBugsBug : bugs) { - if (findBugsBug.getType().equals(type)) { - return findBugsBug; - } - } - return null; - } - - private static class FindBugsBug { - private String category; - private String type; - - public FindBugsBug(String category, String type) { - super(); - this.category = category; - this.type = type; - } - - public String getCategory() { - return category; - } - - public String getType() { - return type; - } - - } - -} diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportPatterns.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportPatterns.xml deleted file mode 100644 index 072a4b077ce..00000000000 --- a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportPatterns.xml +++ /dev/null @@ -1,9 +0,0 @@ -<FindBugsFilter> - <Match> - <Or> - <Bug pattern="FB1_IMPORT_TEST_1"/> - <Bug pattern="FB2_IMPORT_TEST_4"/> - </Or> - <Priority value="2"/> - </Match> -</FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml index 32e2e8f516c..9fc59110054 100644 --- a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugs-include.xml @@ -5,7 +5,7 @@ <Match> <Class name="com.foobar.ClassWithSomeBugsMatched" /> - <Bug code="FB1" /> + <Bug code="NP_CLOSING_NULL" /> <Priority value="2" /> </Match> @@ -13,7 +13,7 @@ <Class name="com.foobar.MyClass" /> <Method name="someMethod" /> <Local name="maxArgs" /> - <Bug pattern="FB1_IMPORT_TEST_3" /> + <Bug pattern="RC_REF_COMPARISON_BAD_PRACTICE" /> <Priority value="1" /> </Match> @@ -30,7 +30,7 @@ <Method name="frob" params="int,java.lang.String" returns="void" /> <Method name="blat" params="" returns="boolean" /> </Or> - <Bug category="MT_CORRECTNESS" /> + <Bug category="SECURITY" /> <Priority value="3" /> </Match> </FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml new file mode 100644 index 00000000000..33115c28621 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml @@ -0,0 +1,6 @@ +<FindBugsFilter>
+ <Match>
+ <Bug category="CORRECTNESS,MT_CORRECTN9876976" />
+ <Priority value="3"/>
+ </Match>
+</FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml new file mode 100644 index 00000000000..874d9c570aa --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml @@ -0,0 +1,6 @@ +<FindBugsFilter>
+ <Match>
+ <Bug code="BC,EC87LK"/>
+ <Priority value="2"/>
+ </Match>
+</FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownRule.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownRule.xml new file mode 100644 index 00000000000..4e542bbcba7 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXmlWithUnknownRule.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<FindBugsFilter>
+ <Match>
+ <Bug pattern="DLS_DEAD_LOCAL_STORE" />
+ </Match>
+ <Match>
+ <Bug pattern="UNKNOWN" />
+ </Match>
+</FindBugsFilter>
diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCategories.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportCategories.xml index 5f4ae48efa2..5f4ae48efa2 100644 --- a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCategories.xml +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportCategories.xml diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCodes.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportCodes.xml index 0da831362e5..f079f7dabbd 100644 --- a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/FindbugsRulesRepositoryTest/shouldImportCodes.xml +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportCodes.xml @@ -1,6 +1,6 @@ <FindBugsFilter> <Match> - <Bug code="FB1,FB3"/> + <Bug code="BC,EC"/> <Priority value="2"/> </Match> </FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml new file mode 100644 index 00000000000..b454f431b93 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/shouldImportPatterns.xml @@ -0,0 +1,9 @@ +<FindBugsFilter> + <Match> + <Or> + <Bug pattern="NP_CLOSING_NULL"/> + <Bug pattern="RC_REF_COMPARISON_BAD_PRACTICE"/> + </Or> + <Priority value="2"/> + </Match> +</FindBugsFilter>
\ No newline at end of file diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml new file mode 100644 index 00000000000..23b9b37a5d2 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml @@ -0,0 +1,5 @@ +<BugCollection timestamp='1282919233000' analysisTimestamp='1282919402891' sequence='0' release='' version='1.3.9'> + <Project projectName=''> + <Jar>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target/classes</Jar> + <AuxClasspathEntry>/Users/freddy/.m2/repository/org/apache/maven/reporting/maven-reporting-impl/2.0/maven-reporting-impl-2.0.jar</AuxClasspathEntry> +
\ No newline at end of file |