3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 java.util.Arrays;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.InOrder;
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;
38 public class ViewsVisitorsCrawlerTest {
41 public ExpectedException thrown = ExpectedException.none();
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);
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));
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);
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));
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));
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));
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));
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));
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);
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);
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);
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");
109 ComponentVisitor componentVisitor = new ComponentVisitor() {
111 public Order getOrder() {
116 public CrawlerDepthLimit getMaxDepth() {
117 return CrawlerDepthLimit.PROJECT_VIEW;
120 new VisitorsCrawler(Arrays.asList(componentVisitor));
123 private static Component component(final Component.Type type, final int ref, final Component... children) {
124 return ViewsComponent.builder(type, ref).addChildren(children).build();
127 private static class TestTypeAwareVisitor extends TypeAwareVisitorAdapter {
129 public TestTypeAwareVisitor(CrawlerDepthLimit maxDepth, Order order) {
130 super(maxDepth, order);
134 private static class TestPathAwareVisitor extends PathAwareVisitorAdapter<Integer> {
136 public TestPathAwareVisitor(CrawlerDepthLimit maxDepth, Order order) {
137 super(maxDepth, order, new SimpleStackElementFactory<Integer>() {
139 public Integer createForAny(Component component) {
140 return Integer.valueOf(component.getKey());