Search in sources :

Example 1 with DefaultCpdTokens

use of org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens in project sonarqube by SonarSource.

the class DefaultCpdTokensTest method dont_save_for_test_files.

@Test
public void dont_save_for_test_files() {
    DefaultInputFile testInputFile = new TestInputFileBuilder("foo", "src/Foo.java").setLines(2).setOriginalLineStartOffsets(new int[] { 0, 50 }).setOriginalLineEndOffsets(new int[] { 49, 100 }).setLastValidOffset(101).setType(InputFile.Type.TEST).build();
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage).onFile(testInputFile).addToken(testInputFile.newRange(1, 2, 1, 5), "foo");
    tokens.save();
    verifyZeroInteractions(sensorStorage);
    assertThat(tokens.getTokenLines()).isEmpty();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Test(org.junit.Test)

Example 2 with DefaultCpdTokens

use of org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens in project sonarqube by SonarSource.

the class DefaultCpdTokensTest method handle_exclusions.

@Test
public void handle_exclusions() {
    inputFile.setExcludedForDuplication(true);
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage).onFile(inputFile).addToken(inputFile.newRange(1, 2, 1, 5), "foo");
    tokens.save();
    verifyZeroInteractions(sensorStorage);
    assertThat(tokens.getTokenLines()).isEmpty();
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Test(org.junit.Test)

Example 3 with DefaultCpdTokens

use of org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens in project sonarqube by SonarSource.

the class DefaultCpdTokensTest method save_many_tokens.

@Test
public void save_many_tokens() {
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage).onFile(inputFile).addToken(inputFile.newRange(1, 2, 1, 5), "foo").addToken(inputFile.newRange(1, 6, 1, 10), "bar").addToken(inputFile.newRange(1, 20, 1, 25), "biz").addToken(inputFile.newRange(2, 1, 2, 10), "next");
    tokens.save();
    verify(sensorStorage).store(tokens);
    assertThat(tokens.getTokenLines()).extracting("value", "startLine", "hashCode", "startUnit", "endUnit").containsExactly(tuple("foobarbiz", 1, "foobarbiz".hashCode(), 1, 3), tuple("next", 2, "next".hashCode(), 4, 4));
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Test(org.junit.Test)

Example 4 with DefaultCpdTokens

use of org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens in project sonarqube by SonarSource.

the class DefaultCpdTokensTest method save_no_tokens.

@Test
public void save_no_tokens() {
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage).onFile(inputFile);
    tokens.save();
    verify(sensorStorage).store(tokens);
    assertThat(tokens.inputFile()).isEqualTo(inputFile);
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Test(org.junit.Test)

Example 5 with DefaultCpdTokens

use of org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewCpdTokens cpdTokens) {
    DefaultCpdTokens defaultCpdTokens = (DefaultCpdTokens) cpdTokens;
    DefaultInputFile inputFile = (DefaultInputFile) defaultCpdTokens.inputFile();
    inputFile.setPublished(true);
    PmdBlockChunker blockChunker = new PmdBlockChunker(getCpdBlockSize(inputFile.language()));
    List<Block> blocks = blockChunker.chunk(inputFile.key(), defaultCpdTokens.getTokenLines());
    index.insert(inputFile, blocks);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Block(org.sonar.duplications.block.Block)

Aggregations

DefaultCpdTokens (org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens)9 Test (org.junit.Test)7 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 Block (org.sonar.duplications.block.Block)1 PmdBlockChunker (org.sonar.duplications.internal.pmd.PmdBlockChunker)1