]> source.dussan.org Git - sonarqube.git/blob
20384237dce2e2f3da071974d0b60b0b73f8102d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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 java.util.Random;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.fail;
31 import static org.assertj.guava.api.Assertions.assertThat;
32 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
33
34 public class MutableMovedFilesRepositoryImplTest {
35   private static final Component SOME_FILE = builder(Component.Type.FILE, 1).build();
36   private static final Component[] COMPONENTS_EXCEPT_FILE = {
37     builder(Component.Type.PROJECT, 1).build(),
38     builder(Component.Type.DIRECTORY, 1).build(),
39     ViewsComponent.builder(Component.Type.VIEW, 1).build(),
40     ViewsComponent.builder(Component.Type.SUBVIEW, 1).build(),
41     ViewsComponent.builder(Component.Type.PROJECT_VIEW, 1).build()
42   };
43   private static final MovedFilesRepository.OriginalFile SOME_ORIGINAL_FILE = new MovedFilesRepository.OriginalFile(100, "uuid for 100", "key for 100");
44
45   @Rule
46   public ExpectedException expectedException = ExpectedException.none();
47
48   private MutableMovedFilesRepositoryImpl underTest = new MutableMovedFilesRepositoryImpl();
49
50   @Test
51   public void setOriginalFile_throws_NPE_when_file_is_null() {
52     expectedException.expect(NullPointerException.class);
53     expectedException.expectMessage("file can't be null");
54
55     underTest.setOriginalFile(null, SOME_ORIGINAL_FILE);
56   }
57
58   @Test
59   public void setOriginalFile_throws_NPE_when_originalFile_is_null() {
60     expectedException.expect(NullPointerException.class);
61     expectedException.expectMessage("originalFile can't be null");
62
63     underTest.setOriginalFile(SOME_FILE, null);
64   }
65
66   @Test
67   public void setOriginalFile_throws_IAE_when_type_is_no_FILE() {
68     for (Component component : COMPONENTS_EXCEPT_FILE) {
69       try {
70         underTest.setOriginalFile(component, SOME_ORIGINAL_FILE);
71         fail("should have raised a NPE");
72       } catch (IllegalArgumentException e) {
73         assertThat(e)
74           .isInstanceOf(IllegalArgumentException.class)
75           .hasMessage("file must be of type FILE");
76       }
77     }
78   }
79
80   @Test
81   public void setOriginalFile_throws_ISE_if_settings_another_originalFile() {
82     underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
83
84     expectedException.expect(IllegalStateException.class);
85     expectedException.expectMessage("Original file OriginalFile{id=100, uuid='uuid for 100', key='key for 100'} " +
86       "already registered for file ReportComponent{ref=1, key='key_1', type=FILE}");
87
88     underTest.setOriginalFile(SOME_FILE, new MovedFilesRepository.OriginalFile(987, "uudi", "key"));
89   }
90
91   @Test
92   public void setOriginalFile_does_not_fail_if_same_original_file_is_added_multiple_times_for_the_same_component() {
93     underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
94
95     for (int i = 0; i < 1 + Math.abs(new Random().nextInt(10)); i++) {
96       underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
97     }
98   }
99
100   @Test
101   public void setOriginalFile_does_not_fail_when_originalFile_is_added_twice_for_different_files() {
102     underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
103     underTest.setOriginalFile(builder(Component.Type.FILE, 2).build(), SOME_ORIGINAL_FILE);
104   }
105
106   @Test
107   public void getOriginalFile_throws_NPE_when_file_is_null() {
108     expectedException.expect(NullPointerException.class);
109     expectedException.expectMessage("file can't be null");
110
111     underTest.getOriginalFile(null);
112   }
113
114   @Test
115   public void getOriginalFile_returns_absent_for_any_component_type_when_empty() {
116     assertThat(underTest.getOriginalFile(SOME_FILE)).isAbsent();
117     for (Component component : COMPONENTS_EXCEPT_FILE) {
118       assertThat(underTest.getOriginalFile(component)).isAbsent();
119     }
120   }
121
122   @Test
123   public void getOriginalFile_returns_absent_for_any_type_of_Component_but_file_when_non_empty() {
124     underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
125
126     for (Component component : COMPONENTS_EXCEPT_FILE) {
127       assertThat(underTest.getOriginalFile(component)).isAbsent();
128     }
129     assertThat(underTest.getOriginalFile(SOME_FILE)).contains(SOME_ORIGINAL_FILE);
130   }
131
132   @Test
133   public void getOriginalFile_returns_originalFile_base_on_file_key() {
134     underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
135
136     assertThat(underTest.getOriginalFile(SOME_FILE)).contains(SOME_ORIGINAL_FILE);
137     assertThat(underTest.getOriginalFile(builder(Component.Type.FILE, 1).setUuid("toto").build())).contains(SOME_ORIGINAL_FILE);
138   }
139 }