aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-jacoco-plugin
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-10-03 15:06:39 +0200
committerDavid Gageot <david@gageot.net>2012-10-03 15:48:45 +0200
commit1359b743a19c251e70961541906a1a71dbb46b5b (patch)
tree53efa6b5c3b8452cd571508b493fe0e5dc964373 /plugins/sonar-jacoco-plugin
parent2f435421c69ba3205bc611ad081891e1d572c69e (diff)
downloadsonarqube-1359b743a19c251e70961541906a1a71dbb46b5b.tar.gz
sonarqube-1359b743a19c251e70961541906a1a71dbb46b5b.zip
SONAR-2804 Add test
Diffstat (limited to 'plugins/sonar-jacoco-plugin')
-rw-r--r--plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java3
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java96
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopybin0 -> 666 bytes
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.execbin0 -> 5862 bytes
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.execbin0 -> 5850 bytes
5 files changed, 98 insertions, 1 deletions
diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java
index 4a9b53fdf8f..79a24239cb4 100644
--- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java
+++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java
@@ -43,7 +43,7 @@ import java.io.InputStream;
import java.util.Collection;
public class JaCoCoAllTestsSensor implements Sensor {
- private static final String MERGED_EXEC = "target/merged.exec";
+ private static final String MERGED_EXEC = "target/sonar/merged.exec";
private final JacocoConfiguration configuration;
@@ -66,6 +66,7 @@ public class JaCoCoAllTestsSensor implements Sensor {
File reportUTs = project.getFileSystem().resolvePath(configuration.getReportPath());
File reportITs = project.getFileSystem().resolvePath(configuration.getItReportPath());
File reportAllTests = project.getFileSystem().resolvePath(MERGED_EXEC);
+ reportAllTests.getParentFile().mkdirs();
SessionInfoStore infoStore = new SessionInfoStore();
ExecutionDataStore dataStore = new ExecutionDataStore();
diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java
new file mode 100644
index 00000000000..bc318e66b6f
--- /dev/null
+++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java
@@ -0,0 +1,96 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.jacoco;
+
+import com.google.common.io.Files;
+import org.junit.Test;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.resources.Resource;
+import org.sonar.test.TestUtils;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class JaCoCoAllTestsSensorTest {
+ private final JacocoConfiguration configuration= mock(JacocoConfiguration.class);
+ private final SensorContext context = mock(SensorContext.class);
+ private final ProjectFileSystem pfs = mock(ProjectFileSystem.class);
+ private final Project project = mock(Project.class);
+ private final JaCoCoAllTestsSensor sensor = new JaCoCoAllTestsSensor(configuration);
+
+ @Test
+ public void should_execute_on_project() {
+ Project project = mock(Project.class);
+ when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec");
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+
+ assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
+ }
+
+ @Test
+ public void do_not_execute_when_report_path_not_specified() {
+ Project project = mock(Project.class);
+ when(configuration.getItReportPath()).thenReturn("");
+
+ assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
+ }
+
+ @Test
+ public void should_save_measures() throws IOException {
+ File outputDir = TestUtils.getResource(JaCoCoAllTestsSensorTest.class, ".");
+ Files.copy(TestUtils.getResource("HelloWorld.class.toCopy"), new File(outputDir, "HelloWorld.class"));
+
+ JavaFile resource = new JavaFile("com.sonar.coverages.HelloWorld");
+
+ when(project.getFileSystem()).thenReturn(pfs);
+ when(context.getResource(any(Resource.class))).thenReturn(resource);
+ when(configuration.getReportPath()).thenReturn("ut.exec");
+ when(configuration.getItReportPath()).thenReturn("it.exec");
+ when(pfs.getBuildOutputDir()).thenReturn(outputDir);
+ when(pfs.resolvePath("ut.exec")).thenReturn(new File(outputDir, "ut.exec"));
+ when(pfs.resolvePath("it.exec")).thenReturn(new File(outputDir, "it.exec"));
+ when(pfs.resolvePath("target/sonar/merged.exec")).thenReturn(new File("target/sonar/merged.exec"));
+
+ sensor.analyse(project, context);
+
+ verify(context).getResource(resource);
+ verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_LINES_TO_COVER, 5.0));
+ verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, 0.0));
+ verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_COVERAGE_LINE_HITS_DATA, "3=1;6=1;7=1;10=1;11=1"));
+ verifyNoMoreInteractions(context);
+ }
+
+ @Test
+ public void testSensorDefinition() {
+ assertThat(sensor.toString()).isEqualTo("JaCoCoAllTestsSensor");
+ }
+}
diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy
new file mode 100644
index 00000000000..e9d34004571
--- /dev/null
+++ b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy
Binary files differ
diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec
new file mode 100644
index 00000000000..ce2421df376
--- /dev/null
+++ b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec
Binary files differ
diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec
new file mode 100644
index 00000000000..09d84373641
--- /dev/null
+++ b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec
Binary files differ