]> source.dussan.org Git - sonarqube.git/blob
6920e086a23043db0217663a80654936a9ae716d
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2012 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.findbugs;
21
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;
31
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;
37
38 import static org.fest.assertions.Assertions.assertThat;
39
40 public class FindbugsProfileImporterTest {
41
42   private final FindbugsProfileImporter importer = new FindbugsProfileImporter(new FakeRuleFinder());
43
44   @Test
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());
48
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();
52   }
53
54   @Test
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();
59
60     assertThat(results).hasSize(19);
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();
63   }
64
65   @Test
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();
70
71     assertThat(results).hasSize(182);
72     assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")).isNotNull();
73   }
74
75   @Test
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();
80
81     assertThat(results).hasSize(11);
82     assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE")).isNotNull();
83   }
84
85   @Test
86   public void shouldBuilModuleTreeFromXml() throws IOException {
87     InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/findbugs/test_module_tree.xml");
88
89     XStream xStream = FindBugsFilter.createXStream();
90     FindBugsFilter filter = (FindBugsFilter) xStream.fromXML(IOUtils.toString(input));
91
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");
96   }
97
98   @Test
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();
104
105     assertThat(results).hasSize(0);
106     assertThat(messages.getErrors()).hasSize(1);
107   }
108
109   @Test
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();
115
116     assertThat(results).hasSize(1);
117     assertThat(messages.getWarnings()).hasSize(1);
118   }
119
120   @Test
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();
126
127     assertThat(results).hasSize(141);
128     assertThat(messages.getWarnings()).hasSize(1);
129   }
130
131   @Test
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();
137
138     assertThat(results).hasSize(10);
139     assertThat(messages.getWarnings()).hasSize(1);
140   }
141 }