From 000a8e84e0495628c282640fe62dd8cc84f98800 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Tue, 29 Mar 2022 14:05:47 +0200 Subject: [PATCH] SONAR-16039 populating measure for new metric analysis_from_sonarqube_9_4 --- .../AnalysisFromSonarQube94Visitor.java | 65 +++++++++++++++ ...ProjectAnalysisTaskContainerPopulator.java | 2 + .../task/projectanalysis/measure/Measure.java | 15 ++++ .../AnalysisFromSonarQube94VisitorTest.java | 83 +++++++++++++++++++ .../projectanalysis/measure/MeasureTest.java | 18 ++++ 5 files changed, 183 insertions(+) create mode 100644 server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94Visitor.java create mode 100644 server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94VisitorTest.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 index 00000000000..d9dcfb07d2a --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94Visitor.java @@ -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 { + + 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 path) { + measureRepository.add(project, analysisFromSonarQube94Metric, Measure.newMeasureBuilder().create(path.current().sonarQube94OrGreater)); + } + + public static final class AnalysisFromSonarQube94StackFactory extends SimpleStackElementFactory { + + @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; + } +} diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java index faa28a22ba6..e236cfdd0b3 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java @@ -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, diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/Measure.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/Measure.java index 4159f8c57bd..1be2e0f1e50 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/Measure.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/Measure.java @@ -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 index 00000000000..19bd1e91961 --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisFromSonarQube94VisitorTest.java @@ -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 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(); + } +} diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureTest.java index b6351ed0c65..cfb2b5f308e 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureTest.java @@ -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(); + } + } -- 2.39.5