Search in sources :

Example 6 with DefaultCpdTokens

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

the class InMemorySensorStorage method store.

@Override
public void store(NewCpdTokens cpdTokens) {
    DefaultCpdTokens defaultCpdTokens = (DefaultCpdTokens) cpdTokens;
    String fileKey = defaultCpdTokens.inputFile().key();
    // Emulate duplicate storage check
    if (cpdTokensByComponent.containsKey(fileKey)) {
        throw new UnsupportedOperationException("Trying to save CPD tokens twice for the same file is not supported: " + defaultCpdTokens.inputFile());
    }
    cpdTokensByComponent.put(fileKey, defaultCpdTokens);
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens)

Example 7 with DefaultCpdTokens

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

the class DefaultCpdTokensTest method save_one_token.

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

Example 8 with DefaultCpdTokens

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

the class DefaultCpdTokensTest method validate_tokens_order.

@Test
public void validate_tokens_order() {
    SensorStorage sensorStorage = mock(SensorStorage.class);
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage).onFile(inputFile).addToken(inputFile.newRange(1, 6, 1, 10), "bar");
    try {
        tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo");
        fail("Expected exception");
    } catch (Exception e) {
        assertThat(e).hasMessage("Tokens of file src/Foo.java should be provided in order.\n" + "Previous token: Range[from [line=1, lineOffset=6] to [line=1, lineOffset=10]]\n" + "Last token: Range[from [line=1, lineOffset=2] to [line=1, lineOffset=5]]");
    }
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Example 9 with DefaultCpdTokens

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

the class DefaultCpdTokensTest method basic_validation.

@Test
public void basic_validation() {
    SensorStorage sensorStorage = mock(SensorStorage.class);
    DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage);
    try {
        tokens.save();
        fail("Expected exception");
    } catch (Exception e) {
        assertThat(e).hasMessage("Call onFile() first");
    }
    try {
        tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo");
        fail("Expected exception");
    } catch (Exception e) {
        assertThat(e).hasMessage("Call onFile() first");
    }
    try {
        tokens.addToken(null, "foo");
        fail("Expected exception");
    } catch (Exception e) {
        assertThat(e).hasMessage("Range should not be null");
    }
    try {
        tokens.addToken(inputFile.newRange(1, 2, 1, 5), null);
        fail("Expected exception");
    } catch (Exception e) {
        assertThat(e).hasMessage("Image should not be null");
    }
}
Also used : DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

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