Search in sources :

Example 11 with JavaVersionImpl

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();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileSystem(org.sonar.api.batch.fs.FileSystem) CheckFactory(org.sonar.api.batch.rule.CheckFactory) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileLinesContext(org.sonar.api.measures.FileLinesContext) Test(org.junit.Test)

Example 12 with JavaVersionImpl

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());
}
Also used : NoSonarFilter(org.sonar.api.issue.NoSonarFilter) JavaCheck(org.sonar.plugins.java.api.JavaCheck) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 13 with JavaVersionImpl

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);
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl)

Example 14 with JavaVersionImpl

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);
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl)

Example 15 with JavaVersionImpl

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());
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) JavaSquid(org.sonar.java.JavaSquid)

Aggregations

JavaVersionImpl (org.sonar.java.model.JavaVersionImpl)16 File (java.io.File)8 Test (org.junit.Test)5 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 InputFile (org.sonar.api.batch.fs.InputFile)4 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)4 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)3 JavaSquid (org.sonar.java.JavaSquid)3 FileSystem (org.sonar.api.batch.fs.FileSystem)2 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Server (org.sonar.api.platform.Server)1 SonarComponents (org.sonar.java.SonarComponents)1