3 * Copyright (C) 2009-2020 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.dbmigration;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.ce.task.CeTask;
28 import org.sonar.db.Database;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
33 public class ProjectAnalysisDataChangesImplTest {
35 public ExpectedException expectedException = ExpectedException.none();
38 public void constructor_throws_IAE_if_argument_is_empty() {
39 ProjectAnalysisDataChange[] empty = new ProjectAnalysisDataChange[0];
40 int expectedArraySize = ProjectAnalysisDataChangesImpl.getDataChangeClasses().size();
42 expectedException.expect(IllegalArgumentException.class);
43 expectedException.expectMessage("Number of ProjectAnalysisDataChange instance available (0) is inconsistent with " +
44 "the number of declared ProjectAnalysisDataChange types (" + expectedArraySize + ")");
46 new ProjectAnalysisDataChangesImpl(empty);
50 public void constructor_throws_ISE_if_an_instance_of_declared_class_is_missing() {
51 ProjectAnalysisDataChange[] wrongInstance = new ProjectAnalysisDataChange[] {
52 mock(ProjectAnalysisDataChange.class)
55 expectedException.expect(IllegalStateException.class);
56 expectedException.expectMessage("Some of the ProjectAnalysisDataChange type declared have no instance in the container");
58 new ProjectAnalysisDataChangesImpl(wrongInstance);
63 public void getDataChanges_returns_instances_of_classes_in_order_defined_by_getDataChangeClasses() {
64 Database database = mock(Database.class);
65 CeTask ceTask = mock(CeTask.class);
66 ProjectAnalysisDataChangesImpl underTest = new ProjectAnalysisDataChangesImpl(new ProjectAnalysisDataChange[] {
67 new PopulateFileSourceLineCount(database, ceTask)
70 List<ProjectAnalysisDataChange> dataChanges = underTest.getDataChanges();
72 List<? extends Class<?>> dataChangeClasses = dataChanges
74 .map(ProjectAnalysisDataChange::getClass)
75 .collect(Collectors.toList());
76 assertThat(dataChangeClasses).isEqualTo(ProjectAnalysisDataChangesImpl.getDataChangeClasses());