Search in sources :

Example 6 with JavaCheck

use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.

the class AnalyzerMessageTest method toString_test.

@Test
public void toString_test() throws Exception {
    JavaCheck javaCheck = mock(JavaCheck.class);
    File file = new File("file");
    String message = "analyzer message";
    int cost = 0;
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, 12, message, cost);
    assertThat(analyzerMessage.toString()).isEqualTo("'analyzer message' in file:12");
    analyzerMessage = new AnalyzerMessage(javaCheck, null, null, null, cost);
    assertThat(analyzerMessage.toString()).isEqualTo("'null' in null:null");
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) File(java.io.File) Test(org.junit.Test)

Example 7 with JavaCheck

use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContext method createAnalyzerMessage.

protected static AnalyzerMessage createAnalyzerMessage(File file, JavaCheck javaCheck, Tree startTree, @Nullable Tree endTree, String message, Iterable<List<Location>> flows, @Nullable Integer cost) {
    AnalyzerMessage.TextSpan textSpan = endTree != null ? AnalyzerMessage.textSpanBetween(startTree, endTree) : AnalyzerMessage.textSpanFor(startTree);
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, textSpan, message, cost != null ? cost : 0);
    for (List<Location> flow : flows) {
        List<AnalyzerMessage> sonarqubeFlow = flow.stream().map(l -> new AnalyzerMessage(javaCheck, file, AnalyzerMessage.textSpanFor(l.syntaxNode), l.msg, 0)).collect(Collectors.toList());
        analyzerMessage.flows.add(sonarqubeFlow);
    }
    return analyzerMessage;
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) ComplexityVisitor(org.sonar.java.ast.visitors.ComplexityVisitor) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) File(java.io.File) List(java.util.List) JavaVersion(org.sonar.plugins.java.api.JavaVersion) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JavaCheck(org.sonar.plugins.java.api.JavaCheck) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SonarComponents(org.sonar.java.SonarComponents) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 8 with JavaCheck

use of org.sonar.plugins.java.api.JavaCheck 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 9 with JavaCheck

use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.

the class SonarComponentsTest method creation_of_custom_checks.

@Test
public void creation_of_custom_checks() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(new ArrayList<>());
    SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null, null, this.checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    JavaCheck[] visitors = sonarComponents.checkClasses();
    assertThat(visitors).hasSize(1);
    assertThat(visitors[0]).isEqualTo(expectedCheck);
    Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
    assertThat(testChecks).hasSize(0);
    postTestExecutionChecks();
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) Test(org.junit.Test)

Example 10 with JavaCheck

use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.

the class SonarComponentsTest method no_issue_if_file_not_found.

@Test
public void no_issue_if_file_not_found() throws Exception {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    DefaultFileSystem fileSystem = new DefaultFileSystem(new File(""));
    File file = new File("file.java");
    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, 0, "message", null);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) 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) Test(org.junit.Test)

Aggregations

JavaCheck (org.sonar.plugins.java.api.JavaCheck)21 File (java.io.File)16 Test (org.junit.Test)14 InputFile (org.sonar.api.batch.fs.InputFile)8 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)7 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)7 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)6 RuleKey (org.sonar.api.rule.RuleKey)6 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)4 ImmutableList (com.google.common.collect.ImmutableList)2 RecognitionException (com.sonar.sslr.api.RecognitionException)2 LexerException (com.sonar.sslr.impl.LexerException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)2 NewSymbolTable (org.sonar.api.batch.sensor.symbol.NewSymbolTable)2 Issuable (org.sonar.api.issue.Issuable)2 Issue (org.sonar.api.issue.Issue)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2