Search in sources :

Example 56 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem 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)

Example 57 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class JavaSensorTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock(SensorContextTester contextTester) {
    DefaultFileSystem fs = contextTester.fileSystem();
    ClasspathForTest javaTestClasspath = new ClasspathForTest(contextTester.config(), fs);
    ClasspathForMain javaClasspath = new ClasspathForMain(contextTester.config(), fs);
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    SonarComponents sonarComponents = spy(new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory));
    sonarComponents.setSensorContext(contextTester);
    BadMethodNameCheck check = new BadMethodNameCheck();
    when(sonarComponents.mainChecks()).thenReturn(Collections.singletonList(check));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) BadMethodNameCheck(org.sonar.java.checks.naming.BadMethodNameCheck) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 58 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class SonarComponentsTest method add_issue_or_parse_error.

@Test
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();
    TestInputFileBuilder inputFileBuilder = new TestInputFileBuilder("", "file.java");
    inputFileBuilder.setLines(45);
    int[] lineStartOffsets = new int[45];
    lineStartOffsets[35] = 12;
    lineStartOffsets[42] = 1;
    int lastValidOffset = 420;
    inputFileBuilder.setOriginalLineStartOffsets(lineStartOffsets);
    inputFileBuilder.setOriginalLineEndOffsets(computeLineEndOffsets(lineStartOffsets, lastValidOffset));
    inputFileBuilder.setLastValidOffset(lastValidOffset);
    InputFile inputFile = inputFileBuilder.build();
    fileSystem.add(inputFile);
    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(inputFile, expectedCheck, -5, "message on wrong line", null);
    sonarComponents.addIssue(inputFile, expectedCheck, 42, "message on line 42", 1);
    sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, inputFile, 35, "other message", 0));
    List<Issue> issues = new ArrayList<>(context.allIssues());
    assertThat(issues).hasSize(3);
    assertThat(issues.get(0).primaryLocation().message()).isEqualTo("message on wrong line");
    assertThat(issues.get(1).primaryLocation().message()).isEqualTo("message on line 42");
    assertThat(issues.get(2).primaryLocation().message()).isEqualTo("other message");
    RecognitionException parseError = new RecognitionException(-1, "invalid code", new Exception("parse error"));
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V8_9));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isTrue();
    context.setRuntime(SonarRuntimeImpl.forSonarQube(V8_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isFalse();
}
Also used : Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) RecognitionException(com.sonar.sslr.api.RecognitionException) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 59 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class SonarComponentsTest method test_sonar_components.

@Test
void test_sonar_components() {
    SensorContextTester sensorContextTester = spy(SensorContextTester.create(new File("")));
    DefaultFileSystem fs = sensorContextTester.fileSystem();
    ClasspathForTest javaTestClasspath = mock(ClasspathForTest.class);
    List<File> javaTestClasspathList = Collections.emptyList();
    when(javaTestClasspath.getElements()).thenReturn(javaTestClasspathList);
    InputFile inputFile = TestUtils.emptyInputFile("foo.java");
    fs.add(inputFile);
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, null, javaTestClasspath, checkFactory);
    sonarComponents.setSensorContext(sensorContextTester);
    List<JavaCheck> visitors = sonarComponents.mainChecks();
    assertThat(visitors).isEmpty();
    Collection<JavaCheck> testChecks = sonarComponents.testChecks();
    assertThat(testChecks).isEmpty();
    assertThat(sonarComponents.getJavaClasspath()).isEmpty();
    assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
    NewHighlighting newHighlighting = sonarComponents.highlightableFor(inputFile);
    assertThat(newHighlighting).isNotNull();
    verify(sensorContextTester, times(1)).newHighlighting();
    NewSymbolTable newSymbolTable = sonarComponents.symbolizableFor(inputFile);
    assertThat(newSymbolTable).isNotNull();
    verify(sensorContextTester, times(1)).newSymbolTable();
    assertThat(sonarComponents.fileLinesContextFor(inputFile)).isEqualTo(fileLinesContext);
    assertThat(sonarComponents.context()).isSameAs(sensorContextTester);
    ClasspathForMain javaClasspath = mock(ClasspathForMain.class);
    List<File> list = mock(List.class);
    when(javaClasspath.getElements()).thenReturn(list);
    sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory);
    assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) JavaCheck(org.sonar.plugins.java.api.JavaCheck) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) InputFile(org.sonar.api.batch.fs.InputFile) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 60 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class GeneratedFileTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    expected = tmp.resolve("file.jsp");
    Files.write(expected, "content".getBytes(StandardCharsets.UTF_8));
    fs = new DefaultFileSystem(tmp);
    actual = new GeneratedFile(expected);
}
Also used : DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)126 File (java.io.File)79 InputFile (org.sonar.api.batch.fs.InputFile)70 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)55 Test (org.junit.Test)54 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)52 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)39 Test (org.junit.jupiter.api.Test)27 SonarComponents (org.sonar.java.SonarComponents)25 Before (org.junit.Before)23 MapSettings (org.sonar.api.config.internal.MapSettings)22 PathResolver (org.sonar.api.scan.filesystem.PathResolver)20 JavaCheck (org.sonar.plugins.java.api.JavaCheck)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)13 AnalyzerMessage (org.sonar.java.AnalyzerMessage)11 Path (java.nio.file.Path)10 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)10 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)8 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)8 FileLinesContext (org.sonar.api.measures.FileLinesContext)7