]> source.dussan.org Git - sonarqube.git/blob
558acc0c377a172077faca9ea482a4ecdabfff4f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.task.projectanalysis.component;
21
22 import java.util.Arrays;
23 import org.junit.Test;
24 import org.mockito.InOrder;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.inOrder;
31 import static org.mockito.Mockito.spy;
32 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
33 import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
34 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
35 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
36 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
37
38 public class ViewsVisitorsCrawlerTest {
39
40
41   private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5);
42   private static final Component SUBVIEW_4 = component(SUBVIEW, 4, PROJECT_VIEW_5);
43   private static final Component SUBVIEW_3 = component(SUBVIEW, 3, SUBVIEW_4);
44   private static final Component SUBVIEW_2 = component(SUBVIEW, 2, SUBVIEW_3);
45   private static final Component COMPONENT_TREE = component(VIEW, 1, SUBVIEW_2);
46
47   private final TypeAwareVisitor spyPreOrderTypeAwareVisitor = spy(new TestTypeAwareVisitor(CrawlerDepthLimit.PROJECT_VIEW, PRE_ORDER));
48   private final TypeAwareVisitor spyPostOrderTypeAwareVisitor = spy(new TestTypeAwareVisitor(CrawlerDepthLimit.PROJECT_VIEW, POST_ORDER));
49   private final TestPathAwareVisitor spyPathAwareVisitor = spy(new TestPathAwareVisitor(CrawlerDepthLimit.PROJECT_VIEW, POST_ORDER));
50
51   @Test
52   public void execute_each_visitor_on_each_level() {
53     InOrder inOrder = inOrder(spyPostOrderTypeAwareVisitor, spyPathAwareVisitor);
54     VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(spyPostOrderTypeAwareVisitor, spyPathAwareVisitor), false);
55     underTest.visit(COMPONENT_TREE);
56
57     inOrder.verify(spyPostOrderTypeAwareVisitor).visitAny(PROJECT_VIEW_5);
58     inOrder.verify(spyPostOrderTypeAwareVisitor).visitProjectView(PROJECT_VIEW_5);
59     inOrder.verify(spyPathAwareVisitor).visitAny(eq(PROJECT_VIEW_5), any(PathAwareVisitor.Path.class));
60     inOrder.verify(spyPathAwareVisitor).visitProjectView(eq(PROJECT_VIEW_5), any(PathAwareVisitor.Path.class));
61
62     inOrder.verify(spyPostOrderTypeAwareVisitor).visitAny(SUBVIEW_4);
63     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_4);
64     inOrder.verify(spyPathAwareVisitor).visitAny(eq(SUBVIEW_4), any(PathAwareVisitor.Path.class));
65     inOrder.verify(spyPathAwareVisitor).visitSubView(eq(SUBVIEW_4), any(PathAwareVisitor.Path.class));
66
67     inOrder.verify(spyPostOrderTypeAwareVisitor).visitAny(SUBVIEW_3);
68     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_3);
69     inOrder.verify(spyPathAwareVisitor).visitAny(eq(SUBVIEW_3), any(PathAwareVisitor.Path.class));
70     inOrder.verify(spyPathAwareVisitor).visitSubView(eq(SUBVIEW_3), any(PathAwareVisitor.Path.class));
71
72     inOrder.verify(spyPostOrderTypeAwareVisitor).visitAny(SUBVIEW_2);
73     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_2);
74     inOrder.verify(spyPathAwareVisitor).visitAny(eq(SUBVIEW_2), any(PathAwareVisitor.Path.class));
75     inOrder.verify(spyPathAwareVisitor).visitSubView(eq(SUBVIEW_2), any(PathAwareVisitor.Path.class));
76
77     inOrder.verify(spyPostOrderTypeAwareVisitor).visitAny(COMPONENT_TREE);
78     inOrder.verify(spyPostOrderTypeAwareVisitor).visitView(COMPONENT_TREE);
79     inOrder.verify(spyPathAwareVisitor).visitAny(eq(COMPONENT_TREE), any(PathAwareVisitor.Path.class));
80     inOrder.verify(spyPathAwareVisitor).visitView(eq(COMPONENT_TREE), any(PathAwareVisitor.Path.class));
81   }
82
83   @Test
84   public void execute_pre_visitor_before_post_visitor() {
85     InOrder inOrder = inOrder(spyPreOrderTypeAwareVisitor, spyPostOrderTypeAwareVisitor);
86     VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(spyPreOrderTypeAwareVisitor, spyPostOrderTypeAwareVisitor), false);
87     underTest.visit(COMPONENT_TREE);
88
89     inOrder.verify(spyPreOrderTypeAwareVisitor).visitView(COMPONENT_TREE);
90     inOrder.verify(spyPreOrderTypeAwareVisitor).visitSubView(SUBVIEW_2);
91     inOrder.verify(spyPreOrderTypeAwareVisitor).visitSubView(SUBVIEW_3);
92     inOrder.verify(spyPreOrderTypeAwareVisitor).visitSubView(SUBVIEW_4);
93     inOrder.verify(spyPreOrderTypeAwareVisitor).visitProjectView(PROJECT_VIEW_5);
94
95     inOrder.verify(spyPostOrderTypeAwareVisitor).visitProjectView(PROJECT_VIEW_5);
96     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_4);
97     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_3);
98     inOrder.verify(spyPostOrderTypeAwareVisitor).visitSubView(SUBVIEW_2);
99     inOrder.verify(spyPostOrderTypeAwareVisitor).visitView(COMPONENT_TREE);
100   }
101
102   @Test
103   public void fail_with_IAE_when_visitor_is_not_path_aware_or_type_aware() {
104     assertThatThrownBy(() -> {
105       ComponentVisitor componentVisitor = new ComponentVisitor() {
106         @Override
107         public Order getOrder() {
108           return PRE_ORDER;
109         }
110
111         @Override
112         public CrawlerDepthLimit getMaxDepth() {
113           return CrawlerDepthLimit.PROJECT_VIEW;
114         }
115       };
116
117       new VisitorsCrawler(Arrays.asList(componentVisitor));
118     })
119       .isInstanceOf(IllegalArgumentException.class)
120       .hasMessage("Only TypeAwareVisitor and PathAwareVisitor can be used");
121   }
122
123   @Test
124   public void getCumulativeDurations_returns_an_empty_map_when_computation_is_disabled_in_constructor() {
125     VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(spyPreOrderTypeAwareVisitor, spyPostOrderTypeAwareVisitor), false);
126     underTest.visit(COMPONENT_TREE);
127
128     assertThat(underTest.getCumulativeDurations()).isEmpty();
129   }
130
131   @Test
132   public void getCumulativeDurations_returns_an_non_empty_map_when_computation_is_enabled_in_constructor() {
133     VisitorsCrawler underTest = new VisitorsCrawler(Arrays.asList(spyPreOrderTypeAwareVisitor, spyPostOrderTypeAwareVisitor), true);
134     underTest.visit(COMPONENT_TREE);
135
136     assertThat(underTest.getCumulativeDurations()).hasSize(2);
137   }
138
139   private static Component component(final Component.Type type, final int ref, final Component... children) {
140     return ViewsComponent.builder(type, ref).addChildren(children).build();
141   }
142
143   private static class TestTypeAwareVisitor extends TypeAwareVisitorAdapter {
144
145     public TestTypeAwareVisitor(CrawlerDepthLimit maxDepth, Order order) {
146       super(maxDepth, order);
147     }
148   }
149
150   private static class TestPathAwareVisitor extends PathAwareVisitorAdapter<Integer> {
151
152     public TestPathAwareVisitor(CrawlerDepthLimit maxDepth, Order order) {
153       super(maxDepth, order, new SimpleStackElementFactory<Integer>() {
154         @Override
155         public Integer createForAny(Component component) {
156           return Integer.valueOf(component.getDbKey());
157         }
158       });
159     }
160   }
161
162 }