Search in sources :

Example 1 with RecognitionException

use of com.sonar.sslr.api.RecognitionException in project sonar-java by SonarSource.

the class JavaAstScannerTest method should_interrupt_analysis_when_InterruptedIOException_is_thrown.

@Test
public void should_interrupt_analysis_when_InterruptedIOException_is_thrown() {
    File file = new File("src/test/files/metrics/NoSonar.java");
    thrown.expectMessage("Analysis cancelled");
    thrown.expect(new AnalysisExceptionBaseMatcher(RecognitionException.class, "instanceof AnalysisException with RecognitionException cause"));
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new CheckThrowingException(new RecognitionException(42, "interrupted", new InterruptedIOException()))));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 2 with RecognitionException

use of com.sonar.sslr.api.RecognitionException in project sonar-java by SonarSource.

the class JavaAstScanner method simpleScan.

private void simpleScan(File file) {
    visitor.setCurrentFile(file);
    try {
        String fileContent = getFileContent(file);
        Tree ast;
        if (fileContent.isEmpty()) {
            ast = parser.parse(file);
        } else {
            ast = parser.parse(fileContent);
        }
        visitor.visitFile(ast);
    } catch (RecognitionException e) {
        checkInterrupted(e);
        LOG.error("Unable to parse source file : " + file.getAbsolutePath());
        LOG.error(e.getMessage());
        parseErrorWalkAndVisit(e, file);
    } catch (Exception e) {
        checkInterrupted(e);
        throw new AnalysisException(getAnalysisExceptionMessage(file), e);
    } catch (StackOverflowError error) {
        LOG.error("A stack overflow error occured while analyzing file: " + file.getAbsolutePath());
        throw error;
    }
}
Also used : AnalysisException(org.sonar.java.AnalysisException) Tree(org.sonar.plugins.java.api.tree.Tree) RecognitionException(com.sonar.sslr.api.RecognitionException) AnalysisException(org.sonar.java.AnalysisException) InterruptedIOException(java.io.InterruptedIOException) RecognitionException(com.sonar.sslr.api.RecognitionException)

Example 3 with RecognitionException

use of com.sonar.sslr.api.RecognitionException in project sonar-java by SonarSource.

the class SonarComponentsTest method add_issue_or_parse_error.

@Test
public void add_issue_or_parse_error() throws Exception {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    File file = new File("file.java");
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "file.java");
    inputFile.setLines(45);
    int[] linesOffset = new int[45];
    linesOffset[35] = 12;
    linesOffset[42] = 1;
    inputFile.setOriginalLineOffsets(linesOffset);
    inputFile.setLastValidOffset(420);
    fileSystem.add(inputFile.build());
    when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    sonarComponents.addIssue(file, expectedCheck, -5, "message on wrong line", null);
    sonarComponents.addIssue(file, expectedCheck, 42, "message on line", 1);
    sonarComponents.addIssue(new File("."), expectedCheck, 42, "message on line", 1);
    sonarComponents.addIssue(new File("unknown_file"), expectedCheck, 42, "message on line", 1);
    sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, file, 35, "other message", 0));
    assertThat(context.allIssues()).hasSize(3);
    RecognitionException parseError = new RecognitionException(new LexerException("parse error"));
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
    assertThat(sonarComponents.reportAnalysisError(parseError, file)).isTrue();
    context.setRuntime(SonarRuntimeImpl.forSonarQube(V6_7, SonarQubeSide.SCANNER));
    assertThat(sonarComponents.reportAnalysisError(parseError, file)).isFalse();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) LexerException(com.sonar.sslr.impl.LexerException) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 4 with RecognitionException

use of com.sonar.sslr.api.RecognitionException in project sonar-java by SonarSource.

the class JavaAstScannerTest method should_interrupt_analysis_when_InterruptedException_is_thrown.

@Test
public void should_interrupt_analysis_when_InterruptedException_is_thrown() {
    File file = new File("src/test/files/metrics/NoSonar.java");
    thrown.expectMessage("Analysis cancelled");
    thrown.expect(new AnalysisExceptionBaseMatcher(RecognitionException.class, "instanceof AnalysisException with RecognitionException cause"));
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new CheckThrowingException(new RecognitionException(42, "interrupted", new InterruptedException()))));
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 5 with RecognitionException

use of com.sonar.sslr.api.RecognitionException 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)

Aggregations

RecognitionException (com.sonar.sslr.api.RecognitionException)5 File (java.io.File)4 Test (org.junit.Test)3 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 InterruptedIOException (java.io.InterruptedIOException)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 VisitorsBridge (org.sonar.java.model.VisitorsBridge)2 LexerException (com.sonar.sslr.impl.LexerException)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1 MapSettings (org.sonar.api.config.internal.MapSettings)1 RuleKey (org.sonar.api.rule.RuleKey)1 AnalysisException (org.sonar.java.AnalysisException)1 SonarComponents (org.sonar.java.SonarComponents)1 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1 Tree (org.sonar.plugins.java.api.tree.Tree)1