Search in sources :

Example 1 with SensorContext

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);
}
Also used : SensorContext(org.sonar.api.batch.SensorContext) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArrayList(java.util.ArrayList) List(java.util.List) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 2 with SensorContext

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);
}
Also used : SensorContext(org.sonar.api.batch.SensorContext) DefaultSensorContext(org.sonar.scanner.sensor.DefaultSensorContext) CheckProject(org.sonar.api.batch.CheckProject) Project(org.sonar.api.resources.Project) BuildBreaker(org.sonar.api.batch.BuildBreaker) List(java.util.List) PostJob(org.sonar.api.batch.PostJob) Test(org.junit.Test)

Example 3 with SensorContext

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);
}
Also used : SensorContext(org.sonar.api.batch.SensorContext) CheckProject(org.sonar.api.batch.CheckProject) Project(org.sonar.api.resources.Project) BuildBreaker(org.sonar.api.batch.BuildBreaker) List(java.util.List) PostJob(org.sonar.api.batch.PostJob) Test(org.junit.Test)

Example 4 with SensorContext

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;
}
Also used : SensorContext(org.sonar.api.batch.SensorContext) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputPath(org.sonar.api.batch.fs.InputPath) VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) BeforeClass(org.junit.BeforeClass)

Example 5 with SensorContext

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);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContext(org.sonar.api.batch.SensorContext) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RuleKey(org.sonar.api.rule.RuleKey) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Aggregations

SensorContext (org.sonar.api.batch.SensorContext)5 Test (org.junit.Test)4 List (java.util.List)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 BuildBreaker (org.sonar.api.batch.BuildBreaker)2 CheckProject (org.sonar.api.batch.CheckProject)2 PostJob (org.sonar.api.batch.PostJob)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 DefaultIssue (org.sonar.api.batch.sensor.issue.internal.DefaultIssue)2 Project (org.sonar.api.resources.Project)2 RuleKey (org.sonar.api.rule.RuleKey)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 BeforeClass (org.junit.BeforeClass)1 InputFile (org.sonar.api.batch.fs.InputFile)1 InputPath (org.sonar.api.batch.fs.InputPath)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1 IssueLocation (org.sonar.api.batch.sensor.issue.IssueLocation)1 VisitorsBridge (org.sonar.java.model.VisitorsBridge)1