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