]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2804 Fix Overall Jacoco location
authorDavid Gageot <david@gageot.net>
Tue, 16 Oct 2012 06:43:14 +0000 (08:43 +0200)
committerDavid Gageot <david@gageot.net>
Tue, 16 Oct 2012 06:44:04 +0000 (08:44 +0200)
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 5319ca72ce0132b9f28f9ab3be1467b0de688fcb..9f52d158361ed1a7ec0bcb084a3ed82b36670182 100644 (file)
@@ -31,6 +31,7 @@ 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.utils.SonarException;
 
 import java.io.BufferedInputStream;
@@ -43,7 +44,7 @@ import java.io.InputStream;
 import java.util.Collection;
 
 public class JaCoCoOverallSensor implements Sensor {
-  private static final String JACOCO_OVERALL = "target/sonar/jacoco-overall.exec";
+  public static final String JACOCO_OVERALL = "jacoco-overall.exec";
 
   private final JacocoConfiguration configuration;
 
@@ -57,21 +58,23 @@ public class JaCoCoOverallSensor implements Sensor {
   }
 
   public void analyse(Project project, SensorContext context) {
-    mergeReports(project);
+    ProjectFileSystem fs = project.getFileSystem();
 
-    new OverallAnalyzer().analyse(project, context);
-  }
-
-  private void mergeReports(Project project) {
-    File reportUTs = project.getFileSystem().resolvePath(configuration.getReportPath());
-    File reportITs = project.getFileSystem().resolvePath(configuration.getItReportPath());
-    File reportOverall = project.getFileSystem().resolvePath(JACOCO_OVERALL);
+    File reportUTs = fs.resolvePath(configuration.getReportPath());
+    File reportITs = fs.resolvePath(configuration.getItReportPath());
+    File reportOverall = new File(fs.getSonarWorkingDirectory(), JACOCO_OVERALL);
     reportOverall.getParentFile().mkdirs();
 
+    mergeReports(reportOverall, reportUTs, reportITs);
+
+    new OverallAnalyzer(reportOverall).analyse(project, context);
+  }
+
+  private void mergeReports(File reportOverall, File... reports) {
     SessionInfoStore infoStore = new SessionInfoStore();
     ExecutionDataStore dataStore = new ExecutionDataStore();
 
-    loadSourceFiles(infoStore, dataStore, reportUTs, reportITs);
+    loadSourceFiles(infoStore, dataStore, reports);
 
     BufferedOutputStream outputStream = null;
     try {
@@ -87,18 +90,18 @@ public class JaCoCoOverallSensor implements Sensor {
     }
   }
 
-  private void loadSourceFiles(SessionInfoStore infoStore, ExecutionDataStore dataStore, File... files) {
-    for (File file : files) {
-      if (file.exists()) {
+  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(file));
+          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", file.getAbsolutePath()), e);
+          throw new SonarException(String.format("Unable to read %s", report.getAbsolutePath()), e);
         } finally {
           Closeables.closeQuietly(resourceStream);
         }
@@ -107,9 +110,15 @@ public class JaCoCoOverallSensor implements Sensor {
   }
 
   class OverallAnalyzer extends AbstractAnalyzer {
+    private final File report;
+
+    OverallAnalyzer(File report) {
+      this.report = report;
+    }
+
     @Override
     protected String getReportPath(Project project) {
-      return JACOCO_OVERALL;
+      return report.getAbsolutePath();
     }
 
     @Override
index 4530c57fdaa425cb761119a0b1fba4e11094ae91..60f167d7afc087e1fdbfd06fccf4652c3d15a884 100644 (file)
@@ -80,7 +80,8 @@ public class JaCoCoOverallSensorTest {
     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/jacoco-overall.exec")).thenReturn(new File("target/sonar/jacoco-overall.exec"));
+    when(pfs.getSonarWorkingDirectory()).thenReturn(new File("target/sonar"));
+    when(pfs.resolvePath(new File("target/sonar/jacoco-overall.exec").getAbsolutePath())).thenReturn(new File("target/sonar/jacoco-overall.exec"));
 
     sensor.analyse(project, context);