Search in sources :

Example 1 with ClonePart

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

the class IntegrateCrossProjectDuplications method addDuplication.

private void addDuplication(Component file, CloneGroup duplication) {
    ClonePart originPart = duplication.getOriginPart();
    Iterable<Duplicate> duplicates = convertClonePartsToDuplicates(file, duplication);
    if (!Iterables.isEmpty(duplicates)) {
        duplicationRepository.add(file, new Duplication(new TextBlock(originPart.getStartLine(), originPart.getEndLine()), duplicates));
    }
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart)

Example 2 with ClonePart

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

the class CloneGroupMatcher method describeTo.

public void describeTo(Description description) {
    StringBuilder builder = new StringBuilder();
    for (ClonePart part : expectedParts) {
        builder.append(part).append(" - ");
    }
    builder.append(expectedLen);
    description.appendText(builder.toString());
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart)

Example 3 with ClonePart

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

the class DuplicationsCollector method endOfGroup.

/**
   * Constructs CloneGroup and saves it.
   */
@Override
public void endOfGroup() {
    ClonePart origin = null;
    CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
    List<ClonePart> parts = new ArrayList<>(count);
    for (int[] b : blockNumbers) {
        Block firstBlock = text.getBlock(b[0]);
        Block lastBlock = text.getBlock(b[1]);
        ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine());
        // TODO Godin: maybe use FastStringComparator here ?
        if (originResourceId.equals(part.getResourceId())) {
            // part from origin
            if (origin == null) {
                origin = part;
                // To calculate length important to use the origin, because otherwise block may come from DB without required data
                builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
            } else if (part.getUnitStart() < origin.getUnitStart()) {
                origin = part;
            }
        }
        parts.add(part);
    }
    Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
    builder.setOrigin(origin).setParts(parts);
    filter(builder.build());
    reset();
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block) CloneGroup(org.sonar.duplications.index.CloneGroup)

Example 4 with ClonePart

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

the class JavaDuplicationsFunctionalTest method type2_literals.

/**
   * Supports only subset of Type 2.
   *
   * @see #type2
   * @see #literalsNormalization()
   */
@Test
public void type2_literals() {
    String fragment0 = source("if (a >= b) {", "  c = b + 1; // Comment1", "  d = '1';}", "else", "  c = d - a; // Comment2");
    String fragment1 = source("if (a >= b) {", "  c = b + 2; // Comment1", "  d = '2';}", "else", "  c = d - a; // Comment2");
    List<CloneGroup> duplications = detect2(fragment0, fragment1);
    assertThat(duplications.size(), is(1));
    ClonePart part = duplications.get(0).getOriginPart();
    assertThat(part.getStartLine(), is(1));
    assertThat(part.getEndLine(), is(5));
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 5 with ClonePart

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

the class JavaDuplicationsFunctionalTest method type1.

@Test
public void type1() {
    String fragment0 = source("if (a >= b) {", "  c = d + b; // Comment1", "  d = d + 1;}", "else", "  c = d - a; // Comment2");
    String fragment1 = source("if (a>=b) {", "  // Comment1", "  c=d+b;", "  d=d+1;", "} else // Comment2", "  c=d-a;");
    List<CloneGroup> duplications = detect2(fragment0, fragment1);
    assertThat(duplications.size(), is(1));
    ClonePart part = duplications.get(0).getOriginPart();
    assertThat(part.getStartLine(), is(1));
    assertThat(part.getEndLine(), is(5));
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Aggregations

ClonePart (org.sonar.duplications.index.ClonePart)13 CloneGroup (org.sonar.duplications.index.CloneGroup)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 Block (org.sonar.duplications.block.Block)3 File (java.io.File)2 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 ByteArray (org.sonar.duplications.block.ByteArray)1 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)1 CloneIndex (org.sonar.duplications.index.CloneIndex)1 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)1 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)1