Search in sources :

Example 1 with ByteArray

use of org.sonar.duplications.block.ByteArray in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method add_duplication_for_java_even_when_no_token.

@Test
public void add_duplication_for_java_even_when_no_token() {
    Component javaFile = builder(FILE, 1).setKey(ORIGIN_FILE_KEY).setFileAttributes(new FileAttributes(false, "java", 10)).build();
    Collection<Block> originBlocks = singletonList(// This block contains 0 token
    new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(30, 45).setUnit(0, 0).build());
    Collection<Block> duplicatedBlocks = singletonList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 55).build());
    underTest.computeCpd(javaFile, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Component(org.sonar.server.computation.task.projectanalysis.component.Component) FileAttributes(org.sonar.server.computation.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Example 2 with ByteArray

use of org.sonar.duplications.block.ByteArray in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method add_duplications_from_two_blocks.

@Test
public void add_duplications_from_two_blocks() {
    settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
    Collection<Block> originBlocks = asList(new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(30, 43).setUnit(0, 5).build(), new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("2b5747f0e4c59124")).setIndexInFile(1).setLines(32, 45).setUnit(5, 20).build());
    Collection<Block> duplicatedBlocks = asList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 53).build(), new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("2b5747f0e4c59124")).setIndexInFile(1).setLines(42, 55).build());
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 3 with ByteArray

use of org.sonar.duplications.block.ByteArray in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method add_duplications_from_a_single_block.

@Test
public void add_duplications_from_a_single_block() {
    settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
    Collection<Block> originBlocks = singletonList(// This block contains 11 tokens -> a duplication will be created
    new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(30, 45).setUnit(0, 10).build());
    Collection<Block> duplicatedBlocks = singletonList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 55).build());
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 4 with ByteArray

use of org.sonar.duplications.block.ByteArray in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method default_minimum_tokens_is_one_hundred.

@Test
public void default_minimum_tokens_is_one_hundred() {
    settings.setProperty("sonar.cpd.xoo.minimumTokens", (Integer) null);
    Collection<Block> originBlocks = singletonList(new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(30, 45).setUnit(0, 100).build());
    Collection<Block> duplicatedBlocks = singletonList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 55).build());
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 5 with ByteArray

use of org.sonar.duplications.block.ByteArray in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method do_not_compute_more_than_one_hundred_duplications_when_too_many_duplications.

@Test
public void do_not_compute_more_than_one_hundred_duplications_when_too_many_duplications() throws Exception {
    Collection<Block> originBlocks = new ArrayList<>();
    Collection<Block> duplicatedBlocks = new ArrayList<>();
    Block.Builder blockBuilder = new Block.Builder().setIndexInFile(0).setLines(30, 45).setUnit(0, 100);
    // Generate more than 100 duplication on different files
    for (int i = 0; i < 110; i++) {
        String hash = padStart("hash" + i, 16, 'a');
        originBlocks.add(blockBuilder.setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray(hash)).build());
        duplicatedBlocks.add(blockBuilder.setResourceId("resource" + i).setBlockHash(new ByteArray(hash)).build());
    }
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).hasSize(100);
    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Too many duplication groups on file " + ORIGIN_FILE_KEY + ". Keeping only the first 100 groups.");
}
Also used : ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Aggregations

ByteArray (org.sonar.duplications.block.ByteArray)23 Block (org.sonar.duplications.block.Block)20 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)6 List (java.util.List)3 CloneGroup (org.sonar.duplications.index.CloneGroup)3 CloneIndex (org.sonar.duplications.index.CloneIndex)3 HashMap (java.util.HashMap)2 ComponentDto (org.sonar.db.component.ComponentDto)2 SnapshotDto (org.sonar.db.component.SnapshotDto)2 DuplicationUnitDto (org.sonar.db.duplication.DuplicationUnitDto)2 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)2 ClonePart (org.sonar.duplications.index.ClonePart)2 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1