]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16039 populating measure for new metric analysis_from_sonarqube_9_4
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Tue, 29 Mar 2022 12:05:47 +0000 (14:05 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 31 Mar 2022 20:02:59 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94Visitor.java [new file with mode: 0644]
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/Measure.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94VisitorTest.java [new file with mode: 0644]
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureTest.java

diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94Visitor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94Visitor.java
new file mode 100644 (file)
index 0000000..d9dcfb0
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.ce.task.projectanalysis.analysis;
+
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
+import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
+import org.sonar.ce.task.projectanalysis.measure.Measure;
+import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
+import org.sonar.ce.task.projectanalysis.metric.Metric;
+import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
+
+public class AnalysisFromSonarQube94Visitor extends PathAwareVisitorAdapter<AnalysisFromSonarQube94Visitor.AnalysisFromSonarQube94> {
+
+  private final MeasureRepository measureRepository;
+  private final Metric analysisFromSonarQube94Metric;
+
+  public AnalysisFromSonarQube94Visitor(MetricRepository metricRepository, MeasureRepository measureRepository) {
+    super(CrawlerDepthLimit.PROJECT, Order.PRE_ORDER, new AnalysisFromSonarQube94StackFactory());
+
+    this.measureRepository = measureRepository;
+    this.analysisFromSonarQube94Metric = metricRepository.getByKey(CoreMetrics.ANALYSIS_FROM_SONARQUBE_9_4_KEY);
+  }
+
+  @Override
+  public void visitProject(Component project, Path<AnalysisFromSonarQube94Visitor.AnalysisFromSonarQube94> path) {
+    measureRepository.add(project, analysisFromSonarQube94Metric, Measure.newMeasureBuilder().create(path.current().sonarQube94OrGreater));
+  }
+
+  public static final class AnalysisFromSonarQube94StackFactory extends SimpleStackElementFactory<AnalysisFromSonarQube94> {
+
+    @Override
+    public AnalysisFromSonarQube94 createForAny(Component component) {
+      return new AnalysisFromSonarQube94();
+    }
+
+    /** Stack item is not used at ProjectView level, saves on instantiating useless objects */
+    @Override
+    public AnalysisFromSonarQube94 createForProjectView(Component projectView) {
+      return null;
+    }
+  }
+
+  public static final class AnalysisFromSonarQube94 {
+    final boolean sonarQube94OrGreater = true;
+  }
+}
index faa28a22ba6aac13cb32b642897ed227c5e19116..e236cfdd0b34ba1cb36f9f23c7f6c6f5386de829 100644 (file)
@@ -25,6 +25,7 @@ import javax.annotation.Nullable;
 import org.sonar.ce.task.CeTask;
 import org.sonar.ce.task.container.TaskContainer;
 import org.sonar.ce.task.log.CeTaskMessagesImpl;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisFromSonarQube94Visitor;
 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderImpl;
 import org.sonar.ce.task.projectanalysis.api.posttask.PostProjectAnalysisTasksExecutor;
 import org.sonar.ce.task.projectanalysis.batch.BatchReportDirectoryHolderImpl;
@@ -270,6 +271,7 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop
       IssueOnReferenceBranchVisitor.class,
 
       // visitors : order is important, measure computers must be executed at the end in order to access to every measures / issues
+      AnalysisFromSonarQube94Visitor.class,
       LoadComponentUuidsHavingOpenIssuesVisitor.class,
       IntegrateIssuesVisitor.class,
       CloseIssuesOnRemovedComponentsVisitor.class,
index 4159f8c57bd0039bd8334481eadf76a351c72195..1be2e0f1e50711a4f328841cefc7bfd5e141a0f6 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.measure;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.Locale;
+import java.util.Objects;
 import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -348,7 +349,21 @@ public interface Measure {
     @Override
     public double getVariation() {
       throw new IllegalStateException("Measure does not have variation");
+    }
 
+    @Override
+    public boolean equals(Object o) {
+      if (this == o)
+        return true;
+      if (o == null || getClass() != o.getClass())
+        return false;
+      ValueMeasureImpl that = (ValueMeasureImpl) o;
+      return valueType == that.valueType && Objects.equals(value, that.value);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(valueType, value);
     }
 
     @Override
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94VisitorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94VisitorTest.java
new file mode 100644 (file)
index 0000000..19bd1e9
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.ce.task.projectanalysis.analysis;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisFromSonarQube94Visitor.AnalysisFromSonarQube94;
+import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.PathAwareVisitor;
+import org.sonar.ce.task.projectanalysis.component.ReportAttributes;
+import org.sonar.ce.task.projectanalysis.measure.Measure;
+import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
+import org.sonar.ce.task.projectanalysis.metric.Metric;
+import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.sonar.ce.task.projectanalysis.analysis.AnalysisFromSonarQube94Visitor.*;
+import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
+import static org.sonar.ce.task.projectanalysis.component.ComponentImpl.builder;
+
+public class AnalysisFromSonarQube94VisitorTest {
+
+  private final MeasureRepository measureRepository = mock(MeasureRepository.class);
+  private final MetricRepository metricRepository = mock(MetricRepository.class);
+
+  private final Metric metric = mock(Metric.class);
+
+  private AnalysisFromSonarQube94Visitor underTest;
+
+  @Before
+  public void before() {
+    when(metricRepository.getByKey(anyString())).thenReturn(metric);
+    underTest = new AnalysisFromSonarQube94Visitor(metricRepository, measureRepository);
+  }
+
+  @Test
+  public void visitProject_createMeasureForMetric() {
+    Component project = builder(FILE).setUuid("uuid")
+      .setDbKey("dbKey")
+      .setName("name")
+      .setStatus(Component.Status.SAME)
+      .setReportAttributes(mock(ReportAttributes.class))
+      .build();
+
+    PathAwareVisitor.Path<AnalysisFromSonarQube94> path = mock(PathAwareVisitor.Path.class);
+    when(path.current()).thenReturn(new AnalysisFromSonarQube94());
+
+    underTest.visitProject(project, path);
+
+    Measure expectedMeasure = Measure.newMeasureBuilder().create(true);
+    verify(measureRepository).add(project, metric, expectedMeasure);
+  }
+
+  @Test
+  public void analysisFromSonarQube94StackFactoryCreateForAny_shouldAlwaysReturnAnalysisFromSonarQube94() {
+    AnalysisFromSonarQube94StackFactory factory = new AnalysisFromSonarQube94StackFactory();
+
+    AnalysisFromSonarQube94 analysisFromSonarQube94 = factory.createForAny(mock(Component.class));
+
+    assertThat(analysisFromSonarQube94.sonarQube94OrGreater).isTrue();
+  }
+}
index b6351ed0c6517ecf060afa7156957af685e87e8c..cfb2b5f308e124ce2884208ccaaa156f0e1616f7 100644 (file)
@@ -305,4 +305,22 @@ public class MeasureTest {
       .hasMessage("NaN is not allowed as a Measure value");
   }
 
+  @Test
+  public void valueMeasureImplEquals_instanceNotEqualToNull() {
+    Measure.ValueMeasureImpl valueMeasureImpl = (Measure.ValueMeasureImpl) new Measure.NewMeasureBuilder().create(0, null);
+
+    boolean equal = valueMeasureImpl.equals(null);
+
+    assertThat(equal).isFalse();
+  }
+
+  @Test
+  public void valueMeasureImplEquals_sameInstance_returnTrue() {
+    Measure.ValueMeasureImpl valueMeasureImpl = (Measure.ValueMeasureImpl) new Measure.NewMeasureBuilder().create(0, null);
+
+    boolean equal = valueMeasureImpl.equals(valueMeasureImpl);
+
+    assertThat(equal).isTrue();
+  }
+
 }