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 org.junit.Test;
23 import org.sonar.api.profiles.RulesProfile;
24 import org.sonar.api.rules.ActiveRule;
25 import org.sonar.api.rules.Rule;
26 import org.sonar.api.rules.RulePriority;
27 import org.sonar.plugins.findbugs.xml.Bug;
28 import org.sonar.plugins.findbugs.xml.FindBugsFilter;
29 import org.sonar.plugins.findbugs.xml.Match;
30 import org.xml.sax.SAXException;
32 import java.io.IOException;
33 import java.io.StringWriter;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
38 import static org.fest.assertions.Assertions.assertThat;
40 public class FindbugsProfileExporterTest extends FindbugsTests {
42 private FindbugsProfileExporter exporter = new FindbugsProfileExporter();
45 public void shouldAddHeaderToExportedXml() throws IOException, SAXException {
46 RulesProfile profile = RulesProfile.create();
48 StringWriter xml = new StringWriter();
49 exporter.exportProfile(profile, xml);
50 assertXmlAreSimilar(xml.toString(), "test_header.xml");
54 public void shouldExportConfiguration() throws IOException, SAXException {
55 List<Rule> rules = buildRulesFixture();
56 List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(rules);
57 RulesProfile profile = RulesProfile.create();
58 profile.setActiveRules(activeRulesExpected);
60 StringWriter xml = new StringWriter();
61 exporter.exportProfile(profile, xml);
62 assertXmlAreSimilar(xml.toString(), "test_xml_complete.xml");
66 public void shouldBuildOnlyOneModuleWhenNoActiveRules() {
67 FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Collections.<ActiveRule> emptyList());
68 assertThat(filter.getMatchs()).hasSize(0);
72 public void shouldBuildTwoModulesEvenIfSameTwoRulesActivated() {
73 ActiveRule activeRule1 = anActiveRule(DLS_DEAD_LOCAL_STORE);
74 ActiveRule activeRule2 = anActiveRule(SS_SHOULD_BE_STATIC);
75 FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
77 List<Match> matches = filter.getMatchs();
78 assertThat(matches).hasSize(2);
80 assertThat(matches.get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
81 assertThat(matches.get(1).getBug().getPattern()).isEqualTo("SS_SHOULD_BE_STATIC");
85 public void shouldBuildOnlyOneModuleWhenNoFindbugsActiveRules() {
86 ActiveRule activeRule1 = anActiveRuleFromAnotherPlugin();
87 ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin();
89 FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
90 assertThat(filter.getMatchs()).hasSize(0);
94 public void shouldBuildModuleWithProperties() {
95 ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE);
96 FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule));
98 assertThat(filter.getMatchs()).hasSize(1);
99 assertThat(filter.getMatchs().get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
103 public void shouldBuilXmlFromModuleTree() throws IOException, SAXException {
104 FindBugsFilter findBugsFilter = new FindBugsFilter();
105 findBugsFilter.addMatch(new Match(new Bug("DLS_DEAD_LOCAL_STORE")));
106 findBugsFilter.addMatch(new Match(new Bug("URF_UNREAD_FIELD")));
108 String xml = findBugsFilter.toXml();
110 assertXmlAreSimilar(xml, "test_module_tree.xml");
113 private static final String DLS_DEAD_LOCAL_STORE = "DLS_DEAD_LOCAL_STORE";
114 private static final String SS_SHOULD_BE_STATIC = "SS_SHOULD_BE_STATIC";
116 private static ActiveRule anActiveRule(String configKey) {
117 Rule rule = Rule.create();
118 rule.setConfigKey(configKey);
119 rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
120 ActiveRule activeRule = RulesProfile.create().activateRule(rule, RulePriority.CRITICAL);
124 private static ActiveRule anActiveRuleFromAnotherPlugin() {
125 Rule rule = Rule.create();
126 rule.setPluginName("not-a-findbugs-plugin");
127 ActiveRule activeRule = RulesProfile.create().activateRule(rule, RulePriority.CRITICAL);