3 * Copyright (C) 2009-2017 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.
21 package org.sonar.server.computation.task.projectanalysis.step;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.component.ComponentDao;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.component.Component;
32 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepository;
33 import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder;
34 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
36 import static java.util.Collections.emptyList;
37 import static org.apache.commons.lang.RandomStringUtils.random;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.mock;
43 public class PersistComponentsStepTest {
46 public ExpectedException thrown = ExpectedException.none();
49 public void should_fail_if_project_is_not_stored_in_database_yet() {
50 TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
51 Component component = mock(Component.class);
52 DbClient dbClient = mock(DbClient.class);
53 ComponentDao componentDao = mock(ComponentDao.class);
54 String projectKey = random(20);
56 doReturn(component).when(treeRootHolder).getRoot();
57 doReturn(projectKey).when(component).getKey();
58 doReturn(componentDao).when(dbClient).componentDao();
59 doReturn(emptyList()).when(componentDao).selectAllComponentsFromProjectKey(any(DbSession.class), eq(projectKey));
61 thrown.expect(IllegalStateException.class);
62 thrown.expectMessage("The project '" + projectKey + "' is not stored in the database, during a project analysis");
64 new PersistComponentsStep(
67 mock(MutableDbIdsRepository.class),
69 mock(MutableDisabledComponentsHolder.class),
70 mock(AnalysisMetadataHolder.class)).execute();