Search in sources :

Example 1 with JavaResourceLocator

use of org.sonar.plugins.java.api.JavaResourceLocator in project sonar-java by SonarSource.

the class StrutsTest method initAndScan.

private static void initAndScan() {
    File prjDir = new File("target/test-projects/struts-core-1.3.9");
    File srcDir = new File(prjDir, "src");
    context = SensorContextTester.create(prjDir);
    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 = mock(JavaResourceLocator.class);
    JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, measurer, javaResourceLocator, null, new JavaCheck[0]);
    squid.scan(files, Collections.<File>emptyList());
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 2 with JavaResourceLocator

use of org.sonar.plugins.java.api.JavaResourceLocator 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 3 with JavaResourceLocator

use of org.sonar.plugins.java.api.JavaResourceLocator in project sonar-java by SonarSource.

the class JacocoSensorJava9Test method test_jacoco_java9_coverage.

@Test
public void test_jacoco_java9_coverage() throws Exception {
    Path baseDir = Paths.get(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoJava9Test/").toURI());
    Files.copy(baseDir.resolve("Hello9.class.toCopy"), baseDir.resolve("Hello9.class"), StandardCopyOption.REPLACE_EXISTING);
    DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(10).build();
    JavaResourceLocator javaResourceLocator = mock(JavaResourceLocator.class);
    when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
    JavaClasspath javaClasspath = mock(JavaClasspath.class);
    when(javaClasspath.getBinaryDirs()).thenReturn(Collections.singletonList(baseDir.toFile()));
    SensorContextTester context = SensorContextTester.create(baseDir);
    context.settings().setProperty(REPORT_PATHS_PROPERTY, "jacoco.exec");
    JaCoCoSensor sensor = new JaCoCoSensor(null, javaResourceLocator, javaClasspath);
    sensor.execute(context);
    assertThat(context.lineHits(resource.key(), 5)).isEqualTo(1);
}
Also used : Path(java.nio.file.Path) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaClasspath(org.sonar.java.JavaClasspath) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) Test(org.junit.Test)

Example 4 with JavaResourceLocator

use of org.sonar.plugins.java.api.JavaResourceLocator in project sonar-java by SonarSource.

the class SurefireSensorTest method before.

@Before
public void before() {
    fs = new DefaultFileSystem(new File("src/test/resources"));
    DefaultInputFile javaFile = new TestInputFileBuilder("", "src/org/foo/java").setLanguage("java").build();
    fs.add(javaFile);
    perspectives = mock(ResourcePerspectives.class);
    javaResourceLocator = mock(JavaResourceLocator.class);
    when(javaResourceLocator.findResourceByClassName(anyString())).thenAnswer(invocation -> resource((String) invocation.getArguments()[0]));
    surefireSensor = new SurefireSensor(new SurefireJavaParser(perspectives, javaResourceLocator), new MapSettings().asConfig(), fs, pathResolver);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ResourcePerspectives(org.sonar.api.component.ResourcePerspectives) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Before(org.junit.Before)

Aggregations

TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)4 File (java.io.File)3 InputFile (org.sonar.api.batch.fs.InputFile)3 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)3 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 JavaVersionImpl (org.sonar.java.model.JavaVersionImpl)2 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)1 ResourcePerspectives (org.sonar.api.component.ResourcePerspectives)1 MapSettings (org.sonar.api.config.internal.MapSettings)1 JavaClasspath (org.sonar.java.JavaClasspath)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1