]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3890 The overall code coverage should not be computed when the code coverage...
authorDavid Gageot <david@gageot.net>
Mon, 29 Oct 2012 13:50:27 +0000 (14:50 +0100)
committerDavid Gageot <david@gageot.net>
Mon, 29 Oct 2012 13:50:27 +0000 (14:50 +0100)
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java

index fc5c61f2ced848d84fae6029c0eb18d8f8263e00..4ead9748ed605d785b39e07968baaea9ede36648 100644 (file)
@@ -53,7 +53,8 @@ public class JaCoCoOverallSensor implements Sensor {
   }
 
   public boolean shouldExecuteOnProject(Project project) {
-    return StringUtils.isNotBlank(configuration.getItReportPath()) && project.getAnalysisType().isDynamic(true);
+    return StringUtils.isNotBlank(configuration.getItReportPath())
+      && project.getAnalysisType().isDynamic(true);
   }
 
   public void analyse(Project project, SensorContext context) {
@@ -61,6 +62,10 @@ public class JaCoCoOverallSensor implements Sensor {
 
     File reportUTs = fs.resolvePath(configuration.getReportPath());
     File reportITs = fs.resolvePath(configuration.getItReportPath());
+    if ((!reportUTs.exists()) || (!reportITs.exists())) {
+      return;
+    }
+
     File reportOverall = new File(fs.getSonarWorkingDirectory(), JACOCO_OVERALL);
     reportOverall.getParentFile().mkdirs();
 
@@ -91,19 +96,17 @@ public class JaCoCoOverallSensor implements Sensor {
 
   private void loadSourceFiles(SessionInfoStore infoStore, ExecutionDataStore dataStore, File... reports) {
     for (File report : reports) {
-      if (report.exists()) {
-        InputStream resourceStream = null;
-        try {
-          resourceStream = new BufferedInputStream(new FileInputStream(report));
-          ExecutionDataReader reader = new ExecutionDataReader(resourceStream);
-          reader.setSessionInfoVisitor(infoStore);
-          reader.setExecutionDataVisitor(dataStore);
-          reader.read();
-        } catch (IOException e) {
-          throw new SonarException(String.format("Unable to read %s", report.getAbsolutePath()), e);
-        } finally {
-          Closeables.closeQuietly(resourceStream);
-        }
+      InputStream resourceStream = null;
+      try {
+        resourceStream = new BufferedInputStream(new FileInputStream(report));
+        ExecutionDataReader reader = new ExecutionDataReader(resourceStream);
+        reader.setSessionInfoVisitor(infoStore);
+        reader.setExecutionDataVisitor(dataStore);
+        reader.read();
+      } catch (IOException e) {
+        throw new SonarException(String.format("Unable to read %s", report.getAbsolutePath()), e);
+      } finally {
+        Closeables.closeQuietly(resourceStream);
       }
     }
   }
index 60f167d7afc087e1fdbfd06fccf4652c3d15a884..b9cabe9efe861ca8d26d361251c1e64eb4fa939c 100644 (file)
@@ -40,6 +40,7 @@ import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 public class JaCoCoOverallSensorTest {
@@ -96,6 +97,36 @@ public class JaCoCoOverallSensorTest {
     verifyNoMoreInteractions(context);
   }
 
+  @Test
+  public void should_no_save_measures_when_it_report_is_not_found() throws IOException {
+    File outputDir = TestUtils.getResource(JaCoCoOverallSensorTest.class, ".");
+
+    when(project.getFileSystem()).thenReturn(pfs);
+    when(configuration.getReportPath()).thenReturn("ut.exec");
+    when(configuration.getItReportPath()).thenReturn("it.not.found.exec");
+    when(pfs.resolvePath("ut.exec")).thenReturn(new File(outputDir, "ut.exec"));
+    when(pfs.resolvePath("it.not.found.exec")).thenReturn(new File("it.not.found.exec"));
+
+    sensor.analyse(project, context);
+
+    verifyZeroInteractions(context);
+  }
+
+  @Test
+  public void should_no_save_measures_when_ut_report_is_not_found() throws IOException {
+    File outputDir = TestUtils.getResource(JaCoCoOverallSensorTest.class, ".");
+
+    when(project.getFileSystem()).thenReturn(pfs);
+    when(configuration.getReportPath()).thenReturn("ut.not.found.exec");
+    when(configuration.getItReportPath()).thenReturn("it.exec");
+    when(pfs.resolvePath("ut.not.found.exec")).thenReturn(new File("ut.not.found.exec"));
+    when(pfs.resolvePath("it.exec")).thenReturn(new File(outputDir, "it.exec"));
+
+    sensor.analyse(project, context);
+
+    verifyZeroInteractions(context);
+  }
+
   @Test
   public void testSensorDefinition() {
     assertThat(sensor.toString()).isEqualTo("JaCoCoOverallSensor");