From a8a68caceaeb12c3c3cb2c22adbc41e73f3a5a04 Mon Sep 17 00:00:00 2001 From: Jason Bennett Date: Mon, 3 Jan 2011 08:37:17 +0300 Subject: [PATCH] SONAR-1481: Allow Findbugs extensions Apply contributed patch --- .../findbugs/FindbugsRuleRepository.java | 19 +- .../plugins/findbugs/FakeRuleFinder.java | 12 +- .../findbugs/FindbugsRuleRepositoryTest.java | 5 +- .../sonar/plugins/findbugs/FindbugsTests.java | 194 +++++++++--------- 4 files changed, 127 insertions(+), 103 deletions(-) diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java index 38150ecead9..45b6a5e3416 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java @@ -19,25 +19,36 @@ */ package org.sonar.plugins.findbugs; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.resources.Java; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleRepository; import org.sonar.api.rules.XMLRuleParser; -import java.util.List; - public final class FindbugsRuleRepository extends RuleRepository { private XMLRuleParser xmlRuleParser; - public FindbugsRuleRepository(XMLRuleParser xmlRuleParser) { + private ServerFileSystem fileSystem; + + public FindbugsRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) { super(FindbugsConstants.REPOSITORY_KEY, Java.KEY); setName(FindbugsConstants.REPOSITORY_NAME); this.xmlRuleParser = xmlRuleParser; + this.fileSystem = fileSystem; } @Override public List createRules() { - return xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml")); + List rules = new ArrayList(); + rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml"))); + for (File userExtensionXml : fileSystem.getExtensions(FindbugsConstants.REPOSITORY_KEY, "xml")) { + rules.addAll(xmlRuleParser.parse(userExtensionXml)); + } + return rules; } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java index eae82300b29..6ae98f1ea5d 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java @@ -19,20 +19,24 @@ */ package org.sonar.plugins.findbugs; +import static org.mockito.Mockito.mock; + +import java.util.Collection; +import java.util.List; + +import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleQuery; import org.sonar.api.rules.XMLRuleParser; -import java.util.Collection; -import java.util.List; - public class FakeRuleFinder implements RuleFinder { private final List findbugsRules; public FakeRuleFinder() { - FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser()); + ServerFileSystem sfs = mock(ServerFileSystem.class); + FindbugsRuleRepository repo = new FindbugsRuleRepository(sfs, new XMLRuleParser()); findbugsRules = repo.createRules(); for (Rule rule : findbugsRules) { rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java index 511232b7a6d..87c499354bc 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java @@ -22,10 +22,12 @@ package org.sonar.plugins.findbugs; import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; import java.util.List; import org.junit.Test; +import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.rules.Rule; import org.sonar.api.rules.XMLRuleParser; @@ -33,7 +35,8 @@ public class FindbugsRuleRepositoryTest { @Test public void testLoadRepositoryFromXml() { - FindbugsRuleRepository repository = new FindbugsRuleRepository(new XMLRuleParser()); + ServerFileSystem fileSystem = mock(ServerFileSystem.class); + FindbugsRuleRepository repository = new FindbugsRuleRepository(fileSystem, new XMLRuleParser()); List rules = repository.createRules(); assertThat(rules.size(), greaterThan(300)); for (Rule rule : rules) { 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 b8d91e7eec2..4312ec6b49d 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 @@ -1,94 +1,100 @@ -/* - * 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.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.*; -import org.sonar.test.TestUtils; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.util.ArrayList; -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 { - String expectedContent = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/" + expectedFileName); - TestUtils.assertSimilarXml(expectedContent, actualContent); - } - - protected List buildRulesFixture() { - List rules = new ArrayList(); - - Rule rule1 = Rule.create(FindbugsConstants.REPOSITORY_KEY, "DLS_DEAD_LOCAL_STORE", "DLS: Dead store to local variable"); - Rule rule2 = Rule.create(FindbugsConstants.REPOSITORY_KEY, "URF_UNREAD_FIELD", "UrF: Unread field"); - - rules.add(rule1); - rules.add(rule2); - - return rules; - } - - protected List buildActiveRulesFixture(List rules) { - List activeRules = new ArrayList(); - ActiveRule activeRule1 = new ActiveRule(null, rules.get(0), RulePriority.CRITICAL); - activeRules.add(activeRule1); - ActiveRule activeRule2 = new ActiveRule(null, rules.get(1), RulePriority.MAJOR); - activeRules.add(activeRule2); - return activeRules; - } - - protected RulesManager createRulesManager() { - RulesManager rulesManager = mock(RulesManager.class); - - when(rulesManager.getPluginRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer() { - - public Rule answer(InvocationOnMock invocationOnMock) throws Throwable { - Object[] args = invocationOnMock.getArguments(); - Rule rule = Rule.create(); - rule.setPluginName((String) args[0]); - rule.setKey((String) args[1]); - return rule; - } - }); - return rulesManager; - } - - protected RulesProfile createRulesProfileWithActiveRules() { - RulesProfile profile = RulesProfile.create(); - profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME); - profile.setLanguage(Java.KEY); - for (Rule rule : new FindbugsRuleRepository(new XMLRuleParser()).createRules()) { - rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); - profile.activateRule(rule, null); - } - return profile; - } -} +/* + * 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.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.List; + +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.CoreProperties; +import org.sonar.api.platform.ServerFileSystem; +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.RulesManager; +import org.sonar.api.rules.XMLRuleParser; +import org.sonar.test.TestUtils; +import org.xml.sax.SAXException; + +public abstract class FindbugsTests { + + protected void assertXmlAreSimilar(String actualContent, String expectedFileName) throws IOException, SAXException { + String expectedContent = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/" + expectedFileName); + TestUtils.assertSimilarXml(expectedContent, actualContent); + } + + protected List buildRulesFixture() { + List rules = new ArrayList(); + + Rule rule1 = Rule.create(FindbugsConstants.REPOSITORY_KEY, "DLS_DEAD_LOCAL_STORE", "DLS: Dead store to local variable"); + Rule rule2 = Rule.create(FindbugsConstants.REPOSITORY_KEY, "URF_UNREAD_FIELD", "UrF: Unread field"); + + rules.add(rule1); + rules.add(rule2); + + return rules; + } + + protected List buildActiveRulesFixture(List rules) { + List activeRules = new ArrayList(); + ActiveRule activeRule1 = new ActiveRule(null, rules.get(0), RulePriority.CRITICAL); + activeRules.add(activeRule1); + ActiveRule activeRule2 = new ActiveRule(null, rules.get(1), RulePriority.MAJOR); + activeRules.add(activeRule2); + return activeRules; + } + + protected RulesManager createRulesManager() { + RulesManager rulesManager = mock(RulesManager.class); + + when(rulesManager.getPluginRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer() { + + public Rule answer(InvocationOnMock invocationOnMock) throws Throwable { + Object[] args = invocationOnMock.getArguments(); + Rule rule = Rule.create(); + rule.setPluginName((String) args[0]); + rule.setKey((String) args[1]); + return rule; + } + }); + return rulesManager; + } + + protected RulesProfile createRulesProfileWithActiveRules() { + RulesProfile profile = RulesProfile.create(); + profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME); + profile.setLanguage(Java.KEY); + ServerFileSystem sfs = mock(ServerFileSystem.class); + for (Rule rule : new FindbugsRuleRepository(sfs, new XMLRuleParser()).createRules()) { + rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); + profile.activateRule(rule, null); + } + return profile; + } +} -- 2.39.5