use of org.sonar.api.batch.SensorContext in project sonar-java by SonarSource.
the class JavaIssueTest method test_addFlow.
@Test
public void test_addFlow() throws Exception {
TestInputFileBuilder tifb = new TestInputFileBuilder("module", "relPath");
tifb.setModuleBaseDir(new java.io.File("").toPath());
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 newIssueEmptyFlow = new DefaultIssue(storage);
DefaultIssue newIssueWithFlow = new DefaultIssue(storage);
Mockito.when(sensorContext.newIssue()).thenReturn(newIssueEmptyFlow, newIssueWithFlow);
JavaIssue javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
javaIssue.addFlow(file, new ArrayList<>());
javaIssue.save();
Mockito.verify(storage, Mockito.times(1)).store(newIssueEmptyFlow);
assertThat(newIssueEmptyFlow.flows()).isEmpty();
javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
List<List<AnalyzerMessage>> flows = new ArrayList<>();
flows.add(Lists.newArrayList(new AnalyzerMessage(null, file.file(), new AnalyzerMessage.TextSpan(2, 2, 2, 4), "flow message 1", 0)));
flows.add(Lists.newArrayList(new AnalyzerMessage(null, file.file(), new AnalyzerMessage.TextSpan(3, 1, 3, 5), "flow message 2", 0)));
javaIssue.addFlow(file, flows);
javaIssue.save();
Mockito.verify(storage, Mockito.times(1)).store(newIssueWithFlow);
assertThat(newIssueWithFlow.flows()).hasSize(2);
}
use of org.sonar.api.batch.SensorContext in project sonarqube by SonarSource.
the class ScannerExtensionDictionnaryTest method buildStatusCheckersAreExecutedAfterOtherPostJobs.
@Test
public void buildStatusCheckersAreExecutedAfterOtherPostJobs() {
BuildBreaker checker = new BuildBreaker() {
public void executeOn(Project project, SensorContext context) {
}
};
ScannerExtensionDictionnary selector = newSelector(new FakePostJob(), checker, new FakePostJob());
List extensions = Lists.newArrayList(selector.select(PostJob.class, null, true, null));
assertThat(extensions).hasSize(3);
assertThat(extensions.get(2)).isEqualTo(checker);
}
use of org.sonar.api.batch.SensorContext in project sonarlint-core by SonarSource.
the class ScannerExtensionDictionnaryTest method buildStatusCheckersAreExecutedAfterOtherPostJobs.
@Test
public void buildStatusCheckersAreExecutedAfterOtherPostJobs() {
BuildBreaker checker = new BuildBreaker() {
public void executeOn(Project project, SensorContext context) {
}
};
ScannerExtensionDictionnary selector = newSelector(new FakePostJob(), checker, new FakePostJob());
List extensions = Lists.newArrayList(selector.select(PostJob.class, null, true));
assertThat(extensions).hasSize(3);
assertThat(extensions.get(2)).isEqualTo(checker);
}
use of org.sonar.api.batch.SensorContext in project sonar-java by SonarSource.
the class DefaultJavaResourceLocatorTest method setup.
@BeforeClass
public static void setup() {
JavaClasspath javaClasspath = mock(JavaClasspath.class);
when(javaClasspath.getBinaryDirs()).thenReturn(Lists.newArrayList(new File("target/test-classes")));
when(javaClasspath.getElements()).thenReturn(Lists.newArrayList(new File("target/test-classes")));
SensorContext sensorContext = mock(SensorContext.class);
File file = new File("src/test/java/org/sonar/java/DefaultJavaResourceLocatorTest.java");
when(sensorContext.getResource(any(InputPath.class))).thenReturn(org.sonar.api.resources.File.create(file.getPath()));
DefaultFileSystem fs = new DefaultFileSystem(new File(""));
fs.add(new TestInputFileBuilder("", file.getPath()).build());
DefaultJavaResourceLocator jrl = new DefaultJavaResourceLocator(fs, javaClasspath);
jrl.setSensorContext(sensorContext);
org.sonar.java.ast.JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(jrl));
javaResourceLocator = jrl;
}
use of org.sonar.api.batch.SensorContext 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