]> source.dussan.org Git - sonarqube.git/blob
638e504b6ce6a962756b6162eeb81d2ff00d538a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.scanner.issue.ignore.scanner;
21
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collections;
25
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;
35
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;
41
42 public class IssueExclusionsLoaderTest {
43
44   @Rule
45   public ExpectedException thrown = ExpectedException.none();
46
47   @Mock
48   private IssueExclusionPatternInitializer exclusionPatternInitializer;
49
50   private PatternMatcher patternMatcher;
51
52   private IssueExclusionsLoader scanner;
53
54   @Before
55   public void before() throws Exception {
56     patternMatcher = new PatternMatcher();
57     MockitoAnnotations.initMocks(this);
58     scanner = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
59   }
60
61   @Test
62   public void testToString() throws Exception {
63     assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner");
64   }
65
66   @Test
67   public void createComputer() {
68     assertThat(scanner.createCharHandlerFor("src/main/java/Foo.java")).isNull();
69
70     when(exclusionPatternInitializer.getAllFilePatterns()).thenReturn(Collections.singletonList("pattern"));
71     scanner = new IssueExclusionsLoader(exclusionPatternInitializer, patternMatcher);
72     assertThat(scanner.createCharHandlerFor("src/main/java/Foo.java")).isNotNull();
73
74
75   }
76
77   @Test
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}));
82
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");
87
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);
91   }
92
93   @Test
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);
98
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");
104
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);
108   }
109
110   @Test
111   public void shouldExecute() {
112     when(exclusionPatternInitializer.hasMulticriteriaPatterns()).thenReturn(true);
113     assertThat(scanner.shouldExecute()).isTrue();
114
115     when(exclusionPatternInitializer.hasMulticriteriaPatterns()).thenReturn(false);
116     assertThat(scanner.shouldExecute()).isFalse();
117   }
118 }