]> source.dussan.org Git - sonarqube.git/blob
c7219e182b77b79b5719c855f1d5805247e026a0
[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.google.common.collect.Lists;
23 import org.apache.commons.configuration.Configuration;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.resources.InputFile;
27 import org.sonar.api.resources.InputFileUtils;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.resources.ProjectFileSystem;
30 import org.sonar.api.test.MavenTestUtils;
31
32 import java.util.ArrayList;
33
34 import static org.fest.assertions.Assertions.assertThat;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Matchers.eq;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41
42 public class FindbugsMavenInitializerTest {
43
44   private Project project;
45   private FindbugsMavenInitializer initializer;
46
47   @Before
48   public void setUp() {
49     project = mock(Project.class);
50     initializer = new FindbugsMavenInitializer();
51   }
52
53   @Test
54   public void shouldNotAnalyseIfNoJavaProject() {
55     Project project = mock(Project.class);
56     when(project.getLanguageKey()).thenReturn("php");
57     assertThat(initializer.shouldExecuteOnProject(project)).isFalse();
58   }
59
60   @Test
61   public void shouldNotAnalyseIfJavaProjectButNoSource() {
62     Project project = mock(Project.class);
63     ProjectFileSystem fs = mock(ProjectFileSystem.class);
64     when(fs.mainFiles("java")).thenReturn(new ArrayList<InputFile>());
65     when(project.getFileSystem()).thenReturn(fs);
66     when(project.getLanguageKey()).thenReturn("java");
67     assertThat(initializer.shouldExecuteOnProject(project)).isFalse();
68   }
69
70   @Test
71   public void shouldAnalyse() {
72     Project project = mock(Project.class);
73     ProjectFileSystem fs = mock(ProjectFileSystem.class);
74     when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
75     when(project.getFileSystem()).thenReturn(fs);
76     when(project.getLanguageKey()).thenReturn("java");
77     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
78     assertThat(initializer.shouldExecuteOnProject(project)).isTrue();
79   }
80
81   @Test
82   public void doNotSetExcludesFiltersIfAlreadyConfigured() {
83     Configuration configuration = mock(Configuration.class);
84     when(configuration.containsKey(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)).thenReturn(true);
85     when(project.getConfiguration()).thenReturn(configuration);
86     initializer.execute(project);
87     verify(configuration, never()).setProperty(eq(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY), anyString());
88   }
89
90   @Test
91   public void shouldGetExcludesFiltersFromPom() {
92     Project project = MavenTestUtils.loadProjectFromPom(getClass(), "pom.xml");
93     initializer.execute(project);
94     assertThat(project.getConfiguration().getString(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)).isEqualTo("foo.xml");
95   }
96
97 }