3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
30 import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
32 import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
33 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
34 import org.sonar.ce.task.step.ComputationStep;
35 import org.sonar.ce.task.step.TestComputationStepContext;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.component.ComponentDto;
38 import org.sonar.db.measure.LiveMeasureDto;
39 import org.sonar.db.metric.MetricDto;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY;
43 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
44 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
45 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
47 public class PersistDuplicationDataStepIT extends BaseStepTest {
49 private static final int ROOT_REF = 1;
50 private static final String PROJECT_KEY = "PROJECT_KEY";
51 private static final String PROJECT_UUID = "u1";
53 private static final int FILE_1_REF = 2;
54 private static final String FILE_1_KEY = "FILE_1_KEY";
55 private static final String FILE_1_UUID = "u2";
57 private static final int FILE_2_REF = 3;
58 private static final String FILE_2_KEY = "FILE_2_KEY";
59 private static final String FILE_2_UUID = "u3";
62 public DbTester db = DbTester.create(System2.INSTANCE);
64 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
66 builder(PROJECT, ROOT_REF).setKey(PROJECT_KEY).setUuid(PROJECT_UUID)
68 builder(FILE, FILE_1_REF).setKey(FILE_1_KEY).setUuid(FILE_1_UUID)
70 builder(FILE, FILE_2_REF).setKey(FILE_2_KEY).setUuid(FILE_2_UUID)
75 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
77 public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
79 public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
83 MetricDto metric = db.measures().insertMetric(m -> m.setKey(DUPLICATIONS_DATA_KEY).setValueType(Metric.ValueType.STRING.name()));
84 insertComponent(PROJECT_KEY, PROJECT_UUID);
85 insertComponent(FILE_1_KEY, FILE_1_UUID);
86 insertComponent(FILE_2_KEY, FILE_2_UUID);
88 metricRepository.add(metric.getUuid(), new Metric.Builder(DUPLICATIONS_DATA_KEY, DUPLICATIONS_DATA_KEY, Metric.ValueType.STRING).create());
92 protected ComputationStep step() {
97 public void nothing_to_persist_when_no_duplication() {
98 TestComputationStepContext context = new TestComputationStepContext();
100 underTest().execute(context);
102 assertThatNothingPersisted();
103 verifyStatistics(context, 0);
107 public void compute_duplications_on_same_file() {
108 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), new TextBlock(6, 10));
109 TestComputationStepContext context = new TestComputationStepContext();
111 underTest().execute(context);
113 assertThat(selectMeasureData(FILE_1_UUID)).hasValue("<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
114 + FILE_1_KEY + "\"/></g></duplications>");
115 assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
116 assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
120 public void compute_duplications_on_different_files() {
121 duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
122 TestComputationStepContext context = new TestComputationStepContext();
124 underTest().execute(context);
126 assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
127 "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
128 + FILE_2_KEY + "\"/></g></duplications>");
129 assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
130 assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
134 public void compute_duplications_on_unchanged_file() {
135 duplicationRepository.addExtendedProjectDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
136 TestComputationStepContext context = new TestComputationStepContext();
138 underTest().execute(context);
140 assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
141 "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"true\" r=\""
142 + FILE_2_KEY + "\"/></g></duplications>");
143 assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
144 assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
148 public void compute_duplications_on_different_projects() {
149 String fileKeyFromOtherProject = "PROJECT2_KEY:file2";
150 duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
151 TestComputationStepContext context = new TestComputationStepContext();
153 underTest().execute(context);
155 assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
156 "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
157 + fileKeyFromOtherProject + "\"/></g></duplications>");
158 assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
159 assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
162 private PersistDuplicationDataStep underTest() {
163 return new PersistDuplicationDataStep(db.getDbClient(), treeRootHolder, metricRepository, duplicationRepository,
164 new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder));
167 private void assertThatNothingPersisted() {
168 assertThat(db.countRowsOfTable(db.getSession(), "live_measures")).isZero();
171 private Optional<String> selectMeasureData(String componentUuid) {
172 return db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), componentUuid, "duplications_data")
173 .map(LiveMeasureDto::getTextValue);
176 private ComponentDto insertComponent(String key, String uuid) {
177 ComponentDto componentDto = new ComponentDto()
180 .setUuidPath(uuid + ".")
181 .setBranchUuid(uuid);
182 db.components().insertComponent(componentDto);
186 private static void verifyStatistics(TestComputationStepContext context, int expectedInsertsOrUpdates) {
187 context.getStatistics().assertValue("insertsOrUpdates", expectedInsertsOrUpdates);