]> source.dussan.org Git - sonarqube.git/blob
acd308b1493d0c11f3027e7384cc52d855bf5596
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.task.projectanalysis.filemove;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.util.Arrays;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 @RunWith(DataProviderRunner.class)
37 public class AddedFileRepositoryImplTest {
38
39   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
40   private AddedFileRepositoryImpl underTest = new AddedFileRepositoryImpl(analysisMetadataHolder);
41
42   @Test
43   public void isAdded_fails_with_NPE_if_component_is_null() {
44     assertThatThrownBy(() -> underTest.isAdded(null))
45       .isInstanceOf(NullPointerException.class)
46       .hasMessage("component can't be null");
47   }
48
49   @Test
50   public void isAdded_returns_true_for_any_component_type_on_first_analysis() {
51     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
52
53     Arrays.stream(Component.Type.values()).forEach(type -> {
54       Component component = newComponent(type);
55
56       assertThat(underTest.isAdded(component)).isTrue();
57     });
58   }
59
60   @Test
61   public void isAdded_returns_false_for_unregistered_component_type_when_not_on_first_analysis() {
62     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
63
64     Arrays.stream(Component.Type.values()).forEach(type -> {
65       Component component = newComponent(type);
66
67       assertThat(underTest.isAdded(component)).isFalse();
68     });
69   }
70
71   @Test
72   public void isAdded_returns_true_for_registered_file_when_not_on_first_analysis() {
73     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
74     Component file1 = newComponent(Component.Type.FILE);
75     Component file2 = newComponent(Component.Type.FILE);
76     underTest.register(file1);
77
78     assertThat(underTest.isAdded(file1)).isTrue();
79     assertThat(underTest.isAdded(file2)).isFalse();
80   }
81
82   @Test
83   public void register_fails_with_NPE_if_component_is_null() {
84     assertThatThrownBy(() -> underTest.register(null))
85       .isInstanceOf(NullPointerException.class)
86       .hasMessage("component can't be null");
87   }
88
89   @Test
90   @UseDataProvider("anyTypeButFile")
91   public void register_fails_with_IAE_if_component_is_not_a_file(Component.Type anyTypeButFile) {
92     Component component = newComponent(anyTypeButFile);
93
94     assertThatThrownBy(() -> underTest.register(component))
95       .isInstanceOf(IllegalArgumentException.class)
96       .hasMessage("component must be a file");
97   }
98
99   @DataProvider
100   public static Object[][] anyTypeButFile() {
101     return Arrays.stream(Component.Type.values())
102       .filter(t -> t != Component.Type.FILE)
103       .map(t -> new Object[] {t})
104       .toArray(Object[][]::new);
105   }
106
107   @Test
108   public void register_fails_with_ISE_if_called_on_first_analysis() {
109     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
110     Component component = newComponent(Component.Type.FILE);
111
112     assertThatThrownBy(() -> underTest.register(component))
113       .isInstanceOf(IllegalStateException.class)
114       .hasMessage("No file can be registered on first analysis");
115   }
116
117   private static Component newComponent(Component.Type type) {
118     Component component = mock(Component.class);
119     when(component.getType()).thenReturn(type);
120     return component;
121   }
122 }