Search in sources :

Example 31 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class XmlAnalyzerTest method should_not_run_pom_check_when_no_pom_file_provided.

@Test
public void should_not_run_pom_check_when_no_pom_file_provided() {
    DefaultFileSystem fs = new DefaultFileSystem(new File(""));
    File xmlFile = new File("src/test/files/xml/parsing.xml");
    fs.add(new TestInputFileBuilder("", xmlFile.getAbsolutePath()).setLanguage("xml").build());
    SonarComponents sonarComponents = createSonarComponentsMock(fs, XML_CHECK, POM_CHECK);
    XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, XML_CHECK, POM_CHECK);
    analyzer.scan(Lists.newArrayList(xmlFile));
    verify(sonarComponents, never()).addIssue(any(File.class), eq(POM_CHECK), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, times(1)).addIssue(any(File.class), eq(XML_CHECK), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, never()).reportIssue(any(AnalyzerMessage.class));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 32 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class PomCheckContextImplTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock() {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    Mockito.when(sonarComponents.fileLines(any(File.class))).thenReturn(Arrays.asList("line 1", "line 2", "line 3 is longer"));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            reportedMessage = "onLine:" + invocation.getArguments()[3];
            return null;
        }
    }).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(LINE), anyString(), eq(null));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            reportedMessage = "onFile:" + invocation.getArguments()[3];
            return null;
        }
    }).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(-1), anyString(), eq(null));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
            reportedMessage = "analyzerMessage:" + analyzerMessage.getMessage();
            for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
                TextSpan location = secondary.primaryLocation();
                reportedMessage += ";" + secondary.getMessage() + "[" + location.startLine + ";" + location.startCharacter + "/" + location.endLine + ";" + location.endCharacter + "]";
            }
            return null;
        }
    }).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
    return sonarComponents;
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) SonarComponents(org.sonar.java.SonarComponents) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File)

Example 33 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class CheckVerifier method sonarComponents.

static SonarComponents sonarComponents(File file) {
    SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    context.setSettings(new MapSettings().setProperty("sonar.java.failOnException", true));
    SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null, null) {

        @Override
        public boolean reportAnalysisError(RecognitionException re, File file) {
            return false;
        }
    };
    sonarComponents.setSensorContext(context);
    context.fileSystem().add(new TestInputFileBuilder("", file.getPath()).setCharset(StandardCharsets.UTF_8).build());
    return sonarComponents;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException)

Example 34 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class JavaCheckVerifier method scanFile.

private static void scanFile(String filename, JavaFileScanner check, JavaCheckVerifier javaCheckVerifier, Collection<File> classpath, boolean withSemantic) {
    JavaFileScanner expectedIssueCollector = new ExpectedIssueCollector(javaCheckVerifier);
    VisitorsBridgeForTests visitorsBridge;
    File file = new File(filename);
    SonarComponents sonarComponents = CheckVerifier.sonarComponents(file);
    if (withSemantic) {
        visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), Lists.newArrayList(classpath), sonarComponents);
    } else {
        visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), sonarComponents);
    }
    JavaAstScanner.scanSingleFileForTests(file, visitorsBridge, javaCheckVerifier.javaVersion);
    VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
    if (testJavaFileScannerContext == null) {
        Fail.fail("Semantic was required but it was not possible to create it. Please checks the logs to find out the reason.");
    }
    javaCheckVerifier.checkIssues(testJavaFileScannerContext.getIssues(), javaCheckVerifier.providedJavaVersion);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) File(java.io.File) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests)

Example 35 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class SyntaxHighlighterVisitorTest method setUp.

@Before
public void setUp() throws Exception {
    context = SensorContextTester.create(temp.getRoot());
    fs = context.fileSystem();
    sonarComponents = new SonarComponents(mock(FileLinesContextFactory.class), fs, mock(JavaClasspath.class), mock(JavaTestClasspath.class), mock(CheckFactory.class));
    sonarComponents.setSensorContext(context);
    syntaxHighlighterVisitor = new SyntaxHighlighterVisitor(sonarComponents);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) Before(org.junit.Before)

Aggregations

SonarComponents (org.sonar.java.SonarComponents)36 File (java.io.File)27 Test (org.junit.Test)21 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)17 AnalyzerMessage (org.sonar.java.AnalyzerMessage)14 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)13 JavaCheck (org.sonar.plugins.java.api.JavaCheck)11 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)10 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)7 Before (org.junit.Before)6 Lists (com.google.common.collect.Lists)3 RecognitionException (com.sonar.sslr.api.RecognitionException)3 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 VisitorsBridge (org.sonar.java.model.VisitorsBridge)3 Files (com.google.common.io.Files)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2