]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7426 Take into account issues on level other than file
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 11 Mar 2016 13:14:54 +0000 (14:14 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 14 Mar 2016 08:20:55 +0000 (09:20 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitor.java
server/sonar-server/src/test/java/org/sonar/server/computation/issue/ComponentIssuesRepositoryRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForReportTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForViewsTest.java

index 9ddf433ad807f0899d78adf90784bbef8e06ce80..6860cc81b473c4dc4da09adcc616ab312d6db1c6 100644 (file)
@@ -128,11 +128,6 @@ public class QualityModelMeasuresVisitor extends PathAwareVisitorAdapter<Quality
   public void visitFile(Component file, Path<QualityModelCounter> path) {
     if (!file.getFileAttributes().isUnitTest()) {
       path.current().addDevCosts(computeDevelopmentCost(file));
-      for (Issue issue : componentIssuesRepository.getIssues(file)) {
-        if (issue.resolution() == null) {
-          path.current().addIssue(issue);
-        }
-      }
       computeAndSaveMeasures(file, path);
     }
   }
@@ -182,6 +177,7 @@ public class QualityModelMeasuresVisitor extends PathAwareVisitorAdapter<Quality
   }
 
   private void computeAndSaveMeasures(Component component, Path<QualityModelCounter> path) {
+    addIssues(component, path);
     addDevelopmentCostMeasure(component, path.current());
 
     double density = computeDensity(component, path.current());
@@ -196,6 +192,14 @@ public class QualityModelMeasuresVisitor extends PathAwareVisitorAdapter<Quality
     addToParent(path);
   }
 
+  private void addIssues(Component component, Path<QualityModelCounter> path){
+    for (Issue issue : componentIssuesRepository.getIssues(component)) {
+      if (issue.resolution() == null) {
+        path.current().addIssue(issue);
+      }
+    }
+  }
+
   private double computeDensity(Component component, QualityModelCounter developmentCost) {
     Optional<Measure> measure = measureRepository.getRawMeasure(component, maintainabilityRemediationEffortMetric);
     double maintainabilityRemediationEffort = measure.isPresent() ? measure.get().getLongValue() : 0L;
index 0c9158ee9cde726af567095a56f5ba38cfccb7a6..a8e1fd32113aa7464be5c126d46c8afaf5865ec4 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.issue;
 
+import java.util.Collections;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import org.junit.rules.ExternalResource;
@@ -30,6 +31,9 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
+import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
+import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
+import static org.sonar.server.computation.component.Component.Type.VIEW;
 
 public class ComponentIssuesRepositoryRule extends ExternalResource implements MutableComponentIssuesRepository, ComponentIssuesRepository {
 
@@ -61,6 +65,12 @@ public class ComponentIssuesRepositoryRule extends ExternalResource implements M
   @Override
   public List<DefaultIssue> getIssues(Component component) {
     checkNotNull(component, "component cannot be null");
+    // Views has no issues
+    if (component.getType().equals(PROJECT_VIEW)
+      || component.getType().equals(SUBVIEW)
+      || component.getType().equals(VIEW)) {
+      return Collections.emptyList();
+    }
     return getIssues(component.getReportAttributes().getRef());
   }
 
index 3068fad340bab8706f441b3ced46b55c280b40a4..eca599624c956262a18eee370d07086b4c787989 100644 (file)
@@ -370,7 +370,7 @@ public class QualityModelMeasuresVisitorForReportTest {
   @Test
   public void compute_reliability_rating() throws Exception {
     treeRootHolder.setRoot(ROOT_PROJECT);
-    fillComponentIssuesVisitorRule.setIssues(FILE_1_REF, newBugIssue(10L, BLOCKER), newBugIssue(1L, MAJOR),
+    fillComponentIssuesVisitorRule.setIssues(FILE_1_REF, newBugIssue(10L, MAJOR), newBugIssue(1L, MAJOR),
       // Should not be taken into account
       newVulnerabilityIssue(5L, MINOR)
       );
@@ -378,12 +378,13 @@ public class QualityModelMeasuresVisitorForReportTest {
       // Should not be taken into account
       newBugIssue(10L, BLOCKER).setResolution(RESOLUTION_FIXED)
       );
+    fillComponentIssuesVisitorRule.setIssues(MODULE_REF, newBugIssue(7L, BLOCKER));
 
     underTest.visit(ROOT_PROJECT);
 
-    verifyAddedRawMeasure(FILE_1_REF, RELIABILITY_RATING_KEY, E);
+    verifyAddedRawMeasure(FILE_1_REF, RELIABILITY_RATING_KEY, C);
     verifyAddedRawMeasure(FILE_2_REF, RELIABILITY_RATING_KEY, D);
-    verifyAddedRawMeasure(DIRECTORY_REF, RELIABILITY_RATING_KEY, E);
+    verifyAddedRawMeasure(DIRECTORY_REF, RELIABILITY_RATING_KEY, D);
     verifyAddedRawMeasure(MODULE_REF, RELIABILITY_RATING_KEY, E);
     verifyAddedRawMeasure(PROJECT_REF, RELIABILITY_RATING_KEY, E);
   }
@@ -391,19 +392,20 @@ public class QualityModelMeasuresVisitorForReportTest {
   @Test
   public void compute_security_rating() throws Exception {
     treeRootHolder.setRoot(ROOT_PROJECT);
-    fillComponentIssuesVisitorRule.setIssues(FILE_1_REF, newVulnerabilityIssue(10L, BLOCKER), newVulnerabilityIssue(1L, MAJOR),
+    fillComponentIssuesVisitorRule.setIssues(FILE_1_REF, newVulnerabilityIssue(10L, MAJOR), newVulnerabilityIssue(1L, MAJOR),
       // Should not be taken into account
       newBugIssue(1L, MAJOR));
     fillComponentIssuesVisitorRule.setIssues(FILE_2_REF, newVulnerabilityIssue(2L, CRITICAL), newVulnerabilityIssue(3L, MINOR),
       // Should not be taken into account
       newVulnerabilityIssue(10L, BLOCKER).setResolution(RESOLUTION_FIXED)
       );
+    fillComponentIssuesVisitorRule.setIssues(MODULE_REF, newVulnerabilityIssue(7L, BLOCKER));
 
     underTest.visit(ROOT_PROJECT);
 
-    verifyAddedRawMeasure(FILE_1_REF, SECURITY_RATING_KEY, E);
+    verifyAddedRawMeasure(FILE_1_REF, SECURITY_RATING_KEY, C);
     verifyAddedRawMeasure(FILE_2_REF, SECURITY_RATING_KEY, D);
-    verifyAddedRawMeasure(DIRECTORY_REF, SECURITY_RATING_KEY, E);
+    verifyAddedRawMeasure(DIRECTORY_REF, SECURITY_RATING_KEY, D);
     verifyAddedRawMeasure(MODULE_REF, SECURITY_RATING_KEY, E);
     verifyAddedRawMeasure(PROJECT_REF, SECURITY_RATING_KEY, E);
   }
index df37647cf66075190302cd0cc50e2d03537dbafc..25bf2b51bdcb7ad8d764f6f64ffd60f092d83503 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.ComponentVisitor;
 import org.sonar.server.computation.component.ViewsComponent;
 import org.sonar.server.computation.component.VisitorsCrawler;
+import org.sonar.server.computation.issue.ComponentIssuesRepositoryRule;
 import org.sonar.server.computation.measure.Measure;
 import org.sonar.server.computation.measure.MeasureRepositoryRule;
 import org.sonar.server.computation.metric.MetricRepositoryRule;
@@ -115,6 +116,9 @@ public class QualityModelMeasuresVisitorForViewsTest {
   @Rule
   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
 
+  @Rule
+  public ComponentIssuesRepositoryRule componentIssuesRepositoryRule = new ComponentIssuesRepositoryRule(treeRootHolder);
+
   private RatingSettings ratingSettings = mock(RatingSettings.class);
 
   private VisitorsCrawler underTest;
@@ -122,7 +126,7 @@ public class QualityModelMeasuresVisitorForViewsTest {
   @Before
   public void setUp() {
     when(ratingSettings.getRatingGrid()).thenReturn(new RatingGrid(RATING_GRID));
-    underTest = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new QualityModelMeasuresVisitor(metricRepository, measureRepository, ratingSettings, null)));
+    underTest = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new QualityModelMeasuresVisitor(metricRepository, measureRepository, ratingSettings, componentIssuesRepositoryRule)));
   }
 
   @Test