]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2804 Add test
authorDavid Gageot <david@gageot.net>
Wed, 3 Oct 2012 13:06:39 +0000 (15:06 +0200)
committerDavid Gageot <david@gageot.net>
Wed, 3 Oct 2012 13:48:45 +0000 (15:48 +0200)
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java [new file with mode: 0644]
plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy [new file with mode: 0644]
plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec [new file with mode: 0644]
plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec [new file with mode: 0644]

index 4a9b53fdf8fde1b75669515475d424a2cefab6a5..79a24239cb448db13b84db8d9ae20ffaa39a7cec 100644 (file)
@@ -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 (file)
index 0000000..bc318e6
--- /dev/null
@@ -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 (file)
index 0000000..e9d3400
Binary files /dev/null and b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy 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 (file)
index 0000000..ce2421d
Binary files /dev/null and b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec 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 (file)
index 0000000..09d8437
Binary files /dev/null and b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec differ