3 * Copyright (C) 2009-2019 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 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;
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;
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()
43 private static final MovedFilesRepository.OriginalFile SOME_ORIGINAL_FILE = new MovedFilesRepository.OriginalFile(100, "uuid for 100", "key for 100");
46 public ExpectedException expectedException = ExpectedException.none();
48 private MutableMovedFilesRepositoryImpl underTest = new MutableMovedFilesRepositoryImpl();
51 public void setOriginalFile_throws_NPE_when_file_is_null() {
52 expectedException.expect(NullPointerException.class);
53 expectedException.expectMessage("file can't be null");
55 underTest.setOriginalFile(null, SOME_ORIGINAL_FILE);
59 public void setOriginalFile_throws_NPE_when_originalFile_is_null() {
60 expectedException.expect(NullPointerException.class);
61 expectedException.expectMessage("originalFile can't be null");
63 underTest.setOriginalFile(SOME_FILE, null);
67 public void setOriginalFile_throws_IAE_when_type_is_no_FILE() {
68 for (Component component : COMPONENTS_EXCEPT_FILE) {
70 underTest.setOriginalFile(component, SOME_ORIGINAL_FILE);
71 fail("should have raised a NPE");
72 } catch (IllegalArgumentException e) {
74 .isInstanceOf(IllegalArgumentException.class)
75 .hasMessage("file must be of type FILE");
81 public void setOriginalFile_throws_ISE_if_settings_another_originalFile() {
82 underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
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}");
88 underTest.setOriginalFile(SOME_FILE, new MovedFilesRepository.OriginalFile(987, "uudi", "key"));
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);
95 for (int i = 0; i < 1 + Math.abs(new Random().nextInt(10)); i++) {
96 underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
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);
107 public void getOriginalFile_throws_NPE_when_file_is_null() {
108 expectedException.expect(NullPointerException.class);
109 expectedException.expectMessage("file can't be null");
111 underTest.getOriginalFile(null);
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();
123 public void getOriginalFile_returns_absent_for_any_type_of_Component_but_file_when_non_empty() {
124 underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
126 for (Component component : COMPONENTS_EXCEPT_FILE) {
127 assertThat(underTest.getOriginalFile(component)).isAbsent();
129 assertThat(underTest.getOriginalFile(SOME_FILE)).contains(SOME_ORIGINAL_FILE);
133 public void getOriginalFile_returns_originalFile_base_on_file_key() {
134 underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE);
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);