]> source.dussan.org Git - sonarqube.git/blob
f9d16b0e15771a0f0a3940545b17df6597e89976
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.component;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25
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;
34
35 public class TreeRootHolderImplTest {
36
37   private static final ReportComponent SOME_REPORT_COMPONENT_TREE = ReportComponent.builder(PROJECT, 1)
38     .addChildren(
39       ReportComponent.builder(MODULE, 2)
40         .addChildren(ReportComponent.builder(DIRECTORY, 3)
41           .addChildren(
42             ReportComponent.builder(FILE, 4).build()
43           )
44           .build())
45         .build()
46     )
47     .build();
48   private static final ViewsComponent SOME_VIEWS_COMPONENT_TREE = ViewsComponent.builder(VIEW, 1)
49     .addChildren(
50       ViewsComponent.builder(VIEW, 2)
51         .addChildren(ViewsComponent.builder(PROJECT_VIEW, 3).build())
52         .build()
53     )
54     .build();
55
56   @Rule
57   public ExpectedException expectedException = ExpectedException.none();
58
59   private TreeRootHolderImpl underTest = new TreeRootHolderImpl();
60
61   @Test
62   public void setRoot_throws_NPE_if_arg_is_null() {
63     expectedException.expect(NullPointerException.class);
64     expectedException.expectMessage("root can not be null");
65
66     underTest.setRoot(null);
67   }
68
69   @Test
70   public void setRoot_throws_ISE_when_called_twice() {
71     underTest.setRoot(DUMB_PROJECT);
72
73     expectedException.expect(IllegalStateException.class);
74     expectedException.expectMessage("root can not be set twice in holder");
75
76     underTest.setRoot(DUMB_PROJECT);
77   }
78
79   @Test
80   public void getRoot_throws_ISE_if_root_has_not_been_set_yet() {
81     expectNotInitialized_ISE();
82
83     underTest.getRoot();
84   }
85
86   @Test
87   public void getComponentByRef_throws_ISE_if_root_has_not_been_set() {
88     expectNotInitialized_ISE();
89
90     underTest.getComponentByRef(12);
91   }
92
93   @Test
94   public void getComponentByRef_returns_any_report_component_in_the_tree() {
95     underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
96
97     for (int i = 1; i <= 4; i++) {
98       assertThat(underTest.getComponentByRef(i).getReportAttributes().getRef()).isEqualTo(i);
99     }
100   }
101
102   @Test
103   public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() {
104     underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
105
106     expectedException.expect(IllegalArgumentException.class);
107     expectedException.expectMessage("Component with ref '6' can't be found");
108
109     underTest.getComponentByRef(6);
110   }
111
112   @Test
113   public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() {
114     underTest.setRoot(SOME_VIEWS_COMPONENT_TREE);
115
116     expectedException.expect(IllegalArgumentException.class);
117     expectedException.expectMessage("Component with ref '1' can't be found");
118
119     underTest.getComponentByRef(1);
120   }
121
122   @Test
123   public void verify_setRoot_getRoot() {
124     underTest.setRoot(DUMB_PROJECT);
125     assertThat(underTest.getRoot()).isSameAs(DUMB_PROJECT);
126   }
127
128   private void expectNotInitialized_ISE() {
129     expectedException.expect(IllegalStateException.class);
130     expectedException.expectMessage("Holder has not been initialized yet");
131   }
132
133 }