Search in sources :

Example 76 with DefaultFileSystem

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

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

the class JavaClasspathTest method validate_libraries_only_if_not_filtered_out.

@Test
public void validate_libraries_only_if_not_filtered_out() throws Exception {
    settings.setProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, new File("src/test/files/classpath/lib/lib.so").getAbsolutePath());
    fs = new DefaultFileSystem(new File("src/test/files/classpath/"));
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "plop.java");
    inputFile.setType(InputFile.Type.MAIN);
    inputFile.setLanguage("java");
    fs.add(inputFile.build());
    javaClasspath = createJavaClasspath();
    assertThat(javaClasspath.getElements()).isEmpty();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 78 with DefaultFileSystem

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

the class JavaClasspathTest method setUp.

@Before
public void setUp() throws Exception {
    fs = new DefaultFileSystem(new File("src/test/files/classpath/"));
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "foo.java");
    inputFile.setLanguage("java");
    inputFile.setType(InputFile.Type.MAIN);
    fs.add(inputFile.build());
    settings = new MapSettings();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MapSettings(org.sonar.api.config.internal.MapSettings) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Before(org.junit.Before)

Example 79 with DefaultFileSystem

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

the class JavaClasspathTest method createTwoFilesInFileSystem.

private void createTwoFilesInFileSystem() {
    fs = new DefaultFileSystem(new File("src/test/files/classpath/"));
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "plop.java");
    inputFile.setType(InputFile.Type.MAIN);
    inputFile.setLanguage("java");
    fs.add(inputFile.build());
    inputFile = new TestInputFileBuilder("", "bar.java");
    inputFile.setType(InputFile.Type.MAIN);
    inputFile.setLanguage("java");
    fs.add(inputFile.build());
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 80 with DefaultFileSystem

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

the class JavaClasspathTest method sonar_binaries_should_not_check_for_existence_of_files_when_no_sources.

@Test
public void sonar_binaries_should_not_check_for_existence_of_files_when_no_sources() throws Exception {
    settings.setProperty(JavaClasspathProperties.SONAR_JAVA_BINARIES, "toto/**/hello.jar");
    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();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

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