Search in sources :

Example 16 with JavaCheck

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

the class AnalyzerMessageTest method testAnalyzerMessageOnFile.

@Test
public void testAnalyzerMessageOnFile() {
    JavaCheck javaCheck = mock(JavaCheck.class);
    File file = new File("a");
    String message = "analyzer message";
    int cost = 3;
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, null, message, cost);
    assertThat(analyzerMessage.getCheck()).isEqualTo(javaCheck);
    assertThat(analyzerMessage.getFile()).isEqualTo(file);
    assertThat(analyzerMessage.getLine()).isEqualTo(null);
    assertThat(analyzerMessage.getMessage()).isEqualTo(message);
    assertThat(analyzerMessage.getCost()).isEqualTo(cost);
    assertThat(analyzerMessage.primaryLocation()).isNull();
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) File(java.io.File) Test(org.junit.Test)

Example 17 with JavaCheck

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

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

the class XmlFileSensorTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock(DefaultFileSystem fs, @Nullable File file) throws IOException {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.checkClasses()).thenReturn(new JavaCheck[] { new PomElementOrderCheck() });
    when(sonarComponents.getFileSystem()).thenReturn(fs);
    if (file != null) {
        when(sonarComponents.fileLines(any(File.class))).thenReturn(Files.readLines(file, StandardCharsets.UTF_8));
    }
    Checks<JavaCheck> checks = mock(Checks.class);
    when(checks.ruleKey(any(JavaCheck.class))).thenReturn(RuleKey.of("squid", RuleAnnotationUtils.getRuleKey(PomElementOrderCheck.class)));
    when(sonarComponents.checks()).thenReturn(Lists.<Checks<JavaCheck>>newArrayList(checks));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaCheck(org.sonar.plugins.java.api.JavaCheck) PomElementOrderCheck(org.sonar.java.checks.xml.maven.PomElementOrderCheck) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File)

Example 19 with JavaCheck

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

the class XmlAnalyzerTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock(DefaultFileSystem fs, JavaCheck... visitors) {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.checkClasses()).thenReturn(visitors);
    when(sonarComponents.getFileSystem()).thenReturn(fs);
    Checks<JavaCheck> checks = mock(Checks.class);
    when(checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
    when(sonarComponents.checks()).thenReturn(Lists.newArrayList(checks));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck)

Example 20 with JavaCheck

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

the class FilterVerifier method verify.

public static void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) {
    // set the component to the filter
    filter.setComponentKey(filename);
    IssueCollector issueCollector = new IssueCollector();
    ArrayList<JavaCheck> visitors = Lists.<JavaCheck>newArrayList(filter, issueCollector);
    // instantiate the rules filtered by the filter
    visitors.addAll(instantiateRules(filter.filteredRules()));
    for (JavaCheck visitor : extraJavaChecks) {
        visitors.add(visitor);
    }
    Collection<File> classpath = FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true);
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, Lists.newArrayList(classpath), null);
    JavaAstScanner.scanSingleFileForTests(new File(filename), visitorsBridge);
    VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
    Multimap<Integer, String> issuesByLines = HashMultimap.create();
    for (AnalyzerMessage analyzerMessage : testJavaFileScannerContext.getIssues()) {
        Integer issueLine = analyzerMessage.getLine();
        String ruleKey = AnnotationUtils.getAnnotation(analyzerMessage.getCheck().getClass(), Rule.class).key();
        FilterableIssue issue = mock(FilterableIssue.class);
        when(issue.ruleKey()).thenReturn(RuleKey.of("repo", ruleKey));
        when(issue.componentKey()).thenReturn(filename);
        when(issue.line()).thenReturn(issueLine);
        if (issueCollector.rejectedIssuesLines.contains(issueLine)) {
            assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'NoIssue' but issue of rule '" + ruleKey + "' has been accepted!").isFalse();
        } else if (issueCollector.acceptedIssuesLines.contains(issueLine)) {
            // force check on accepted issues
            assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'WithIssue' but no issue have been raised!").isTrue();
        } else {
            issuesByLines.put(issueLine, ruleKey);
        }
    }
    if (!issuesByLines.isEmpty()) {
        List<Integer> lines = Lists.newArrayList(issuesByLines.keySet());
        Collections.sort(lines);
        StringBuilder builder = new StringBuilder();
        for (Integer line : lines) {
            builder.append("\n#" + line + ": " + issuesByLines.get(line).toString());
        }
        Fail.fail("The following lines have not been marked with 'WithIssue' or 'NoIssue' and raised issues:" + builder.toString());
    }
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) FilterableIssue(org.sonar.api.scan.issue.filter.FilterableIssue) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File)

Aggregations

JavaCheck (org.sonar.plugins.java.api.JavaCheck)21 File (java.io.File)16 Test (org.junit.Test)14 InputFile (org.sonar.api.batch.fs.InputFile)8 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)7 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)7 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)6 RuleKey (org.sonar.api.rule.RuleKey)6 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)4 ImmutableList (com.google.common.collect.ImmutableList)2 RecognitionException (com.sonar.sslr.api.RecognitionException)2 LexerException (com.sonar.sslr.impl.LexerException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)2 NewSymbolTable (org.sonar.api.batch.sensor.symbol.NewSymbolTable)2 Issuable (org.sonar.api.issue.Issuable)2 Issue (org.sonar.api.issue.Issue)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2