]> source.dussan.org Git - sonarqube.git/blob
40611e6367a74045d3d8276ccc7ea1e91b30fab7
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 package org.sonar.server.computation.step;
22
23 import java.util.Map;
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;
42
43 import static com.google.common.collect.Lists.newArrayList;
44 import static org.assertj.core.api.Assertions.assertThat;
45
46 @Category(DbTests.class)
47 public class PersistDuplicationsStepTest extends BaseStepTest {
48
49   private static final String PROJECT_KEY = "PROJECT_KEY";
50
51   @Rule
52   public DbTester dbTester = DbTester.create(System2.INSTANCE);
53
54   @Rule
55   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
56
57   @Rule
58   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
59
60   DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
61
62   DbSession session = dbTester.getSession();
63
64   DbClient dbClient = dbTester.getDbClient();
65
66   PersistDuplicationsStep underTest;
67
68   @Before
69   public void setup() {
70     dbTester.truncateTables();
71     underTest = new PersistDuplicationsStep(dbClient, dbIdsRepository, treeRootHolder, reportReader);
72   }
73
74   @Override
75   protected ComputationStep step() {
76     return underTest;
77   }
78
79   @After
80   public void tearDown() {
81     session.close();
82   }
83
84   @Test
85   public void nothing_to_do_when_no_duplication() {
86     saveDuplicationMetric();
87     initReportWithProjectAndFile();
88
89     underTest.execute();
90
91     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(0);
92   }
93
94   @Test
95   public void persist_duplications_on_same_file() {
96     MetricDto duplicationMetric = saveDuplicationMetric();
97
98     initReportWithProjectAndFile();
99
100     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
101       .setOriginPosition(BatchReport.TextRange.newBuilder()
102         .setStartLine(1)
103         .setEndLine(5)
104         .build())
105       .addDuplicate(BatchReport.Duplicate.newBuilder()
106         .setOtherFileRef(2).setRange(BatchReport.TextRange.newBuilder()
107           .setStartLine(6)
108           .setEndLine(10)
109           .build())
110         .build())
111       .build();
112     reportReader.putDuplications(2, newArrayList(duplication));
113
114     underTest.execute();
115
116     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
117
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>");
122   }
123
124   @Test
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);
130
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);
137
138     saveDuplicationMetric();
139
140     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
141       .setOriginPosition(BatchReport.TextRange.newBuilder()
142         .setStartLine(1)
143         .setEndLine(5)
144         .build())
145       .addDuplicate(BatchReport.Duplicate.newBuilder()
146         .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
147           .setStartLine(6)
148           .setEndLine(10)
149           .build())
150         .build())
151       .build();
152     reportReader.putDuplications(3, newArrayList(duplication));
153
154     underTest.execute();
155
156     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
157
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>");
161   }
162
163   @Test
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);
169
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);
176
177     saveDuplicationMetric();
178
179     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
180       .setOriginPosition(BatchReport.TextRange.newBuilder()
181         .setStartLine(1)
182         .setEndLine(5)
183         .build())
184       .addDuplicate(BatchReport.Duplicate.newBuilder()
185         .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
186           .setStartLine(6)
187           .setEndLine(10)
188           .build())
189         .build())
190       .build();
191     reportReader.putDuplications(3, newArrayList(duplication));
192
193     underTest.execute();
194
195     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
196
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>");
200   }
201
202   @Test
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);
209
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);
218
219     saveDuplicationMetric();
220
221     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
222       .setOriginPosition(BatchReport.TextRange.newBuilder()
223         .setStartLine(1)
224         .setEndLine(5)
225         .build())
226       .addDuplicate(BatchReport.Duplicate.newBuilder()
227         .setOtherFileRef(10).setRange(BatchReport.TextRange.newBuilder()
228           .setStartLine(6)
229           .setEndLine(10)
230           .build())
231         .build())
232       .build();
233     reportReader.putDuplications(10, newArrayList(duplication));
234
235     underTest.execute();
236
237     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
238
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>");
241   }
242
243   @Test
244   public void persist_duplications_on_different_files() {
245     saveDuplicationMetric();
246
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);
251
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);
258
259     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
260       .setOriginPosition(BatchReport.TextRange.newBuilder()
261         .setStartLine(1)
262         .setEndLine(5)
263         .build())
264       .addDuplicate(BatchReport.Duplicate.newBuilder()
265         .setOtherFileRef(3).setRange(BatchReport.TextRange.newBuilder()
266           .setStartLine(6)
267           .setEndLine(10)
268           .build())
269         .build())
270       .build();
271     reportReader.putDuplications(2, newArrayList(duplication));
272
273     underTest.execute();
274
275     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
276
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>");
280   }
281
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);
286
287     dbIdsRepository.setComponentId(project, 1);
288     dbIdsRepository.setSnapshotId(project, 10);
289     dbIdsRepository.setComponentId(file, 2);
290     dbIdsRepository.setSnapshotId(file, 11);
291   }
292
293   private MetricDto saveDuplicationMetric() {
294     MetricDto duplicationMetric = new MetricDto().setKey(CoreMetrics.DUPLICATIONS_DATA_KEY)
295       .setOptimizedBestValue(false)
296       .setDeleteHistoricalData(false)
297       .setHidden(false);
298     dbClient.metricDao().insert(session, duplicationMetric);
299     session.commit();
300     return duplicationMetric;
301   }
302
303 }