private FilterableIssue create(Rule rule, String component, Integer line) { FilterableIssue mockIssue = mock(FilterableIssue.class); RuleKey ruleKey = null; if (rule != null) { ruleKey = rule.ruleKey(); } when(mockIssue.ruleKey()).thenReturn(ruleKey); when(mockIssue.componentKey()).thenReturn(component); when(mockIssue.line()).thenReturn(line); return mockIssue; }
@Test public void should_accept_issues_on_no_sonar_rules() { // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !! FilterableIssue issue = mock(FilterableIssue.class); when(issue.componentKey()).thenReturn("struts:org.apache.Action"); when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "NoSonarCheck")); Set<Integer> noSonarLines = ImmutableSet.of(31, 55); filter.addComponent("struts:org.apache.Action", noSonarLines); when(issue.line()).thenReturn(31); assertThat(filter.accept(issue, chain)).isTrue(); when(issue.line()).thenReturn(222); assertThat(filter.accept(issue, chain)).isTrue(); verify(chain, times(2)).accept(issue); }
@Test public void should_ignore_lines_commented_with_nosonar() { FilterableIssue issue = mock(FilterableIssue.class); when(issue.componentKey()).thenReturn("struts:org.apache.Action"); when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "AvoidCycles")); Set<Integer> noSonarLines = ImmutableSet.of(31, 55); filter.addComponent("struts:org.apache.Action", noSonarLines); // issue on file when(issue.line()).thenReturn(null); assertThat(filter.accept(issue, chain)).isTrue(); // issue on lines when(issue.line()).thenReturn(31); assertThat(filter.accept(issue, chain)).isFalse(); when(issue.line()).thenReturn(222); assertThat(filter.accept(issue, chain)).isTrue(); verify(chain, times(2)).accept(issue); }