use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.
the class JavaSquidTest method number_of_visitors_in_sonarLint_context_LTS.
@Test
public void number_of_visitors_in_sonarLint_context_LTS() throws Exception {
SensorContextTester context = SensorContextTester.create(temp.getRoot().getAbsoluteFile());
// set up a file to analyze
File file = temp.newFile().getAbsoluteFile();
Files.write("/***/\nclass A {\n String foo() {\n return foo();\n }\n}", file, StandardCharsets.UTF_8);
DefaultInputFile defaultFile = new TestInputFileBuilder(temp.getRoot().getAbsolutePath(), file.getName()).setLanguage("java").initMetadata(new String(java.nio.file.Files.readAllBytes(file.getAbsoluteFile().toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
context.fileSystem().add(defaultFile);
// Set sonarLint runtime
context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
// Mock visitor for metrics.
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
FileSystem fs = context.fileSystem();
JavaClasspath javaClasspath = mock(JavaClasspath.class);
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, mock(CheckFactory.class));
sonarComponents.setSensorContext(context);
JavaSquid javaSquid = new JavaSquid(new JavaVersionImpl(), sonarComponents, new Measurer(fs, context, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class), null);
javaSquid.scan(Collections.singletonList(file), Collections.emptyList());
// No symbol table : check reference to foo is empty.
assertThat(context.referencesForSymbolAt(defaultFile.key(), 3, 8)).isNull();
// No metrics on lines
verify(fileLinesContext, never()).save();
// No highlighting
assertThat(context.highlightingTypeAt(defaultFile.key(), 1, 0)).isEmpty();
// No measures
assertThat(context.measures(defaultFile.key())).isEmpty();
verify(javaClasspath, times(1)).getElements();
verify(javaTestClasspath, times(1)).getElements();
}
use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.
the class CommonsCollectionsTest method initAndScan.
private void initAndScan() {
File prjDir = new File("target/test-projects/commons-collections-3.2.1");
File srcDir = new File(prjDir, "src");
context = SensorContextTester.create(srcDir);
DefaultFileSystem fs = context.fileSystem();
Collection<File> files = FileUtils.listFiles(srcDir, new String[] { "java" }, true);
for (File file : files) {
fs.add(new TestInputFileBuilder("", file.getPath()).build());
}
Measurer measurer = new Measurer(fs, context, mock(NoSonarFilter.class));
JavaResourceLocator javaResourceLocator = new JavaResourceLocator() {
public Map<String, String> sourceFileCache = Maps.newHashMap();
@Override
public InputFile findResourceByClassName(String className) {
return null;
}
@Override
public String findSourceFileKeyByClassName(String className) {
String name = className.replace('.', '/');
return sourceFileCache.get(name);
}
@Override
public Collection<File> classFilesToAnalyze() {
return Collections.emptyList();
}
@Override
public Collection<File> classpath() {
return null;
}
@Override
public void scanFile(JavaFileScannerContext context) {
JavaFilesCache javaFilesCache = new JavaFilesCache();
javaFilesCache.scanFile(context);
for (String key : javaFilesCache.resourcesCache.keySet()) {
sourceFileCache.put(key, context.getFileKey());
}
}
};
squid = new JavaSquid(new JavaVersionImpl(), null, measurer, javaResourceLocator, null, new JavaCheck[0]);
squid.scan(files, Collections.<File>emptyList());
}
use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.
the class JavaCheckVerifier method verifyNoIssue.
/**
* Verifies that the provided file will not raise any issue when analyzed with the given check.
*
* @param filename The file to be analyzed
* @param check The check to be used for the analysis
* @param javaVersion The version to consider for the analysis (6 for java 1.6, 7 for 1.7, etc.)
*/
public static void verifyNoIssue(String filename, JavaFileScanner check, int javaVersion) {
JavaCheckVerifier javaCheckVerifier = new JavaCheckVerifier();
javaCheckVerifier.expectNoIssues();
javaCheckVerifier.providedJavaVersion = true;
javaCheckVerifier.javaVersion = new JavaVersionImpl(javaVersion);
scanFile(filename, check, javaCheckVerifier);
}
use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.
the class JavaCheckVerifier method verify.
/**
* Verifies that the provided file will raise all the expected issues when analyzed with the given check and a given
* java version used for the sources.
*
* @param filename The file to be analyzed
* @param check The check to be used for the analysis
* @param javaVersion The version to consider for the analysis (6 for java 1.6, 7 for 1.7, etc.)
*/
public static void verify(String filename, JavaFileScanner check, int javaVersion) {
JavaCheckVerifier javaCheckVerifier = new JavaCheckVerifier();
javaCheckVerifier.providedJavaVersion = true;
javaCheckVerifier.javaVersion = new JavaVersionImpl(javaVersion);
scanFile(filename, check, javaCheckVerifier);
}
use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.
the class SyntaxHighlighterVisitorTest method scan.
private void scan(File file) {
JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, null, null, null, new JavaCheck[] { syntaxHighlighterVisitor });
squid.scan(Lists.newArrayList(file), Collections.<File>emptyList());
}
Aggregations