You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewsPreOrderDepthTraversalTypeAwareCrawlerTest.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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. import org.junit.Test;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  24. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
  25. import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
  26. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  27. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
  28. public class ViewsPreOrderDepthTraversalTypeAwareCrawlerTest {
  29. private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5);
  30. private static final Component PROJECT_VIEW_6 = component(PROJECT_VIEW, 6);
  31. private static final Component SUBVIEW_4 = component(SUBVIEW, 4, PROJECT_VIEW_5, PROJECT_VIEW_6);
  32. private static final Component SUBVIEW_3 = component(SUBVIEW, 3, SUBVIEW_4);
  33. private static final Component SUBVIEW_2 = component(SUBVIEW, 2, SUBVIEW_3);
  34. private static final Component COMPONENT_TREE = component(VIEW, 1, SUBVIEW_2);
  35. private final CallRecorderTypeAwareVisitor viewVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.VIEW, PRE_ORDER);
  36. private final CallRecorderTypeAwareVisitor subViewVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.SUBVIEW, PRE_ORDER);
  37. private final CallRecorderTypeAwareVisitor projectViewVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.PROJECT_VIEW, PRE_ORDER);
  38. private final DepthTraversalTypeAwareCrawler viewCrawler = new DepthTraversalTypeAwareCrawler(viewVisitor);
  39. private final DepthTraversalTypeAwareCrawler subViewCrawler = new DepthTraversalTypeAwareCrawler(subViewVisitor);
  40. private final DepthTraversalTypeAwareCrawler projectViewCrawler = new DepthTraversalTypeAwareCrawler(projectViewVisitor);
  41. @Test
  42. public void visit_null_Component_throws_NPE() {
  43. assertThatThrownBy(() -> projectViewCrawler.visit(null))
  44. .isInstanceOf(NullPointerException.class);
  45. }
  46. @Test
  47. public void visit_projectView_with_depth_PROJECT_VIEW_calls_visit_projectView() {
  48. Component component = component(PROJECT_VIEW, 1);
  49. projectViewCrawler.visit(component);
  50. assertThat(projectViewVisitor.callsRecords).containsExactly(
  51. viewsCallRecord("visitAny", component),
  52. viewsCallRecord("visitProjectView", component));
  53. }
  54. @Test
  55. public void visit_subView_with_depth_PROJECT_VIEW_calls_visit_subView() {
  56. Component component = component(SUBVIEW, 1);
  57. projectViewCrawler.visit(component);
  58. assertThat(projectViewVisitor.callsRecords).containsExactly(
  59. viewsCallRecord("visitAny", component),
  60. viewsCallRecord("visitSubView", component));
  61. }
  62. @Test
  63. public void visit_view_with_depth_PROJECT_VIEW_calls_visit_view() {
  64. Component component = component(VIEW, 1);
  65. projectViewCrawler.visit(component);
  66. assertThat(projectViewVisitor.callsRecords).containsExactly(
  67. viewsCallRecord("visitAny", component),
  68. viewsCallRecord("visitView", component));
  69. }
  70. @Test
  71. public void visit_projectView_with_depth_SUBVIEW_does_not_call_visit_projectView_nor_visitAny() {
  72. Component component = component(PROJECT_VIEW, 1);
  73. subViewCrawler.visit(component);
  74. assertThat(subViewVisitor.callsRecords).isEmpty();
  75. }
  76. @Test
  77. public void visit_subView_with_depth_SUBVIEW_calls_visit_subView() {
  78. Component component = component(SUBVIEW, 1);
  79. subViewCrawler.visit(component);
  80. assertThat(subViewVisitor.callsRecords).containsExactly(
  81. viewsCallRecord("visitAny", component),
  82. viewsCallRecord("visitSubView", component));
  83. }
  84. @Test
  85. public void visit_view_with_depth_SUBVIEW_calls_visit_view() {
  86. Component component = component(VIEW, 1);
  87. subViewCrawler.visit(component);
  88. assertThat(subViewVisitor.callsRecords).containsExactly(
  89. viewsCallRecord("visitAny", component),
  90. viewsCallRecord("visitView", component));
  91. }
  92. @Test
  93. public void visit_projectView_with_depth_VIEW_does_not_call_visit_projectView_nor_visitAny() {
  94. Component component = component(PROJECT_VIEW, 1);
  95. viewCrawler.visit(component);
  96. assertThat(viewVisitor.callsRecords).isEmpty();
  97. }
  98. @Test
  99. public void visit_subView_with_depth_VIEW_does_not_call_visit_subView_nor_visitAny() {
  100. Component component = component(SUBVIEW, 1);
  101. viewCrawler.visit(component);
  102. assertThat(viewVisitor.callsRecords).isEmpty();
  103. }
  104. @Test
  105. public void visit_view_with_depth_VIEW_calls_visit_view_nor_visitAny() {
  106. Component component = component(VIEW, 1);
  107. viewCrawler.visit(component);
  108. assertThat(viewVisitor.callsRecords).containsExactly(
  109. viewsCallRecord("visitAny", component),
  110. viewsCallRecord("visitView", component));
  111. }
  112. @Test
  113. public void verify_visit_call_when_visit_tree_with_depth_PROJECT_VIEW() {
  114. projectViewCrawler.visit(COMPONENT_TREE);
  115. assertThat(projectViewVisitor.callsRecords).containsExactly(
  116. viewsCallRecord("visitAny", COMPONENT_TREE),
  117. viewsCallRecord("visitView", COMPONENT_TREE),
  118. viewsCallRecord("visitAny", SUBVIEW_2),
  119. viewsCallRecord("visitSubView", SUBVIEW_2),
  120. viewsCallRecord("visitAny", SUBVIEW_3),
  121. viewsCallRecord("visitSubView", SUBVIEW_3),
  122. viewsCallRecord("visitAny", SUBVIEW_4),
  123. viewsCallRecord("visitSubView", SUBVIEW_4),
  124. viewsCallRecord("visitAny", PROJECT_VIEW_5),
  125. viewsCallRecord("visitProjectView", PROJECT_VIEW_5),
  126. viewsCallRecord("visitAny", PROJECT_VIEW_6),
  127. viewsCallRecord("visitProjectView", PROJECT_VIEW_6));
  128. }
  129. @Test
  130. public void verify_visit_call_when_visit_tree_with_depth_SUBVIEW() {
  131. subViewCrawler.visit(COMPONENT_TREE);
  132. assertThat(subViewVisitor.callsRecords).containsExactly(
  133. viewsCallRecord("visitAny", COMPONENT_TREE),
  134. viewsCallRecord("visitView", COMPONENT_TREE),
  135. viewsCallRecord("visitAny", SUBVIEW_2),
  136. viewsCallRecord("visitSubView", SUBVIEW_2),
  137. viewsCallRecord("visitAny", SUBVIEW_3),
  138. viewsCallRecord("visitSubView", SUBVIEW_3),
  139. viewsCallRecord("visitAny", SUBVIEW_4),
  140. viewsCallRecord("visitSubView", SUBVIEW_4));
  141. }
  142. @Test
  143. public void verify_visit_call_when_visit_tree_with_depth_VIEW() {
  144. viewCrawler.visit(COMPONENT_TREE);
  145. assertThat(viewVisitor.callsRecords).containsExactly(
  146. viewsCallRecord("visitAny", COMPONENT_TREE),
  147. viewsCallRecord("visitView", COMPONENT_TREE));
  148. }
  149. private static Component component(final Component.Type type, final int ref, final Component... children) {
  150. return ViewsComponent.builder(type, ref).addChildren(children).build();
  151. }
  152. private static CallRecord viewsCallRecord(String methodName, Component component) {
  153. return CallRecord.viewsCallRecord(methodName, component.getDbKey());
  154. }
  155. }