From 07e3d0d4c4341d7abd231395f41eeb1d8e05cecc Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 29 Oct 2012 14:50:27 +0100 Subject: [PATCH] SONAR-3890 The overall code coverage should not be computed when the code coverage by unit tests is not computed with jacoco --- .../plugins/jacoco/JaCoCoOverallSensor.java | 31 ++++++++++--------- .../jacoco/JaCoCoOverallSensorTest.java | 31 +++++++++++++++++++ 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java index fc5c61f2ced..4ead9748ed6 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java @@ -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); } } } diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java index 60f167d7afc..b9cabe9efe8 100644 --- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java @@ -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"); -- 2.39.5