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.
20 package org.sonar.server.computation.step;
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;
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;
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;
50 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
51 builder(PROJECT, ROOT_REF)
53 builder(FILE, FILE_1_REF).build(),
54 builder(FILE, FILE_2_REF).build()
59 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
61 public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
63 public ExpectedException expectedException = ExpectedException.none();
65 private LoadDuplicationsFromReportStep underTest = new LoadDuplicationsFromReportStep(treeRootHolder, reportReader, duplicationRepository);
68 public void verify_description() {
69 assertThat(underTest.getDescription()).isEqualTo("Load inner file and in project duplications");
73 public void loads_no_duplications_if_reader_has_no_duplication() {
76 assertNoDuplication(FILE_1_REF);
80 public void loads_duplication_without_otherFileRef_as_inner_duplication() {
81 reportReader.putDuplications(FILE_2_REF, createDuplication(singleLineTextRange(LINE), createInnerDuplicate(LINE + 1)));
85 assertNoDuplication(FILE_1_REF);
86 assertDuplications(FILE_2_REF, singleLineTextBlock(LINE), new InnerDuplicate(singleLineTextBlock(LINE + 1)));
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)));
95 assertDuplications(FILE_1_REF, singleLineTextBlock(LINE), new InProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1)));
96 assertNoDuplication(FILE_2_REF);
100 public void loads_multiple_duplications_with_multiple_duplicates() {
101 reportReader.putDuplications(
104 singleLineTextRange(LINE),
105 createInnerDuplicate(LINE + 1), createInnerDuplicate(LINE + 2), createInProjectDuplicate(FILE_1_REF, LINE), createInProjectDuplicate(FILE_1_REF, LINE + 10)),
107 singleLineTextRange(OTHER_LINE),
108 createInProjectDuplicate(FILE_1_REF, OTHER_LINE)),
110 singleLineTextRange(OTHER_LINE + 80),
111 createInnerDuplicate(LINE), createInnerDuplicate(LINE + 10))
116 Component file1Component = treeRootHolder.getComponentByRef(FILE_1_REF);
117 assertThat(duplicationRepository.getDuplications(FILE_2_REF)).containsOnly(
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))),
123 singleLineTextBlock(OTHER_LINE),
124 new InProjectDuplicate(file1Component, singleLineTextBlock(OTHER_LINE))
127 singleLineTextBlock(OTHER_LINE + 80),
128 new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10))
134 public void loads_duplication_with_otherFileRef_throws_IAE_if_component_does_not_exist() {
136 reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(666, line + 1)));
138 expectedException.expect(IllegalArgumentException.class);
139 expectedException.expectMessage("Component with ref '666' hasn't been found");
145 public void loads_duplication_with_otherFileRef_throws_IAE_if_references_itself() {
147 reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(FILE_1_REF, line + 1)));
149 expectedException.expect(IllegalArgumentException.class);
150 expectedException.expectMessage("file and otherFile Components can not be the same");
155 private void assertDuplications(int fileRef, TextBlock original, Duplicate... duplicates) {
156 assertThat(duplicationRepository.getDuplications(fileRef)).containsExactly(duplication(original, duplicates));
159 private static Duplication duplication(TextBlock original, Duplicate... duplicates) {
160 return new Duplication(original, Arrays.asList(duplicates));
163 private TextBlock singleLineTextBlock(int line) {
164 return new TextBlock(line, line);
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);
173 return builder.build();
176 private static BatchReport.Duplicate createInnerDuplicate(int line) {
177 return BatchReport.Duplicate.newBuilder()
178 .setRange(singleLineTextRange(line))
182 private static BatchReport.Duplicate createInProjectDuplicate(int componentRef, int line) {
183 return BatchReport.Duplicate.newBuilder()
184 .setOtherFileRef(componentRef)
185 .setRange(singleLineTextRange(line))
189 private static BatchReport.TextRange singleLineTextRange(int line) {
190 return BatchReport.TextRange.newBuilder()
196 private void assertNoDuplication(int fileRef) {
197 assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty();