Search in sources :

Example 6 with CheckRegistrar

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

the class SonarComponentsTest method creation_of_custom_test_checks.

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

Example 7 with CheckRegistrar

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

the class SonarComponentsTest method fail_on_empty_location.

@Test
public void fail_on_empty_location() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
    File file = new File("file.java");
    DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath()).initMetadata("class A {\n" + "  void foo() {\n" + "    System.out.println();\n" + "  }\n" + "}\n").build();
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    AnalyzerMessage.TextSpan emptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 10);
    AnalyzerMessage analyzerMessageEmptyLocation = new AnalyzerMessage(expectedCheck, file, emptyTextSpan, "message", 0);
    assertThatThrownBy(() -> sonarComponents.reportIssue(analyzerMessageEmptyLocation, ruleKey, inputFile, 0.0)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Issue location should not be empty");
    assertThat(context.allIssues()).isEmpty();
    AnalyzerMessage.TextSpan nonEmptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 15);
    AnalyzerMessage analyzerMessageValidLocation = new AnalyzerMessage(expectedCheck, file, nonEmptyTextSpan, "message", 0);
    sonarComponents.reportIssue(analyzerMessageValidLocation, ruleKey, inputFile, 0.0);
    assertThat(context.allIssues()).isNotEmpty();
}
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) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) 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

Test (org.junit.Test)7 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)7 JavaCheck (org.sonar.plugins.java.api.JavaCheck)7 File (java.io.File)5 InputFile (org.sonar.api.batch.fs.InputFile)5 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)5 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)4 RuleKey (org.sonar.api.rule.RuleKey)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3 RecognitionException (com.sonar.sslr.api.RecognitionException)2 LexerException (com.sonar.sslr.impl.LexerException)2 ArrayList (java.util.ArrayList)2 Issuable (org.sonar.api.issue.Issuable)2 Issue (org.sonar.api.issue.Issue)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 JavaRules (com.sonar.plugins.security.api.JavaRules)1