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.
20 package org.sonar.server.computation.task.projectanalysis.component;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
28 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
29 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
30 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
31 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
32 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
33 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
35 public class TreeRootHolderImplTest {
37 private static final ReportComponent SOME_REPORT_COMPONENT_TREE = ReportComponent.builder(PROJECT, 1)
39 ReportComponent.builder(MODULE, 2)
40 .addChildren(ReportComponent.builder(DIRECTORY, 3)
42 ReportComponent.builder(FILE, 4).build()
48 private static final ViewsComponent SOME_VIEWS_COMPONENT_TREE = ViewsComponent.builder(VIEW, 1)
50 ViewsComponent.builder(VIEW, 2)
51 .addChildren(ViewsComponent.builder(PROJECT_VIEW, 3).build())
57 public ExpectedException expectedException = ExpectedException.none();
59 private TreeRootHolderImpl underTest = new TreeRootHolderImpl();
62 public void setRoot_throws_NPE_if_arg_is_null() {
63 expectedException.expect(NullPointerException.class);
64 expectedException.expectMessage("root can not be null");
66 underTest.setRoot(null);
70 public void setRoot_throws_ISE_when_called_twice() {
71 underTest.setRoot(DUMB_PROJECT);
73 expectedException.expect(IllegalStateException.class);
74 expectedException.expectMessage("root can not be set twice in holder");
76 underTest.setRoot(DUMB_PROJECT);
80 public void getRoot_throws_ISE_if_root_has_not_been_set_yet() {
81 expectNotInitialized_ISE();
87 public void getComponentByRef_throws_ISE_if_root_has_not_been_set() {
88 expectNotInitialized_ISE();
90 underTest.getComponentByRef(12);
94 public void getComponentByRef_returns_any_report_component_in_the_tree() {
95 underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
97 for (int i = 1; i <= 4; i++) {
98 assertThat(underTest.getComponentByRef(i).getReportAttributes().getRef()).isEqualTo(i);
103 public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() {
104 underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
106 expectedException.expect(IllegalArgumentException.class);
107 expectedException.expectMessage("Component with ref '6' can't be found");
109 underTest.getComponentByRef(6);
113 public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() {
114 underTest.setRoot(SOME_VIEWS_COMPONENT_TREE);
116 expectedException.expect(IllegalArgumentException.class);
117 expectedException.expectMessage("Component with ref '1' can't be found");
119 underTest.getComponentByRef(1);
123 public void verify_setRoot_getRoot() {
124 underTest.setRoot(DUMB_PROJECT);
125 assertThat(underTest.getRoot()).isSameAs(DUMB_PROJECT);
128 private void expectNotInitialized_ISE() {
129 expectedException.expect(IllegalStateException.class);
130 expectedException.expectMessage("Holder has not been initialized yet");