Search in sources :

Example 1 with JavaVersionImpl

use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.

the class JavaCheckVerifier method verifyNoIssueWithoutSemantic.

public static void verifyNoIssueWithoutSemantic(String filename, JavaFileScanner check, int javaVersion) {
    JavaCheckVerifier javaCheckVerifier = new JavaCheckVerifier() {

        @Override
        public String getExpectedIssueTrigger() {
            return "// NOSEMANTIC_ISSUE";
        }
    };
    javaCheckVerifier.expectNoIssues();
    javaCheckVerifier.providedJavaVersion = true;
    javaCheckVerifier.javaVersion = new JavaVersionImpl(javaVersion);
    scanFile(filename, check, javaCheckVerifier, Collections.<File>emptyList(), false);
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl)

Example 2 with JavaVersionImpl

use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.

the class FileLinesVisitorTest method countTrivia.

private int countTrivia(String filename) {
    TriviaVisitor triviaVisitor = new TriviaVisitor();
    JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, null, null, null, triviaVisitor);
    squid.scan(Lists.newArrayList(new File(baseDir, filename)), Collections.emptyList());
    return triviaVisitor.numberTrivia;
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) JavaSquid(org.sonar.java.JavaSquid) File(java.io.File)

Example 3 with JavaVersionImpl

use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.

the class FileLinesVisitorTest method checkLines.

private void checkLines(String filename, FileLinesContext context) {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.fileLength(Mockito.any(File.class))).thenAnswer(invocation -> {
        File arg = (File) invocation.getArguments()[0];
        return Files.readLines(arg, StandardCharsets.UTF_8).size();
    });
    when(sonarComponents.fileLinesContextFor(Mockito.any(File.class))).thenReturn(context);
    JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, null, null, null, new FileLinesVisitor(sonarComponents));
    squid.scan(Lists.newArrayList(new File(baseDir, filename)), Collections.emptyList());
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) JavaSquid(org.sonar.java.JavaSquid) File(java.io.File)

Example 4 with JavaVersionImpl

use of org.sonar.java.model.JavaVersionImpl in project sonar-java by SonarSource.

the class MeasurerTest method checkMetric.

private void checkMetric(String filename, String metric, @Nullable Number expectedValue, int numberOfMetrics) {
    String relativePath = PathUtils.sanitize(new File(baseDir, filename).getPath());
    TestInputFileBuilder inputFile = new TestInputFileBuilder(context.module().key(), relativePath);
    inputFile.setModuleBaseDir(fs.baseDirPath());
    fs.add(inputFile.build());
    Measurer measurer = new Measurer(fs, context, mock(NoSonarFilter.class));
    JavaSquid squid = new JavaSquid(new JavaVersionImpl(), null, measurer, null, null, new JavaCheck[0]);
    squid.scan(Lists.newArrayList(new File(baseDir, filename)), Collections.emptyList());
    assertThat(context.measures("projectKey:" + relativePath)).hasSize(numberOfMetrics);
    Measure<Serializable> measure = context.measure("projectKey:" + relativePath, metric);
    if (expectedValue == null) {
        assertThat(measure).isNull();
    } else {
        assertThat(measure.value()).isEqualTo(expectedValue);
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Serializable(java.io.Serializable) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) File(java.io.File)

Example 5 with JavaVersionImpl

use of org.sonar.java.model.JavaVersionImpl 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)

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