3 * Copyright (C) 2009-2022 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.ce.task.projectanalysis.filemove;
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;
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;
36 @RunWith(DataProviderRunner.class)
37 public class AddedFileRepositoryImplTest {
39 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
40 private AddedFileRepositoryImpl underTest = new AddedFileRepositoryImpl(analysisMetadataHolder);
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");
50 public void isAdded_returns_true_for_any_component_type_on_first_analysis() {
51 when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
53 Arrays.stream(Component.Type.values()).forEach(type -> {
54 Component component = newComponent(type);
56 assertThat(underTest.isAdded(component)).isTrue();
61 public void isAdded_returns_false_for_unregistered_component_type_when_not_on_first_analysis() {
62 when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
64 Arrays.stream(Component.Type.values()).forEach(type -> {
65 Component component = newComponent(type);
67 assertThat(underTest.isAdded(component)).isFalse();
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);
78 assertThat(underTest.isAdded(file1)).isTrue();
79 assertThat(underTest.isAdded(file2)).isFalse();
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");
90 @UseDataProvider("anyTypeButFile")
91 public void register_fails_with_IAE_if_component_is_not_a_file(Component.Type anyTypeButFile) {
92 Component component = newComponent(anyTypeButFile);
94 assertThatThrownBy(() -> underTest.register(component))
95 .isInstanceOf(IllegalArgumentException.class)
96 .hasMessage("component must be a file");
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);
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);
112 assertThatThrownBy(() -> underTest.register(component))
113 .isInstanceOf(IllegalStateException.class)
114 .hasMessage("No file can be registered on first analysis");
117 private static Component newComponent(Component.Type type) {
118 Component component = mock(Component.class);
119 when(component.getType()).thenReturn(type);