2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.issue.ignore.pattern;
22 import com.google.common.collect.Sets;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.issue.Issue;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rules.Rule;
30 import java.io.IOException;
33 import static org.fest.assertions.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class PatternMatcherTest {
39 public static final Rule CHECKSTYLE_RULE = Rule.create("checkstyle", "MagicNumber", "");
40 public static final String JAVA_FILE = "org.foo.Hello";
42 private PatternMatcher patternMatcher;
46 patternMatcher = new PatternMatcher();
50 public void shouldReturnExtraPatternForResource() {
52 patternMatcher.addPatternToExcludeResource(file);
54 IssuePattern extraPattern = patternMatcher.getPatternsForComponent(file).iterator().next();
55 assertThat(extraPattern.matchResource(file)).isTrue();
56 assertThat(extraPattern.isCheckLines()).isFalse();
60 public void shouldReturnExtraPatternForLinesOfResource() {
62 Set<LineRange> lineRanges = Sets.newHashSet();
63 lineRanges.add(new LineRange(25, 28));
64 patternMatcher.addPatternToExcludeLines(file, lineRanges);
66 IssuePattern extraPattern = patternMatcher.getPatternsForComponent(file).iterator().next();
67 assertThat(extraPattern.matchResource(file)).isTrue();
68 assertThat(extraPattern.getAllLines()).isEqualTo(Sets.newHashSet(25, 26, 27, 28));
72 public void shouldHaveNoMatcherIfNoneDefined() {
73 assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, null))).isNull();
77 public void shouldMatchWithStandardPatterns() throws IOException {
78 patternMatcher.addPatternForComponent(JAVA_FILE, createPattern("org.foo.Hello;checkstyle:MagicNumber;[15-200]"));
80 assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isNotNull();
84 public void shouldNotMatchWithStandardPatterns() throws IOException {
85 patternMatcher.addPatternForComponent(JAVA_FILE, createPattern("org.foo.Hello;checkstyle:MagicNumber;[15-200]"));
87 assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isNull();
91 public void shouldMatchWithExtraPattern() throws IOException {
92 patternMatcher.addPatternForComponent(JAVA_FILE, createPattern("org.foo.Hello;*;[15-200]"));
94 assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isNotNull();
98 public void shouldNotMatchWithExtraPattern() throws IOException {
99 patternMatcher.addPatternForComponent(JAVA_FILE, createPattern("org.foo.Hello;*;[15-200]"));
101 assertThat(patternMatcher.getMatchingPattern(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isNull();
104 private Issue create(Rule rule, String component, Integer line) {
105 Issue mockIssue = mock(Issue.class);
106 RuleKey ruleKey = null;
108 ruleKey = rule.ruleKey();
110 when(mockIssue.ruleKey()).thenReturn(ruleKey);
111 when(mockIssue.componentKey()).thenReturn(component);
112 when(mockIssue.line()).thenReturn(line);
116 private IssuePattern createPattern(String line) {
117 return new PatternDecoder().decode(line).get(0);