3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.scanner.issue.ignore.scanner;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collections;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
33 import org.sonar.scanner.issue.ignore.pattern.IssuePattern;
34 import org.sonar.scanner.issue.ignore.pattern.PatternMatcher;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 import static org.mockito.Mockito.when;
42 public class IssueExclusionsLoaderTest {
45 public ExpectedException thrown = ExpectedException.none();
48 private IssueExclusionPatternInitializer exclusionPatternInitializer;
50 private PatternMatcher patternMatcher;
52 private IssueExclusionsLoader scanner;
55 public void before() throws Exception {
56 patternMatcher = new PatternMatcher();
57 MockitoAnnotations.initMocks(this);
58 scanner = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
62 public void testToString() throws Exception {
63 assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner");
67 public void createComputer() {
68 assertThat(scanner.createCharHandlerFor("src/main/java/Foo.java")).isNull();
70 when(exclusionPatternInitializer.getAllFilePatterns()).thenReturn(Collections.singletonList("pattern"));
71 scanner = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
72 assertThat(scanner.createCharHandlerFor("src/main/java/Foo.java")).isNotNull();
78 public void shouldHavePatternsBasedOnMulticriteriaPattern() {
79 IssuePattern pattern1 = new IssuePattern("org/foo/Bar.java", "*");
80 IssuePattern pattern2 = new IssuePattern("org/foo/Hello.java", "checkstyle:MagicNumber");
81 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(Arrays.asList(new IssuePattern[] {pattern1, pattern2}));
83 IssueExclusionsLoader loader = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
84 loader.addMulticriteriaPatterns("org/foo/Bar.java", "org.foo.Bar");
85 loader.addMulticriteriaPatterns("org/foo/Baz.java", "org.foo.Baz");
86 loader.addMulticriteriaPatterns("org/foo/Hello.java", "org.foo.Hello");
88 assertThat(patternMatcher.getPatternsForComponent("org.foo.Bar")).hasSize(1);
89 assertThat(patternMatcher.getPatternsForComponent("org.foo.Baz")).hasSize(0);
90 assertThat(patternMatcher.getPatternsForComponent("org.foo.Hello")).hasSize(1);
94 public void shouldAnalyzeProject() throws IOException {
95 IssuePattern pattern = new IssuePattern("**", "*");
96 when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(Collections.singletonList(pattern));
97 when(exclusionPatternInitializer.hasMulticriteriaPatterns()).thenReturn(true);
99 PatternMatcher patternMatcher = mock(PatternMatcher.class);
100 IssueExclusionsLoader loader = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
101 assertThat(loader.shouldExecute()).isTrue();
102 loader.addMulticriteriaPatterns("src/main/java/Foo.java", "polop:src/main/java/Foo.java");
103 loader.addMulticriteriaPatterns("src/test/java/FooTest.java", "polop:src/test/java/FooTest.java");
105 verify(patternMatcher).addPatternForComponent("polop:src/main/java/Foo.java", pattern);
106 verify(patternMatcher).addPatternForComponent("polop:src/test/java/FooTest.java", pattern);
107 verifyNoMoreInteractions(patternMatcher);
111 public void shouldExecute() {
112 when(exclusionPatternInitializer.hasMulticriteriaPatterns()).thenReturn(true);
113 assertThat(scanner.shouldExecute()).isTrue();
115 when(exclusionPatternInitializer.hasMulticriteriaPatterns()).thenReturn(false);
116 assertThat(scanner.shouldExecute()).isFalse();