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";
50 private static final int FILE_1_REF = 2;
51 private static final int FILE_2_REF = 3;
52 private static final int ROOT_REF = 1;
55 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
57 public DbTester dbTester = DbTester.create(System2.INSTANCE);
59 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
61 DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
63 DbSession session = dbTester.getSession();
65 DbClient dbClient = dbTester.getDbClient();
67 PersistDuplicationsStep underTest;
71 dbTester.truncateTables();
72 underTest = new PersistDuplicationsStep(dbClient, dbIdsRepository, treeRootHolder, reportReader);
76 protected ComputationStep step() {
81 public void tearDown() {
86 public void nothing_to_do_when_no_duplication() {
87 saveDuplicationMetric();
88 initReportWithProjectAndFile();
92 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(0);
96 public void persist_duplications_on_same_file() {
97 MetricDto duplicationMetric = saveDuplicationMetric();
98 initReportWithProjectAndFile();
100 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
101 .setOriginPosition(BatchReport.TextRange.newBuilder()
105 .addDuplicate(BatchReport.Duplicate.newBuilder()
106 .setRange(BatchReport.TextRange.newBuilder()
112 reportReader.putDuplications(FILE_1_REF, 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_different_files() {
126 saveDuplicationMetric();
127 initReportWithProjectAndFile();
129 BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
130 .setOriginPosition(BatchReport.TextRange.newBuilder()
134 .addDuplicate(BatchReport.Duplicate.newBuilder()
135 .setOtherFileRef(FILE_2_REF).setRange(BatchReport.TextRange.newBuilder()
141 reportReader.putDuplications(FILE_1_REF, newArrayList(duplication));
145 assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
147 Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
148 assertThat(dto.get("snapshotId")).isEqualTo(11L);
149 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>");
152 private void initReportWithProjectAndFile() {
153 Component file1 = ReportComponent.builder(Component.Type.FILE, FILE_1_REF).setUuid("BCDE").setKey("PROJECT_KEY:file").build();
154 Component file2 = ReportComponent.builder(Component.Type.FILE, FILE_2_REF).setUuid("CDEF").setKey("PROJECT_KEY:file2").build();
155 Component project = ReportComponent.builder(Component.Type.PROJECT, ROOT_REF).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(file1, file2).build();
156 treeRootHolder.setRoot(project);
158 dbIdsRepository.setComponentId(project, 1);
159 dbIdsRepository.setSnapshotId(project, 10);
160 dbIdsRepository.setComponentId(file1, 2);
161 dbIdsRepository.setSnapshotId(file1, 11);
162 dbIdsRepository.setComponentId(file2, 2);
163 dbIdsRepository.setSnapshotId(file2, 12);
166 private MetricDto saveDuplicationMetric() {
167 MetricDto duplicationMetric = new MetricDto().setKey(CoreMetrics.DUPLICATIONS_DATA_KEY)
168 .setOptimizedBestValue(false)
169 .setDeleteHistoricalData(false)
171 dbClient.metricDao().insert(session, duplicationMetric);
173 return duplicationMetric;