2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.findbugs;
22 import com.thoughtworks.xstream.XStream;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.Test;
25 import org.sonar.api.profiles.RulesProfile;
26 import org.sonar.api.rules.ActiveRule;
27 import org.sonar.api.utils.ValidationMessages;
28 import org.sonar.plugins.findbugs.xml.FindBugsFilter;
29 import org.sonar.plugins.findbugs.xml.Match;
30 import org.sonar.test.TestUtils;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.StringReader;
36 import java.util.List;
38 import static org.fest.assertions.Assertions.assertThat;
40 public class FindbugsProfileImporterTest {
42 private final FindbugsProfileImporter importer = new FindbugsProfileImporter(new FakeRuleFinder());
45 public void shouldImportPatterns() {
46 String findbugsConf = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/shouldImportPatterns.xml");
47 RulesProfile profile = importer.importProfile(new StringReader(findbugsConf), ValidationMessages.create());
49 assertThat(profile.getActiveRules()).hasSize(2);
50 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "NP_CLOSING_NULL")).isNotNull();
51 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE")).isNotNull();
55 public void shouldImportCodes() {
56 InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/shouldImportCodes.xml");
57 RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
58 List<ActiveRule> results = profile.getActiveRules();
60 assertThat(results).hasSize(18);
61 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "EC_INCOMPATIBLE_ARRAY_COMPARE")).isNotNull();
62 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY")).isNotNull();
66 public void shouldImportCategories() {
67 InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/shouldImportCategories.xml");
68 RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
69 List<ActiveRule> results = profile.getActiveRules();
71 assertThat(results).hasSize(182);
72 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")).isNotNull();
76 public void shouldImportConfigurationBugInclude() {
77 InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/findbugs-include.xml");
78 RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
79 List<ActiveRule> results = profile.getActiveRules();
81 assertThat(results).hasSize(11);
82 assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE")).isNotNull();
86 public void shouldBuilModuleTreeFromXml() throws IOException {
87 InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/test_module_tree.xml");
89 XStream xStream = FindBugsFilter.createXStream();
90 FindBugsFilter filter = (FindBugsFilter) xStream.fromXML(IOUtils.toString(input));
92 List<Match> matches = filter.getMatchs();
93 assertThat(matches).hasSize(2);
94 assertThat(matches.get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
95 assertThat(matches.get(1).getBug().getPattern()).isEqualTo("URF_UNREAD_FIELD");
99 public void testImportingUncorrectXmlFile() {
100 String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/uncorrectFindbugsXml.xml");
101 ValidationMessages messages = ValidationMessages.create();
102 RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
103 List<ActiveRule> results = profile.getActiveRules();
105 assertThat(results).hasSize(0);
106 assertThat(messages.getErrors()).hasSize(1);
110 public void testImportingXmlFileWithUnknownRule() {
111 String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownRule.xml");
112 ValidationMessages messages = ValidationMessages.create();
113 RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
114 List<ActiveRule> results = profile.getActiveRules();
116 assertThat(results).hasSize(1);
117 assertThat(messages.getWarnings()).hasSize(1);
121 public void testImportingXmlFileWithUnknownCategory() {
122 String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCategory.xml");
123 ValidationMessages messages = ValidationMessages.create();
124 RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
125 List<ActiveRule> results = profile.getActiveRules();
127 assertThat(results).hasSize(141);
128 assertThat(messages.getWarnings()).hasSize(1);
132 public void testImportingXmlFileWithUnknownCode() {
133 String uncorrectFindbugsXml = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/findbugsXmlWithUnknownCode.xml");
134 ValidationMessages messages = ValidationMessages.create();
135 RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
136 List<ActiveRule> results = profile.getActiveRules();
138 assertThat(results).hasSize(9);
139 assertThat(messages.getWarnings()).hasSize(1);