]> source.dussan.org Git - sonarqube.git/blob
f5135948eb19955a3138c2b16321726542f91c96
[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   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;
53
54   @Rule
55   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
56   @Rule
57   public DbTester dbTester = DbTester.create(System2.INSTANCE);
58   @Rule
59   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
60
61   DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
62
63   DbSession session = dbTester.getSession();
64
65   DbClient dbClient = dbTester.getDbClient();
66
67   PersistDuplicationsStep underTest;
68
69   @Before
70   public void setup() {
71     dbTester.truncateTables();
72     underTest = new PersistDuplicationsStep(dbClient, dbIdsRepository, treeRootHolder, reportReader);
73   }
74
75   @Override
76   protected ComputationStep step() {
77     return underTest;
78   }
79
80   @After
81   public void tearDown() {
82     session.close();
83   }
84
85   @Test
86   public void nothing_to_do_when_no_duplication() {
87     saveDuplicationMetric();
88     initReportWithProjectAndFile();
89
90     underTest.execute();
91
92     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(0);
93   }
94
95   @Test
96   public void persist_duplications_on_same_file() {
97     MetricDto duplicationMetric = saveDuplicationMetric();
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             .setRange(BatchReport.TextRange.newBuilder()
107                 .setStartLine(6)
108                 .setEndLine(10)
109                 .build())
110             .build())
111         .build();
112     reportReader.putDuplications(FILE_1_REF, 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_different_files() {
126     saveDuplicationMetric();
127     initReportWithProjectAndFile();
128
129     BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
130         .setOriginPosition(BatchReport.TextRange.newBuilder()
131             .setStartLine(1)
132             .setEndLine(5)
133             .build())
134         .addDuplicate(BatchReport.Duplicate.newBuilder()
135             .setOtherFileRef(FILE_2_REF).setRange(BatchReport.TextRange.newBuilder()
136                 .setStartLine(6)
137                 .setEndLine(10)
138                 .build())
139             .build())
140         .build();
141     reportReader.putDuplications(FILE_1_REF, newArrayList(duplication));
142
143     underTest.execute();
144
145     assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
146
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>");
150   }
151
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);
157
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);
164   }
165
166   private MetricDto saveDuplicationMetric() {
167     MetricDto duplicationMetric = new MetricDto().setKey(CoreMetrics.DUPLICATIONS_DATA_KEY)
168         .setOptimizedBestValue(false)
169         .setDeleteHistoricalData(false)
170         .setHidden(false);
171     dbClient.metricDao().insert(session, duplicationMetric);
172     session.commit();
173     return duplicationMetric;
174   }
175
176 }