]> source.dussan.org Git - sonarqube.git/blob
a2d2cc3643df0144080fb3b77c7ed6c0bd4fc9b2
[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 org.apache.commons.io.FileUtils;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.TemporaryFolder;
27 import org.sonar.api.CoreProperties;
28 import org.sonar.api.config.PropertyDefinitions;
29 import org.sonar.api.config.Settings;
30 import org.sonar.api.profiles.RulesProfile;
31 import org.sonar.api.resources.Project;
32 import org.sonar.api.test.SimpleProjectFileSystem;
33
34 import java.io.File;
35 import java.util.Locale;
36
37 import static org.fest.assertions.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41 public class FindbugsConfigurationTest {
42
43   @Rule
44   public TemporaryFolder tempFolder = new TemporaryFolder();
45
46   private Project project;
47   private Settings settings;
48   private File findbugsTempDir;
49   private FindbugsConfiguration conf;
50
51   @Before
52   public void setUp() {
53     project = mock(Project.class);
54     settings = new Settings(new PropertyDefinitions().addComponent(FindbugsPlugin.class));
55     findbugsTempDir = tempFolder.newFolder("findbugs");
56     when(project.getFileSystem()).thenReturn(new SimpleProjectFileSystem(findbugsTempDir));
57     conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
58   }
59
60   @Test
61   public void should_return_report_file() throws Exception {
62     assertThat(conf.getTargetXMLReport()).isEqualTo(new File(findbugsTempDir, "target/sonar/findbugs-result.xml"));
63   }
64
65   @Test
66   public void should_save_include_config() throws Exception {
67     conf.saveIncludeConfigXml();
68     File findbugsIncludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-include.xml");
69     assertThat(findbugsIncludeFile.exists()).isTrue();
70   }
71
72   @Test
73   public void should_save_exclude_config() throws Exception {
74     when(project.getExclusionPatterns()).thenReturn(new String[] {"dir/**/*.java"});
75     conf.saveExcludeConfigXml();
76     File findbugsExcludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-exclude.xml");
77     assertThat(findbugsExcludeFile.exists()).isTrue();
78     String findbugsExclude = FileUtils.readFileToString(findbugsExcludeFile);
79     assertThat(findbugsExclude).contains("Match");
80   }
81
82   @Test
83   public void should_save_empty_exclude_config() throws Exception {
84     conf.saveExcludeConfigXml();
85     File findbugsExcludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-exclude.xml");
86     assertThat(findbugsExcludeFile.exists()).isTrue();
87     String findbugsExclude = FileUtils.readFileToString(findbugsExcludeFile);
88     assertThat(findbugsExclude).doesNotContain("Match");
89   }
90
91   @Test
92   public void should_return_effort() {
93     assertThat(conf.getEffort()).as("default effort").isEqualTo("default");
94     settings.setProperty(CoreProperties.FINDBUGS_EFFORT_PROPERTY, "Max");
95     assertThat(conf.getEffort()).isEqualTo("max");
96   }
97
98   @Test
99   public void should_return_timeout() {
100     assertThat(conf.getTimeout()).as("default timeout").isEqualTo(600000);
101     settings.setProperty(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY, 1);
102     assertThat(conf.getTimeout()).isEqualTo(1);
103   }
104
105   @Test
106   public void should_return_locale() {
107     settings.setProperty(CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, "fr");
108     assertThat(conf.getLocale()).isEqualTo(Locale.FRENCH);
109   }
110
111   @Test
112   public void should_return_excludes_filters() {
113     assertThat(conf.getExcludesFilters()).isEmpty();
114     settings.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, " foo.xml , bar.xml,");
115     assertThat(conf.getExcludesFilters()).hasSize(2);
116   }
117
118   @Test
119   public void should_return_confidence_level() {
120     assertThat(conf.getConfidenceLevel()).as("default confidence level").isEqualTo("medium");
121     settings.setProperty(CoreProperties.FINDBUGS_EFFORT_PROPERTY, "HIGH");
122     assertThat(conf.getEffort()).isEqualTo("high");
123   }
124
125 }