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);
}
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));
}
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]]");
}
}
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");
}
}
Aggregations