use of org.sonar.api.batch.fs.internal.DefaultFileSystem 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);
}
use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.
the class JavaClasspathTest method no_interaction_with_FileSystem_at_initialization.
/**
* See SONARJAVA-1764
* The fileSystem should not be used in initialization phase, as it will fail the analysis if other plugins are used.
* Accessing the filesystem before the Sensor phase is not supported by SonarQube.
*/
@Test
public void no_interaction_with_FileSystem_at_initialization() {
fs = Mockito.spy(new DefaultFileSystem(new File("src/test/files/classpath/")));
javaClasspath = new JavaClasspath(settings.asConfig(), fs);
Mockito.verifyZeroInteractions(fs);
}
use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.
the class JavaClasspathTest method libraries_should_read_dir_of_class_files.
@Test
public void libraries_should_read_dir_of_class_files() {
fs = new DefaultFileSystem(new File("src/test/files/"));
TestInputFileBuilder inputFile = new TestInputFileBuilder("", "foo.java");
inputFile.setLanguage("java");
inputFile.setType(InputFile.Type.MAIN);
fs.add(inputFile.build());
settings.setProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, "classpath");
javaClasspath = createJavaClasspath();
assertThat(javaClasspath.getElements()).hasSize(4);
settings.setProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, "classpath/");
javaClasspath = createJavaClasspath();
assertThat(javaClasspath.getElements()).hasSize(4);
}
use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.
the class JavaClasspathTest method classpath_empty_if_only_test_files.
@Test
public void classpath_empty_if_only_test_files() throws Exception {
fs = new DefaultFileSystem(new File("src/test/files/classpath/"));
TestInputFileBuilder inputFile = new TestInputFileBuilder("", "plop.java");
inputFile.setType(InputFile.Type.TEST);
inputFile.setLanguage("java");
fs.add(inputFile.build());
javaClasspath = createJavaClasspath();
assertThat(javaClasspath.getElements()).isEmpty();
}
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();
}
Aggregations