]> source.dussan.org Git - sonarqube.git/blob
0da0ba15dd452e94335c1058be74271931c6c66b
[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 package org.sonar.server.computation.step;
21
22 import java.util.Arrays;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.batch.protocol.output.BatchReport;
27 import org.sonar.server.computation.batch.BatchReportReaderRule;
28 import org.sonar.server.computation.batch.TreeRootHolderRule;
29 import org.sonar.server.computation.component.Component;
30 import org.sonar.server.computation.duplication.Duplicate;
31 import org.sonar.server.computation.duplication.Duplication;
32 import org.sonar.server.computation.duplication.DuplicationRepositoryRule;
33 import org.sonar.server.computation.duplication.InProjectDuplicate;
34 import org.sonar.server.computation.duplication.InnerDuplicate;
35 import org.sonar.server.computation.duplication.TextBlock;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.component.Component.Type.FILE;
39 import static org.sonar.server.computation.component.Component.Type.PROJECT;
40 import static org.sonar.server.computation.component.ReportComponent.builder;
41
42 public class LoadDuplicationsFromReportStepTest {
43   private static final int LINE = 2;
44   private static final int OTHER_LINE = 300;
45   private static final int ROOT_REF = 1;
46   private static final int FILE_1_REF = 11;
47   private static final int FILE_2_REF = 12;
48
49   @Rule
50   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
51     builder(PROJECT, ROOT_REF)
52       .addChildren(
53         builder(FILE, FILE_1_REF).build(),
54         builder(FILE, FILE_2_REF).build()
55       )
56       .build()
57     );
58   @Rule
59   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
60   @Rule
61   public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
62   @Rule
63   public ExpectedException expectedException = ExpectedException.none();
64
65   private LoadDuplicationsFromReportStep underTest = new LoadDuplicationsFromReportStep(treeRootHolder, reportReader, duplicationRepository);
66
67   @Test
68   public void verify_description() {
69     assertThat(underTest.getDescription()).isEqualTo("Load inner file and in project duplications");
70   }
71
72   @Test
73   public void loads_no_duplications_if_reader_has_no_duplication() {
74     underTest.execute();
75
76     assertNoDuplication(FILE_1_REF);
77   }
78
79   @Test
80   public void loads_duplication_without_otherFileRef_as_inner_duplication() {
81     reportReader.putDuplications(FILE_2_REF, createDuplication(singleLineTextRange(LINE), createInnerDuplicate(LINE + 1)));
82
83     underTest.execute();
84
85     assertNoDuplication(FILE_1_REF);
86     assertDuplications(FILE_2_REF, singleLineTextBlock(LINE), new InnerDuplicate(singleLineTextBlock(LINE + 1)));
87   }
88
89   @Test
90   public void loads_duplication_with_otherFileRef_as_inProject_duplication() {
91     reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(LINE), createInProjectDuplicate(FILE_2_REF, LINE + 1)));
92
93     underTest.execute();
94
95     assertDuplications(FILE_1_REF, singleLineTextBlock(LINE), new InProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1)));
96     assertNoDuplication(FILE_2_REF);
97   }
98
99   @Test
100   public void loads_multiple_duplications_with_multiple_duplicates() {
101     reportReader.putDuplications(
102       FILE_2_REF,
103       createDuplication(
104         singleLineTextRange(LINE),
105         createInnerDuplicate(LINE + 1), createInnerDuplicate(LINE + 2), createInProjectDuplicate(FILE_1_REF, LINE), createInProjectDuplicate(FILE_1_REF, LINE + 10)),
106       createDuplication(
107         singleLineTextRange(OTHER_LINE),
108         createInProjectDuplicate(FILE_1_REF, OTHER_LINE)),
109       createDuplication(
110         singleLineTextRange(OTHER_LINE + 80),
111         createInnerDuplicate(LINE), createInnerDuplicate(LINE + 10))
112       );
113
114     underTest.execute();
115
116     Component file1Component = treeRootHolder.getComponentByRef(FILE_1_REF);
117     assertThat(duplicationRepository.getDuplications(FILE_2_REF)).containsOnly(
118       duplication(
119         singleLineTextBlock(LINE),
120         new InnerDuplicate(singleLineTextBlock(LINE + 1)), new InnerDuplicate(singleLineTextBlock(LINE + 2)), new InProjectDuplicate(file1Component, singleLineTextBlock(LINE)),
121         new InProjectDuplicate(file1Component, singleLineTextBlock(LINE + 10))),
122       duplication(
123         singleLineTextBlock(OTHER_LINE),
124         new InProjectDuplicate(file1Component, singleLineTextBlock(OTHER_LINE))
125       ),
126       duplication(
127         singleLineTextBlock(OTHER_LINE + 80),
128         new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10))
129       )
130       );
131   }
132
133   @Test
134   public void loads_duplication_with_otherFileRef_throws_IAE_if_component_does_not_exist() {
135     int line = 2;
136     reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(666, line + 1)));
137
138     expectedException.expect(IllegalArgumentException.class);
139     expectedException.expectMessage("Component with ref '666' hasn't been found");
140
141     underTest.execute();
142   }
143
144   @Test
145   public void loads_duplication_with_otherFileRef_throws_IAE_if_references_itself() {
146     int line = 2;
147     reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(FILE_1_REF, line + 1)));
148
149     expectedException.expect(IllegalArgumentException.class);
150     expectedException.expectMessage("file and otherFile Components can not be the same");
151
152     underTest.execute();
153   }
154
155   private void assertDuplications(int fileRef, TextBlock original, Duplicate... duplicates) {
156     assertThat(duplicationRepository.getDuplications(fileRef)).containsExactly(duplication(original, duplicates));
157   }
158
159   private static Duplication duplication(TextBlock original, Duplicate... duplicates) {
160     return new Duplication(original, Arrays.asList(duplicates));
161   }
162
163   private TextBlock singleLineTextBlock(int line) {
164     return new TextBlock(line, line);
165   }
166
167   private static BatchReport.Duplication createDuplication(BatchReport.TextRange original, BatchReport.Duplicate... duplicates) {
168     BatchReport.Duplication.Builder builder = BatchReport.Duplication.newBuilder()
169       .setOriginPosition(original);
170     for (BatchReport.Duplicate duplicate : duplicates) {
171       builder.addDuplicate(duplicate);
172     }
173     return builder.build();
174   }
175
176   private static BatchReport.Duplicate createInnerDuplicate(int line) {
177     return BatchReport.Duplicate.newBuilder()
178       .setRange(singleLineTextRange(line))
179       .build();
180   }
181
182   private static BatchReport.Duplicate createInProjectDuplicate(int componentRef, int line) {
183     return BatchReport.Duplicate.newBuilder()
184       .setOtherFileRef(componentRef)
185       .setRange(singleLineTextRange(line))
186       .build();
187   }
188
189   private static BatchReport.TextRange singleLineTextRange(int line) {
190     return BatchReport.TextRange.newBuilder()
191       .setStartLine(line)
192       .setEndLine(line)
193       .build();
194   }
195
196   private void assertNoDuplication(int fileRef) {
197     assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty();
198   }
199 }