Search in sources :

Example 1 with Block

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

the class DefaultCpdBlockIndexer method populateIndex.

private void populateIndex(String languageKey, List<InputFile> sourceFiles, CpdMapping mapping) {
    TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey));
    for (InputFile inputFile : sourceFiles) {
        if (!index.isIndexed(inputFile)) {
            LOG.debug("Populating index from {}", inputFile.absolutePath());
            String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
            List<Block> blocks = bridge.chunk(resourceEffectiveKey, inputFile.file());
            index.insert(inputFile, blocks);
        }
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TokenizerBridge(org.sonar.duplications.internal.pmd.TokenizerBridge) Block(org.sonar.duplications.block.Block) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 2 with Block

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

the class JavaCpdBlockIndexer method createIndex.

private void createIndex(Iterable<InputFile> sourceFiles) {
    TokenChunker tokenChunker = JavaTokenProducer.build();
    StatementChunker statementChunker = JavaStatementBuilder.build();
    BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE);
    for (InputFile inputFile : sourceFiles) {
        LOG.debug("Populating index from {}", inputFile);
        String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
        List<Statement> statements;
        try (InputStream is = new FileInputStream(inputFile.file());
            Reader reader = new InputStreamReader(is, fs.encoding())) {
            statements = statementChunker.chunk(tokenChunker.chunk(reader));
        } catch (FileNotFoundException e) {
            throw new IllegalStateException("Cannot find file " + inputFile.file(), e);
        } catch (IOException e) {
            throw new IllegalStateException("Exception handling file: " + inputFile.file(), e);
        }
        List<Block> blocks = blockChunker.chunk(resourceEffectiveKey, statements);
        index.insert(inputFile, blocks);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TokenChunker(org.sonar.duplications.token.TokenChunker) Statement(org.sonar.duplications.statement.Statement) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) StatementChunker(org.sonar.duplications.statement.StatementChunker) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Example 3 with Block

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

the class CpdExecutorTest method timeout.

@Test
public void timeout() {
    for (int i = 1; i <= 2; i++) {
        DefaultInputFile component = createComponent("src/Foo" + i + ".php", 100);
        List<Block> blocks = new ArrayList<>();
        for (int j = 1; j <= 10000; j++) {
            blocks.add(Block.builder().setResourceId(component.key()).setIndexInFile(j).setLines(j, j + 1).setUnit(j, j + 1).setBlockHash(new ByteArray("abcd1234".getBytes())).build());
        }
        index.insert((InputFile) component, blocks);
    }
    executor.execute(1);
    readDuplications(0);
    assertThat(logTester.logs(LoggerLevel.WARN)).usingElementComparator((l, r) -> l.matches(r) ? 0 : 1).containsOnly("Timeout during detection of duplications for .*Foo1.php", "Timeout during detection of duplications for .*Foo2.php");
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Duplication(org.sonar.scanner.protocol.output.ScannerReport.Duplication) Arrays(java.util.Arrays) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) CloneGroup(org.sonar.duplications.index.CloneGroup) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Block(org.sonar.duplications.block.Block) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) Duplicate(org.sonar.scanner.protocol.output.ScannerReport.Duplicate) ClonePart(org.sonar.duplications.index.ClonePart) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) ByteArray(org.sonar.duplications.block.ByteArray) PathResolver(org.sonar.api.scan.filesystem.PathResolver) CloseableIterator(org.sonar.core.util.CloseableIterator) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) List(java.util.List) ReportPublisher(org.sonar.scanner.report.ReportPublisher) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) Settings(org.sonar.api.config.Settings) MapSettings(org.sonar.api.config.MapSettings) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.mock(org.mockito.Mockito.mock) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 4 with Block

use of org.sonar.duplications.block.Block 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 5 with Block

use of org.sonar.duplications.block.Block 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)

Aggregations

Block (org.sonar.duplications.block.Block)56 Test (org.junit.Test)37 ByteArray (org.sonar.duplications.block.ByteArray)29 CloneGroup (org.sonar.duplications.index.CloneGroup)18 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 ArrayList (java.util.ArrayList)13 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)12 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)8 ClonePart (org.sonar.duplications.index.ClonePart)5 IOException (java.io.IOException)4 List (java.util.List)4 InputFile (org.sonar.api.batch.fs.InputFile)4 Statement (org.sonar.duplications.statement.Statement)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2