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.api.batch.sensor.duplication.internal;
22 import org.junit.Test;
23 import org.sonar.api.batch.fs.internal.DefaultInputFile;
24 import org.sonar.api.batch.sensor.duplication.Duplication.Block;
26 import static org.assertj.core.api.Assertions.assertThat;
28 public class DefaultDuplicationTest {
31 public void testDuplicationEqualsAndCo() {
32 DefaultInputFile file1 = new DefaultInputFile("foo", "bar.txt").setLines(50);
33 DefaultInputFile file2 = new DefaultInputFile("foo", "bar2.txt").setLines(50);
34 DefaultDuplication d1 = new DefaultDuplication()
35 .originBlock(file1, 1, 10)
36 .isDuplicatedBy(file1, 20, 29)
37 .isDuplicatedBy(file2, 1, 10);
38 DefaultDuplication d2 = new DefaultDuplication()
39 .originBlock(file1, 1, 10)
40 .isDuplicatedBy(file1, 20, 29)
41 .isDuplicatedBy(file2, 1, 10);
42 DefaultDuplication d3 = new DefaultDuplication()
43 .originBlock(file1, 1, 10);
44 assertThat(d1).isEqualTo(d1);
45 assertThat(d1).isEqualTo(d2);
46 assertThat(d1).isNotEqualTo("");
47 assertThat(d1).isNotEqualTo(d3);
49 assertThat(d1.hashCode()).isNotNull();
50 assertThat(d1.toString()).contains("origin=Duplication.Block[resourceKey=foo:bar.txt,startLine=1,length=10]");
51 assertThat(d1.toString()).contains(
52 "duplicates=[Duplication.Block[resourceKey=foo:bar.txt,startLine=20,length=10], Duplication.Block[resourceKey=foo:bar2.txt,startLine=1,length=10]]");
58 DefaultInputFile file1 = new DefaultInputFile("foo", "foo.php").setLines(50);
59 DefaultInputFile file2 = new DefaultInputFile("foo", "foo2.php").setLines(50);
61 DefaultDuplication dup = new DefaultDuplication().originBlock(file1, 1, 11)
62 .isDuplicatedBy(file1, 40, 50)
63 .isDuplicatedBy(file2, 1, 10);
65 Block originBlock = dup.originBlock();
66 assertThat(originBlock.resourceKey()).isEqualTo("foo:foo.php");
67 assertThat(originBlock.startLine()).isEqualTo(1);
68 assertThat(originBlock.length()).isEqualTo(11);
69 assertThat(dup.duplicates()).hasSize(2);