]> source.dussan.org Git - sonarqube.git/blob
fa6ef266f75b580d07e61a5bc43c9b95a6f93996
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2011 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.cobertura;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.Matchers.nullValue;
24 import static org.hamcrest.core.IsNot.not;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import org.apache.commons.configuration.Configuration;
34 import org.apache.maven.project.MavenProject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.sonar.api.CoreProperties;
38 import org.sonar.api.resources.Project;
39 import org.sonar.api.resources.ProjectFileSystem;
40 import org.sonar.api.test.MavenTestUtils;
41
42 import java.io.File;
43
44 public class CoberturaMavenInitializerTest {
45
46   private Project project;
47   private CoberturaMavenInitializer initializer;
48
49   @Before
50   public void setUp() {
51     project = mock(Project.class);
52     initializer = new CoberturaMavenInitializer(new CoberturaMavenPluginHandler());
53   }
54
55   @Test
56   public void doNotExecuteMavenPluginIfReuseReports() {
57     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
58     assertThat(initializer.getMavenPluginHandler(project), nullValue());
59   }
60
61   @Test
62   public void doNotExecuteMavenPluginIfStaticAnalysis() {
63     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
64     assertThat(initializer.getMavenPluginHandler(project), nullValue());
65   }
66
67   @Test
68   public void executeMavenPluginIfDynamicAnalysis() {
69     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
70     assertThat(initializer.getMavenPluginHandler(project), not(nullValue()));
71     assertThat(initializer.getMavenPluginHandler(project).getArtifactId(), is("cobertura-maven-plugin"));
72   }
73
74   @Test
75   public void doNotSetReportPathIfAlreadyConfigured() {
76     Configuration configuration = mock(Configuration.class);
77     when(configuration.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn(true);
78     when(project.getConfiguration()).thenReturn(configuration);
79     initializer.execute(project);
80     verify(configuration, never()).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), anyString());
81   }
82
83   @Test
84   public void shouldSetReportPathFromPom() {
85     Configuration configuration = mock(Configuration.class);
86     when(project.getConfiguration()).thenReturn(configuration);
87     MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldGetReportPathFromPom/pom.xml");
88     when(project.getPom()).thenReturn(pom);
89     initializer.execute(project);
90     verify(configuration).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), eq("overridden/dir/coverage.xml"));
91   }
92
93   @Test
94   public void shouldSetDefaultReportPath() {
95     ProjectFileSystem pfs = mock(ProjectFileSystem.class);
96     when(pfs.getReportOutputDir()).thenReturn(new File("target/sites"));
97     Configuration configuration = mock(Configuration.class);
98     when(project.getConfiguration()).thenReturn(configuration);
99     when(project.getFileSystem()).thenReturn(pfs);
100     initializer.execute(project);
101     verify(configuration).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), eq("target/sites/cobertura/coverage.xml"));
102   }
103 }