Search in sources :

Example 66 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class DroppedPropertiesSensorTest method test_two_reportPaths_property.

@Test
void test_two_reportPaths_property() throws Exception {
    SensorContextTester contextTester = SensorContextTester.create(tmp.newFolder());
    MapSettings mapSettings = new MapSettings().setProperty("sonar.jacoco.reportPaths", "/path").setProperty("sonar.jacoco.reportPath", "/path");
    contextTester.setSettings(mapSettings);
    List<String> analysisWarnings = new ArrayList<>();
    DroppedPropertiesSensor sensor = new DroppedPropertiesSensor(analysisWarnings::add);
    sensor.execute(contextTester);
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
    assertThat(analysisWarnings).isEmpty();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 67 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class DroppedPropertiesSensorTest method test_empty.

@Test
void test_empty() throws Exception {
    SensorContextTester contextTester = SensorContextTester.create(tmp.newFolder());
    List<String> analysisWarnings = new ArrayList<>();
    DroppedPropertiesSensor sensor = new DroppedPropertiesSensor(analysisWarnings::add);
    sensor.execute(contextTester);
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
    assertThat(analysisWarnings).isEmpty();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 68 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSensorTest method createContext.

private static SensorContextTester createContext(InputFile.Type onType) throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/java/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    String effectiveKey = "org/sonar/plugins/java/JavaSensorTest.java";
    File file = new File(fs.baseDir(), effectiveKey);
    DefaultInputFile inputFile = new TestInputFileBuilder("", effectiveKey).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(onType).initMetadata(new String(Files.readAllBytes(file.toPath()), UTF_8)).setCharset(UTF_8).build();
    fs.add(inputFile);
    return context;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GeneratedFile(org.sonar.java.model.GeneratedFile) 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)

Example 69 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSensorTest method testIssueCreation.

private void testIssueCreation(InputFile.Type onType, int expectedIssues) throws IOException {
    MapSettings settings = new MapSettings();
    NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
    SensorContextTester context = createContext(onType).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    DefaultFileSystem fs = context.fileSystem();
    fs.setWorkDir(tmp.newFolder().toPath());
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    DefaultJavaResourceLocator javaResourceLocator = new DefaultJavaResourceLocator(new ClasspathForMain(settings.asConfig(), fs));
    JavaSensor jss = new JavaSensor(sonarComponents, fs, javaResourceLocator, settings.asConfig(), noSonarFilter, null);
    jss.execute(context);
    // argument 120 refers to the comment on line #120 in this file
    verify(noSonarFilter, times(1)).noSonarInFile(fs.inputFiles().iterator().next(), Collections.singleton(120));
    verify(sonarComponents, times(expectedIssues)).reportIssue(any(AnalyzerMessage.class));
    settings.setProperty(JavaVersion.SOURCE_VERSION, "wrongFormat");
    jss.execute(context);
    settings.setProperty(JavaVersion.SOURCE_VERSION, "1.7");
    jss.execute(context);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) MapSettings(org.sonar.api.config.internal.MapSettings) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 70 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSensorTest method analyzeTwoFilesWithIssues.

private SensorContextTester analyzeTwoFilesWithIssues(MapSettings settings) throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files").getAbsoluteFile()).setSettings(settings).setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(8, 7), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
    DefaultFileSystem fs = context.fileSystem();
    fs.setWorkDir(tmp.newFolder().toPath());
    File mainFile = new File(fs.baseDir(), "CodeWithIssues.java");
    fs.add(new TestInputFileBuilder("", mainFile.getName()).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(InputFile.Type.MAIN).initMetadata(Files.readString(mainFile.toPath())).setCharset(UTF_8).build());
    File testFile = new File(fs.baseDir(), "CodeWithIssuesTest.java");
    fs.add(new TestInputFileBuilder("", testFile.getName()).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(InputFile.Type.TEST).initMetadata(Files.readString(testFile.toPath())).setCharset(UTF_8).build());
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
    ClasspathForTest javaTestClasspath = new ClasspathForTest(context.config(), fs);
    ClasspathForMain javaClasspath = new ClasspathForMain(context.config(), fs);
    DefaultJavaResourceLocator resourceLocator = new DefaultJavaResourceLocator(new ClasspathForMain(context.config(), fs));
    CheckRegistrar[] checkRegistrars = new CheckRegistrar[] { new CustomRegistrar() };
    ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder();
    CheckList.getChecks().stream().map(check -> AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class).key()).map(key -> new NewActiveRule.Builder().setRuleKey(RuleKey.of("java", key)).build()).forEach(activeRulesBuilder::addRule);
    Stream.of("CustomMainCheck", "CustomJspCheck", "CustomTestCheck").map(key -> new NewActiveRule.Builder().setRuleKey(RuleKey.of("CustomRepository", key)).build()).forEach(activeRulesBuilder::addRule);
    CheckFactory checkFactory = new CheckFactory(activeRulesBuilder.build());
    SonarComponents components = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory, checkRegistrars, null);
    JavaSensor jss = new JavaSensor(components, fs, resourceLocator, context.config(), mock(NoSonarFilter.class), null);
    jss.execute(context);
    return context;
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Arrays(java.util.Arrays) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) PerformanceMeasure(org.sonarsource.performance.measure.PerformanceMeasure) GeneratedFile(org.sonar.java.model.GeneratedFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) MapSettings(org.sonar.api.config.internal.MapSettings) CheckList(org.sonar.java.checks.CheckList) FileLinesContext(org.sonar.api.measures.FileLinesContext) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) Loggers(org.sonar.api.utils.log.Loggers) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JspCodeVisitor(org.sonar.plugins.java.api.JspCodeVisitor) Arrays.asList(java.util.Arrays.asList) Configuration(org.sonar.api.config.Configuration) JavaCheck(org.sonar.plugins.java.api.JavaCheck) EnableRuleMigrationSupport(org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport) LogTesterJUnit5(org.sonar.api.utils.log.LogTesterJUnit5) SonarComponents(org.sonar.java.SonarComponents) Checks(org.sonar.api.batch.rule.Checks) Path(java.nio.file.Path) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) BadMethodNameCheck(org.sonar.java.checks.naming.BadMethodNameCheck) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Version(org.sonar.api.utils.Version) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.jupiter.api.Test) List(java.util.List) JavaVersion(org.sonar.plugins.java.api.JavaVersion) Stream(java.util.stream.Stream) RuleKey(org.sonar.api.rule.RuleKey) SonarEdition(org.sonar.api.SonarEdition) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) AnnotationUtils(org.sonar.api.utils.AnnotationUtils) Mockito.spy(org.mockito.Mockito.spy) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) CheckFactory(org.sonar.api.batch.rule.CheckFactory) ArgumentCaptor(org.mockito.ArgumentCaptor) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) SonarQubeSide(org.sonar.api.SonarQubeSide) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) Jasper(org.sonar.java.jsp.Jasper) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) RuleAnnotationUtils(org.sonar.api.rules.RuleAnnotationUtils) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) SonarRuntimeImpl(org.sonar.api.internal.SonarRuntimeImpl) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) CheckFactory(org.sonar.api.batch.rule.CheckFactory) GeneratedFile(org.sonar.java.model.GeneratedFile) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)224 File (java.io.File)96 Test (org.junit.jupiter.api.Test)94 InputFile (org.sonar.api.batch.fs.InputFile)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)63 MapSettings (org.sonar.api.config.internal.MapSettings)60 Test (org.junit.Test)59 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)49 Test (org.testng.annotations.Test)42 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)40 OpenEdgePluginTest (org.sonar.plugins.openedge.OpenEdgePluginTest)37 Path (java.nio.file.Path)35 OpenEdgeSettings (org.sonar.plugins.openedge.foundation.OpenEdgeSettings)30 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)25 SonarComponents (org.sonar.java.SonarComponents)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 GeneratedFile (org.sonar.java.model.GeneratedFile)18 ArrayList (java.util.ArrayList)15 ExternalIssue (org.sonar.api.batch.sensor.issue.ExternalIssue)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14