2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.computation.step;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.api.utils.System2;
31 import org.sonar.batch.protocol.output.BatchReport;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.metric.MetricDto;
36 import org.sonar.server.computation.batch.BatchReportReaderRule;
37 import org.sonar.server.computation.batch.TreeRootHolderRule;
38 import org.sonar.server.computation.component.Component;
39 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
40 import org.sonar.server.computation.component.ReportComponent;
41 import org.sonar.test.DbTests;
43 import static com.google.common.collect.Lists.newArrayList;
44 import static org.assertj.core.api.Assertions.assertThat;
46 @Category(DbTests.class)
47 public class PersistDuplicationsStepTest extends BaseStepTest {
49 private static final String PROJECT_KEY = "PROJECT_KEY";
52 public DbTester dbTester = DbTester.create(System2.INSTANCE);
55 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
58 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
60 DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
62 DbSession session = dbTester.getSession();
64 DbClient dbClient = dbTester.getDbClient();
66 PersistDuplicationsStep underTest;
70 dbTester.truncateTables();
71 underTest = new PersistDuplicationsStep(dbClient, dbIdsRepository, treeRootHolder, reportReader);
75 protected ComputationStep step() {
80 public void tearDown() {
85 public void nothing_to_do_when_no_duplication() {
86 saveDuplicationMetric();
87 initReportWithProjectAndFile();
91 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(0);
95 public void persist_duplications_on_same_file() {
96 MetricDto duplicationMetric = saveDuplicationMetric();
98 initReportWithProjectAndFile();
100 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
101 .setOriginPosition(BatchReport.TextRange.newBuilder()
105 .addDuplicate(BatchReport.Duplicate.newBuilder()
106 .setOtherFileRef(2).setRange(BatchReport.TextRange.newBuilder()
112 reportReader.putDuplications(2, newArrayList(duplication));
116 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
118 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", metric_id as \"metricId\", text_value as \"textValue\" from project_measures");
119 assertThat(dto.get("snapshotId")).isEqualTo(11L);
120 assertThat(dto.get("metricId")).isEqualTo(duplicationMetric.getId().longValue());
121 assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"5\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"5\" r=\"PROJECT_KEY:file\"/></g></duplications>");
125 public void persist_duplications_on_same_file_linked_on_a_module() {
126 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("CDEF").setKey("MODULE_KEY:file").build();
127 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(file).build();
128 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
129 treeRootHolder.setRoot(project);
131 dbIdsRepository.setComponentId(project, 1);
132 dbIdsRepository.setSnapshotId(project, 10);
133 dbIdsRepository.setComponentId(module, 3);
134 dbIdsRepository.setSnapshotId(module, 11);
135 dbIdsRepository.setComponentId(file, 2);
136 dbIdsRepository.setSnapshotId(file, 12);
138 saveDuplicationMetric();
140 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
141 .setOriginPosition(BatchReport.TextRange.newBuilder()
145 .addDuplicate(BatchReport.Duplicate.newBuilder()
146 .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
152 reportReader.putDuplications(3, newArrayList(duplication));
156 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
158 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
159 assertThat(dto.get("snapshotId")).isEqualTo(12L);
160 assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"5\" r=\"MODULE_KEY:file\"/><b s=\"6\" l=\"5\" r=\"MODULE_KEY:file\"/></g></duplications>");
164 public void persist_duplications_on_same_file_linked_on_a_folder() {
165 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("CDEF").setKey("PROJECT_KEY:file").build();
166 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("BCDE").setKey("PROJECT_KEY:dir").addChildren(file).build();
167 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
168 treeRootHolder.setRoot(project);
170 dbIdsRepository.setComponentId(project, 1);
171 dbIdsRepository.setSnapshotId(project, 10);
172 dbIdsRepository.setComponentId(directory, 3);
173 dbIdsRepository.setSnapshotId(directory, 11);
174 dbIdsRepository.setComponentId(file, 2);
175 dbIdsRepository.setSnapshotId(file, 12);
177 saveDuplicationMetric();
179 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
180 .setOriginPosition(BatchReport.TextRange.newBuilder()
184 .addDuplicate(BatchReport.Duplicate.newBuilder()
185 .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
191 reportReader.putDuplications(3, newArrayList(duplication));
195 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
197 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
198 assertThat(dto.get("snapshotId")).isEqualTo(12L);
199 assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"5\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"5\" r=\"PROJECT_KEY:file\"/></g></duplications>");
203 public void persist_duplications_on_same_file_linked_on_sub_folder() {
204 Component file = ReportComponent.builder(Component.Type.FILE, 10).setUuid("DEFG").setKey("PROJECT_KEY:file").build();
205 Component directory1 = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:dir1").addChildren(file).build();
206 Component directory2 = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("BCDE").setKey("PROJECT_KEY:dir2").addChildren(directory1).build();
207 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory2).build();
208 treeRootHolder.setRoot(project);
210 dbIdsRepository.setComponentId(project, 1);
211 dbIdsRepository.setSnapshotId(project, 10);
212 dbIdsRepository.setComponentId(directory1, 2);
213 dbIdsRepository.setSnapshotId(directory1, 11);
214 dbIdsRepository.setComponentId(directory2, 3);
215 dbIdsRepository.setSnapshotId(directory2, 12);
216 dbIdsRepository.setComponentId(file, 10);
217 dbIdsRepository.setSnapshotId(file, 20);
219 saveDuplicationMetric();
221 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
222 .setOriginPosition(BatchReport.TextRange.newBuilder()
226 .addDuplicate(BatchReport.Duplicate.newBuilder()
227 .setOtherFileRef(10).setRange(BatchReport.TextRange.newBuilder()
233 reportReader.putDuplications(10, newArrayList(duplication));
237 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
239 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
240 assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"5\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"5\" r=\"PROJECT_KEY:file\"/></g></duplications>");
244 public void persist_duplications_on_different_files() {
245 saveDuplicationMetric();
247 Component file2 = ReportComponent.builder(Component.Type.FILE, 3).setUuid("CDEF").setKey("PROJECT_KEY:file2").build();
248 Component file = ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").setKey("PROJECT_KEY:file").build();
249 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(file, file2).build();
250 treeRootHolder.setRoot(project);
252 dbIdsRepository.setComponentId(project, 1);
253 dbIdsRepository.setSnapshotId(project, 10);
254 dbIdsRepository.setComponentId(file, 2);
255 dbIdsRepository.setSnapshotId(file, 11);
256 dbIdsRepository.setComponentId(file2, 2);
257 dbIdsRepository.setSnapshotId(file2, 12);
259 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
260 .setOriginPosition(BatchReport.TextRange.newBuilder()
264 .addDuplicate(BatchReport.Duplicate.newBuilder()
265 .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
271 reportReader.putDuplications(2, newArrayList(duplication));
275 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
277 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
278 assertThat(dto.get("snapshotId")).isEqualTo(11L);
279 assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"5\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"5\" r=\"PROJECT_KEY:file2\"/></g></duplications>");
282 private void initReportWithProjectAndFile() {
283 Component file = ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").setKey("PROJECT_KEY:file").build();
284 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(file).build();
285 treeRootHolder.setRoot(project);
287 dbIdsRepository.setComponentId(project, 1);
288 dbIdsRepository.setSnapshotId(project, 10);
289 dbIdsRepository.setComponentId(file, 2);
290 dbIdsRepository.setSnapshotId(file, 11);
293 private MetricDto saveDuplicationMetric() {
294 MetricDto duplicationMetric = new MetricDto().setKey(CoreMetrics.DUPLICATIONS_DATA_KEY)
295 .setOptimizedBestValue(false)
296 .setDeleteHistoricalData(false)
298 dbClient.metricDao().insert(session, duplicationMetric);
300 return duplicationMetric;