2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 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.cobertura;
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;
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.test.MavenTestUtils;
41 public class CoberturaMavenInitializerTest {
43 private Project project;
44 private CoberturaMavenInitializer initializer;
48 project = mock(Project.class);
49 initializer = new CoberturaMavenInitializer(new CoberturaMavenPluginHandler());
53 public void doNotExecuteMavenPluginIfReuseReports() {
54 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
55 assertThat(initializer.getMavenPluginHandler(project), nullValue());
59 public void doNotExecuteMavenPluginIfStaticAnalysis() {
60 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
61 assertThat(initializer.getMavenPluginHandler(project), nullValue());
65 public void executeMavenPluginIfDynamicAnalysis() {
66 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
67 assertThat(initializer.getMavenPluginHandler(project), not(nullValue()));
68 assertThat(initializer.getMavenPluginHandler(project).getArtifactId(), is("cobertura-maven-plugin"));
72 public void doNotSetReportPathIfAlreadyConfigured() {
73 Configuration configuration = mock(Configuration.class);
74 when(configuration.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn(true);
75 when(project.getConfiguration()).thenReturn(configuration);
76 initializer.execute(project);
77 verify(configuration, never()).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), anyString());
81 public void shouldSetReportPathFromPom() {
82 Configuration configuration = mock(Configuration.class);
83 when(project.getConfiguration()).thenReturn(configuration);
84 MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldGetReportPathFromPom/pom.xml");
85 when(project.getPom()).thenReturn(pom);
86 initializer.execute(project);
87 verify(configuration).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), eq("overridden/dir/coverage.xml"));