Search in sources :

Example 16 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method rule_metadata_unparsable.

@Test
public void rule_metadata_unparsable() {
    @Rule(key = "BrokenJSON")
    class WrongJson extends FakeVisitor {
    }
    WrongJson check = new WrongJson();
    check.withPreciseIssue(new AnalyzerMessage(check, new File("a"), 1, "message", 0));
    JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", check);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File) Test(org.junit.Test)

Example 17 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method rule_with_constant_remediation_function_should_not_provide_cost.

@Test
public void rule_with_constant_remediation_function_should_not_provide_cost() throws Exception {
    FakeVisitor fakeVisitor = new FakeVisitor();
    fakeVisitor.withPreciseIssue(new AnalyzerMessage(fakeVisitor, new File("a"), new AnalyzerMessage.TextSpan(1), "message", 1));
    Throwable throwable = catchThrowable(() -> JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", fakeVisitor));
    assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("Rule with constant remediation function shall not provide cost");
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) Test(org.junit.Test)

Example 18 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.

the class CheckVerifier method assertMultipleIssue.

private void assertMultipleIssue(Set<AnalyzerMessage> issues) throws AssertionError {
    Preconditions.checkState(!issues.isEmpty(), "At least one issue expected");
    List<Integer> unexpectedLines = Lists.newLinkedList();
    RemediationFunction remediationFunction = remediationFunction(issues.iterator().next());
    for (AnalyzerMessage issue : issues) {
        validateIssue(expected, unexpectedLines, issue, remediationFunction);
    }
    if (!expected.isEmpty() || !unexpectedLines.isEmpty()) {
        Collections.sort(unexpectedLines);
        String expectedMsg = expectedMessage();
        String unexpectedMsg = unexpectedMessage(unexpectedLines);
        Fail.fail(expectedMsg + unexpectedMsg);
    }
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 19 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.

the class CheckVerifier method validateAnalyzerMessage.

private static void validateAnalyzerMessage(Map<IssueAttribute, String> attrs, AnalyzerMessage analyzerMessage) {
    Double effortToFix = analyzerMessage.getCost();
    if (effortToFix != null) {
        assertEquals(Integer.toString(effortToFix.intValue()), attrs, IssueAttribute.EFFORT_TO_FIX);
    }
    AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
    assertEquals(normalizeColumn(textSpan.startCharacter), attrs, IssueAttribute.START_COLUMN);
    assertEquals(Integer.toString(textSpan.endLine), attrs, IssueAttribute.END_LINE);
    assertEquals(normalizeColumn(textSpan.endCharacter), attrs, IssueAttribute.END_COLUMN);
    if (attrs.containsKey(IssueAttribute.SECONDARY_LOCATIONS)) {
        List<AnalyzerMessage> secondaryLocations = analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList());
        Multiset<String> actualLines = HashMultiset.create();
        for (AnalyzerMessage secondaryLocation : secondaryLocations) {
            actualLines.add(Integer.toString(secondaryLocation.getLine()));
        }
        List<String> expected = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults().split(attrs.get(IssueAttribute.SECONDARY_LOCATIONS)));
        List<String> unexpected = new ArrayList<>();
        for (String actualLine : actualLines) {
            if (expected.contains(actualLine)) {
                expected.remove(actualLine);
            } else {
                unexpected.add(actualLine);
            }
        }
        if (!expected.isEmpty() || !unexpected.isEmpty()) {
            // Line is not covered by JaCoCo because of thrown exception but effectively covered in UT.
            Fail.fail(String.format("Secondary locations: expected: %s unexpected:%s. In %s:%d", expected, unexpected, normalizedFilePath(analyzerMessage), analyzerMessage.getLine()));
        }
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) JsonParseException(com.google.gson.JsonParseException) Iterables(com.google.common.collect.Iterables) StringUtils(org.apache.commons.lang.StringUtils) Multiset(com.google.common.collect.Multiset) URL(java.net.URL) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AnnotationUtils(org.sonar.api.utils.AnnotationUtils) Multimap(com.google.common.collect.Multimap) MapSettings(org.sonar.api.config.internal.MapSettings) ArrayList(java.util.ArrayList) Loggers(org.sonar.api.utils.log.Loggers) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) HashMultiset(com.google.common.collect.HashMultiset) RecognitionException(com.sonar.sslr.api.RecognitionException) Gson(com.google.gson.Gson) Map(java.util.Map) SonarComponents(org.sonar.java.SonarComponents) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) RspecKey(org.sonar.java.RspecKey) Set(java.util.Set) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Version(org.sonar.api.utils.Version) Fail(org.assertj.core.api.Fail) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) List(java.util.List) SonarRuntimeImpl(org.sonar.api.internal.SonarRuntimeImpl) Preconditions(com.google.common.base.Preconditions) Rule(org.sonar.check.Rule) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ArrayList(java.util.ArrayList) AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 20 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.

the class PomCheckContextImpl method reportIssue.

@Override
public void reportIssue(PomCheck check, int line, String message, List<Location> secondary) {
    File file = getFile();
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(check, file, line, message, 0);
    for (Location location : secondary) {
        AnalyzerMessage secondaryLocation = getSecondaryAnalyzerMessage(check, file, location);
        analyzerMessage.flows.add(Collections.singletonList(secondaryLocation));
    }
    getSonarComponents().reportIssue(analyzerMessage);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) XmlLocation(org.sonar.maven.model.XmlLocation)

Aggregations

AnalyzerMessage (org.sonar.java.AnalyzerMessage)23 File (java.io.File)13 Test (org.junit.Test)11 Rule (org.sonar.check.Rule)6 SonarComponents (org.sonar.java.SonarComponents)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 TextSpan (org.sonar.java.AnalyzerMessage.TextSpan)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 VisitorsBridgeForTests (org.sonar.java.model.VisitorsBridgeForTests)3 JavaCheck (org.sonar.plugins.java.api.JavaCheck)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 CheckForNull (javax.annotation.CheckForNull)2 Nullable (javax.annotation.Nullable)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Node (org.w3c.dom.Node)2 Preconditions (com.google.common.base.Preconditions)1 Splitter (com.google.common.base.Splitter)1