use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.
the class DefaultExternalIssueTest method fail_to_store_if_primary_location_has_no_message.
@Test
public void fail_to_store_if_primary_location_has_no_message() {
SensorStorage storage = mock(SensorStorage.class);
DefaultExternalIssue issue = new DefaultExternalIssue(project, storage).at(new DefaultIssueLocation().on(inputFile).at(inputFile.selectLine(1))).forRule(RuleKey.of("repo", "rule")).remediationEffortMinutes(10L).type(RuleType.BUG).severity(Severity.BLOCKER);
assertThatThrownBy(() -> issue.save()).isInstanceOf(IllegalStateException.class).hasMessage("External issues must have a message");
}
use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.
the class DefaultExternalIssueTest method fail_to_store_if_no_severity.
@Test
public void fail_to_store_if_no_severity() {
SensorStorage storage = mock(SensorStorage.class);
DefaultExternalIssue issue = new DefaultExternalIssue(project, storage).at(new DefaultIssueLocation().on(inputFile).at(inputFile.selectLine(1)).message("Wrong way!")).forRule(RuleKey.of("repo", "rule")).remediationEffortMinutes(10L).type(RuleType.BUG);
assertThatThrownBy(() -> issue.save()).isInstanceOf(IllegalStateException.class).hasMessageContaining("Severity is mandatory");
}
use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.
the class DefaultFileLinesContextTest method setUp.
@Before
public void setUp() {
MetricFinder metricFinder = mock(MetricFinder.class);
org.sonar.api.batch.measure.Metric<String> hitsMetric = mock(org.sonar.api.batch.measure.Metric.class);
when(hitsMetric.valueType()).thenReturn(String.class);
when(hitsMetric.key()).thenReturn(HITS_METRIC_KEY);
when(metricFinder.<String>findByKey(HITS_METRIC_KEY)).thenReturn(hitsMetric);
org.sonar.api.batch.measure.Metric<String> authorMetric = mock(org.sonar.api.batch.measure.Metric.class);
when(authorMetric.valueType()).thenReturn(String.class);
when(authorMetric.key()).thenReturn(AUTHOR_METRIC_KEY);
when(metricFinder.<String>findByKey(AUTHOR_METRIC_KEY)).thenReturn(authorMetric);
org.sonar.api.batch.measure.Metric<String> branchesMetric = mock(org.sonar.api.batch.measure.Metric.class);
when(branchesMetric.valueType()).thenReturn(String.class);
when(branchesMetric.key()).thenReturn(BRANCHES_METRIC_KEY);
when(metricFinder.<String>findByKey(BRANCHES_METRIC_KEY)).thenReturn(branchesMetric);
when(metricFinder.<String>findByKey(CoreMetrics.NCLOC_DATA_KEY)).thenReturn(CoreMetrics.NCLOC_DATA);
when(metricFinder.<String>findByKey(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)).thenReturn(CoreMetrics.EXECUTABLE_LINES_DATA);
sensorStorage = mock(SensorStorage.class);
file = new TestInputFileBuilder("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz").build();
fileLineMeasures = new DefaultFileLinesContext(sensorStorage, file, metricFinder);
}
use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.
the class DefaultSensorContextTest method prepare.
@Before
public void prepare() throws Exception {
activeRules = new ActiveRulesBuilder().build();
fs = new DefaultFileSystem(temp.newFolder().toPath());
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
settings = new MapSettings();
sensorStorage = mock(SensorStorage.class);
analysisMode = mock(AnalysisMode.class);
runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER);
adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage, runtime);
}
use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonar-java by SonarSource.
the class JavaIssueTest method testIssueCreation.
@Test
public void testIssueCreation() {
TestInputFileBuilder tifb = new TestInputFileBuilder("module", "relPath");
tifb.setLines(3);
tifb.setOriginalLineOffsets(new int[] { 0, 10, 15 });
tifb.setLastValidOffset(25);
DefaultInputFile file = tifb.build();
RuleKey ruleKey = RuleKey.of("squid", "ruleKey");
SensorContext sensorContext = mock(SensorContext.class);
SensorStorage storage = mock(SensorStorage.class);
DefaultIssue newIssue = new DefaultIssue(storage);
DefaultIssue newIssueOnFile = new DefaultIssue(storage);
DefaultIssue newIssueOnLine = new DefaultIssue(storage);
Mockito.when(sensorContext.newIssue()).thenReturn(newIssue, newIssueOnFile, newIssueOnLine);
// issue with secondary locations
JavaIssue javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
javaIssue.addSecondaryLocation(file, 2, 2, 2, 4, "secondary message 1");
javaIssue.addSecondaryLocation(file, 3, 1, 3, 5, "secondary message 2");
javaIssue.save();
Mockito.verify(storage, Mockito.times(1)).store(newIssue);
assertThat(newIssue.ruleKey()).isEqualTo(ruleKey);
assertLocation(newIssue.primaryLocation(), file, "main message", 1, 2, 1, 6);
assertThat(newIssue.flows()).hasSize(2);
assertLocation(newIssue.flows().get(0).locations().get(0), file, "secondary message 1", 2, 2, 2, 4);
assertLocation(newIssue.flows().get(1).locations().get(0), file, "secondary message 2", 3, 1, 3, 5);
// issue on file
javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
javaIssue.setPrimaryLocationOnFile(file, "file message");
javaIssue.save();
Mockito.verify(storage, Mockito.times(1)).store(newIssueOnFile);
assertThat(newIssueOnFile.ruleKey()).isEqualTo(ruleKey);
IssueLocation location = newIssueOnFile.primaryLocation();
assertThat(location.inputComponent()).isEqualTo(file);
assertThat(location.textRange()).isNull();
assertThat(location.message()).isEqualTo("file message");
// issue on entire line
javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
javaIssue.setPrimaryLocation(file, "line message", 2, -1, 2, -1);
javaIssue.save();
Mockito.verify(storage, Mockito.times(1)).store(newIssueOnLine);
assertLocation(newIssueOnLine.primaryLocation(), file, "line message", 2, 0, 2, 4);
}
Aggregations