use of org.sonar.java.classpath.ClasspathForTest in project sonar-java by SonarSource.
the class InternalCheckVerifier method sonarComponents.
private static SonarComponents sonarComponents() {
SensorContext context = new InternalSensorContext();
FileSystem fileSystem = context.fileSystem();
Configuration config = context.config();
ClasspathForMain classpathForMain = new ClasspathForMain(config, fileSystem);
ClasspathForTest classpathForTest = new ClasspathForTest(config, fileSystem);
SonarComponents sonarComponents = new SonarComponents(null, fileSystem, classpathForMain, classpathForTest, null) {
@Override
public boolean reportAnalysisError(RecognitionException re, InputFile inputFile) {
throw new AssertionError(String.format("Should not fail analysis (%s)", re.getMessage()));
}
};
sonarComponents.setSensorContext(context);
return sonarComponents;
}
use of org.sonar.java.classpath.ClasspathForTest 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);
}
use of org.sonar.java.classpath.ClasspathForTest 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;
}
use of org.sonar.java.classpath.ClasspathForTest 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;
}
use of org.sonar.java.classpath.ClasspathForTest in project sonar-java by SonarSource.
the class JavaAstScannerTest method scanFilesWithVisitors.
private void scanFilesWithVisitors(List<InputFile> inputFiles, List<JavaFileScanner> visitors, int javaVersion, boolean failOnException, boolean autoscanMode) {
context.setSettings(new MapSettings().setProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, failOnException).setProperty(SonarComponents.SONAR_AUTOSCAN, autoscanMode));
DefaultFileSystem fileSystem = context.fileSystem();
ClasspathForMain classpathForMain = new ClasspathForMain(context.config(), fileSystem);
ClasspathForTest classpathForTest = new ClasspathForTest(context.config(), fileSystem);
SonarComponents sonarComponents = new SonarComponents(null, fileSystem, classpathForMain, classpathForTest, null);
sonarComponents.setSensorContext(context);
JavaAstScanner scanner = new JavaAstScanner(sonarComponents);
VisitorsBridge visitorBridge = new VisitorsBridge(visitors, new ArrayList<>(), sonarComponents);
visitorBridge.setJavaVersion(new JavaVersionImpl(javaVersion));
scanner.setVisitorBridge(visitorBridge);
scanner.scan(inputFiles);
}
Aggregations