Search in sources :

Example 26 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class JavaDuplicationsFunctionalTest method type3.

/**
   * Does not support Type 3, however able to detect inner parts.
   */
@Test
public void type3() {
    String fragment0 = source("public int getSoLinger() throws SocketException {", "  Object o = impl.getOption( SocketOptions.SO_LINGER);", "  if (o instanceof Integer) {", "    return((Integer) o).intValue();", "  }", "  else return -1;", "}");
    String fragment1 = source("public synchronized int getSoTimeout() throws SocketException {", "  Object o = impl.getOption( SocketOptions.SO_TIMEOUT);", "  if (o instanceof Integer) {", "    return((Integer) o).intValue();", "  }", "  else return -0;", "}");
    List<CloneGroup> duplications = detect2(fragment0, fragment1);
    assertThat(duplications.size(), is(1));
    ClonePart part = duplications.get(0).getOriginPart();
    assertThat(part.getStartLine(), is(3));
    assertThat(part.getEndLine(), is(6));
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 27 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class DetectorTestCase method exampleFromPaperWithModifiedResourceIds.

/**
   * Given:
   * <pre>
   * a:   2 3 4 5
   * b:     3 4
   * c: 1 2 3 4 5 6
   * </pre>
   * Expected:
   * <pre>
   * c-a (2 3 4 5)
   * c-a-b (3 4)
   * </pre>
   */
@Test
public void exampleFromPaperWithModifiedResourceIds() {
    CloneIndex cloneIndex = createIndex(newBlocks("a", "2 3 4 5"), newBlocks("b", "3 4"));
    Block[] fileBlocks = newBlocks("c", "1 2 3 4 5 6");
    List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
    print(clones);
    assertThat(clones.size(), is(2));
    assertThat(clones, hasCloneGroup(4, newClonePart("c", 1, 4), newClonePart("a", 0, 4)));
    assertThat(clones, hasCloneGroup(2, newClonePart("c", 2, 2), newClonePart("a", 1, 2), newClonePart("b", 0, 2)));
}
Also used : Block(org.sonar.duplications.block.Block) MemoryCloneIndex(org.sonar.duplications.index.MemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Example 28 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class DetectorTestCase method same_lines_but_different_indexes.

/**
   * Given file with two lines, containing following statements:
   * <pre>
   * 0: A,B,A,B
   * 1: A,B,A
   * </pre>
   * with block size 5 each block will span both lines, and hashes will be:
   * <pre>
   * A,B,A,B,A=1
   * B,A,B,A,B=2
   * A,B,A,B,A=1
   * </pre>
   * Expected: one clone with two parts, which contain exactly the same lines
   */
@Test
public void same_lines_but_different_indexes() {
    CloneIndex cloneIndex = createIndex();
    Block.Builder block = Block.builder().setResourceId("a").setLines(0, 1);
    Block[] fileBlocks = new Block[] { block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(0).build(), block.setBlockHash(new ByteArray("2".getBytes())).setIndexInFile(1).build(), block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(2).build() };
    List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
    print(clones);
    assertThat(clones.size(), is(1));
    Iterator<CloneGroup> clonesIterator = clones.iterator();
    CloneGroup clone = clonesIterator.next();
    assertThat(clone.getCloneUnitLength(), is(1));
    assertThat(clone.getCloneParts().size(), is(2));
    assertThat(clone.getOriginPart(), is(new ClonePart("a", 0, 0, 1)));
    assertThat(clone.getCloneParts(), hasItem(new ClonePart("a", 0, 0, 1)));
    assertThat(clone.getCloneParts(), hasItem(new ClonePart("a", 2, 0, 1)));
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) MemoryCloneIndex(org.sonar.duplications.index.MemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Example 29 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class FilterTest method length_of_C1_bigger_than_length_of_C2.

/**
   * Given:
   * <pre>
   * c1: a[0-2]
   * c2: a[0-0]
   * </pre>
   * Expected:
   * <pre>
   * c1 not in c2
   * </pre>
   */
@Test
public void length_of_C1_bigger_than_length_of_C2() {
    CloneGroup c1 = spy(newCloneGroup(3, newClonePart("a", 0)));
    CloneGroup c2 = spy(newCloneGroup(1, newClonePart("a", 0)));
    assertThat(Filter.containsIn(c1, c2), is(false));
    // containsIn method should check only origin and length - no need to compare all parts
    verify(c1).getCloneUnitLength();
    verify(c2).getCloneUnitLength();
    verifyNoMoreInteractions(c1);
    verifyNoMoreInteractions(c2);
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 30 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class FilterTest method different_resources.

/**
   * Given:
   * <pre>
   * c1: a[0-0], a[2-2]
   * c2: a[0-2], b[0-2]
   * </pre>
   * Expected:
   * <pre>
   * c1 not in c2 (all parts of c1 covered by parts of c2, but different resources)
   * c2 not in c1 (not all parts of c2 covered by parts of c1 and different resources)
   * </pre>
   */
@Test
public void different_resources() {
    CloneGroup c1 = newCloneGroup(1, newClonePart("a", 0), newClonePart("a", 2));
    CloneGroup c2 = newCloneGroup(3, newClonePart("a", 0), newClonePart("b", 0));
    assertThat(Filter.containsIn(c1, c2), is(false));
    assertThat(Filter.containsIn(c2, c1), is(false));
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Aggregations

CloneGroup (org.sonar.duplications.index.CloneGroup)33 Test (org.junit.Test)28 Block (org.sonar.duplications.block.Block)16 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)10 ClonePart (org.sonar.duplications.index.ClonePart)9 ArrayList (java.util.ArrayList)3 File (java.io.File)2 ByteArray (org.sonar.duplications.block.ByteArray)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 PackedMemoryCloneIndex (org.sonar.duplications.index.PackedMemoryCloneIndex)1 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)1