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.

ReportPostOrderDepthTraversalTypeAwareCrawlerTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.DIRECTORY;
  25. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  26. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  27. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
  28. public class ReportPostOrderDepthTraversalTypeAwareCrawlerTest {
  29. private static final Component FILE_5 = component(FILE, 5);
  30. private static final Component FILE_6 = component(FILE, 6);
  31. private static final Component DIRECTORY_4 = component(DIRECTORY, 4, FILE_5, FILE_6);
  32. private static final Component COMPONENT_TREE = component(PROJECT, 1, DIRECTORY_4);
  33. private final CallRecorderTypeAwareVisitor projectVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.PROJECT, POST_ORDER);
  34. private final CallRecorderTypeAwareVisitor directoryVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.DIRECTORY, POST_ORDER);
  35. private final CallRecorderTypeAwareVisitor fileVisitor = new CallRecorderTypeAwareVisitor(CrawlerDepthLimit.FILE, POST_ORDER);
  36. private final DepthTraversalTypeAwareCrawler projectCrawler = new DepthTraversalTypeAwareCrawler(projectVisitor);
  37. private final DepthTraversalTypeAwareCrawler directoryCrawler = new DepthTraversalTypeAwareCrawler(directoryVisitor);
  38. private final DepthTraversalTypeAwareCrawler fileCrawler = new DepthTraversalTypeAwareCrawler(fileVisitor);
  39. @Test
  40. public void visit_null_Component_throws_NPE() {
  41. assertThatThrownBy(() -> fileCrawler.visit(null))
  42. .isInstanceOf(NullPointerException.class);
  43. }
  44. @Test
  45. public void visit_file_with_depth_FILE_calls_visit_file() {
  46. Component component = component(FILE, 1);
  47. fileCrawler.visit(component);
  48. assertThat(fileVisitor.callsRecords).containsExactly(
  49. reportCallRecord("visitAny", component),
  50. reportCallRecord("visitFile", component));
  51. }
  52. @Test
  53. public void visit_directory_with_depth_FILE_calls_visit_directory() {
  54. Component component = component(DIRECTORY, 1);
  55. fileCrawler.visit(component);
  56. assertThat(fileVisitor.callsRecords).containsExactly(
  57. reportCallRecord("visitAny", component),
  58. reportCallRecord("visitDirectory", component));
  59. }
  60. @Test
  61. public void visit_project_with_depth_FILE_calls_visit_project() {
  62. Component component = component(PROJECT, 1);
  63. fileCrawler.visit(component);
  64. assertThat(fileVisitor.callsRecords).containsExactly(
  65. reportCallRecord("visitAny", component),
  66. reportCallRecord("visitProject", component));
  67. }
  68. @Test
  69. public void visit_file_with_depth_DIRECTORY_does_not_call_visit_file_nor_visitAny() {
  70. Component component = component(FILE, 1);
  71. directoryCrawler.visit(component);
  72. assertThat(directoryVisitor.callsRecords).isEmpty();
  73. }
  74. @Test
  75. public void visit_directory_with_depth_DIRECTORY_calls_visit_directory() {
  76. Component component = component(DIRECTORY, 1);
  77. directoryCrawler.visit(component);
  78. assertThat(directoryVisitor.callsRecords).containsExactly(
  79. reportCallRecord("visitAny", component),
  80. reportCallRecord("visitDirectory", component));
  81. }
  82. @Test
  83. public void visit_project_with_depth_DIRECTORY_calls_visit_project() {
  84. Component component = component(PROJECT, 1);
  85. directoryCrawler.visit(component);
  86. assertThat(directoryVisitor.callsRecords).containsExactly(
  87. reportCallRecord("visitAny", component),
  88. reportCallRecord("visitProject", component));
  89. }
  90. @Test
  91. public void visit_file_with_depth_PROJECT_does_not_call_visit_file_nor_visitAny() {
  92. Component component = component(FILE, 1);
  93. projectCrawler.visit(component);
  94. assertThat(projectVisitor.callsRecords).isEmpty();
  95. }
  96. @Test
  97. public void visit_directory_with_depth_PROJECT_does_not_call_visit_directory_nor_visitAny() {
  98. Component component = component(DIRECTORY, 1);
  99. projectCrawler.visit(component);
  100. assertThat(projectVisitor.callsRecords).isEmpty();
  101. }
  102. @Test
  103. public void visit_project_with_depth_PROJECT_calls_visit_project() {
  104. Component component = component(PROJECT, 1);
  105. projectCrawler.visit(component);
  106. assertThat(projectVisitor.callsRecords).containsExactly(
  107. reportCallRecord("visitAny", component),
  108. reportCallRecord("visitProject", component));
  109. }
  110. @Test
  111. public void verify_visit_call_when_visit_tree_with_depth_FILE() {
  112. fileCrawler.visit(COMPONENT_TREE);
  113. assertThat(fileVisitor.callsRecords).containsExactly(
  114. reportCallRecord("visitAny", FILE_5),
  115. reportCallRecord("visitFile", FILE_5),
  116. reportCallRecord("visitAny", FILE_6),
  117. reportCallRecord("visitFile", FILE_6),
  118. reportCallRecord("visitAny", DIRECTORY_4),
  119. reportCallRecord("visitDirectory", DIRECTORY_4),
  120. reportCallRecord("visitAny", COMPONENT_TREE),
  121. reportCallRecord("visitProject", COMPONENT_TREE));
  122. }
  123. @Test
  124. public void verify_visit_call_when_visit_tree_with_depth_DIRECTORY() {
  125. directoryCrawler.visit(COMPONENT_TREE);
  126. assertThat(directoryVisitor.callsRecords).containsExactly(
  127. reportCallRecord("visitAny", DIRECTORY_4),
  128. reportCallRecord("visitDirectory", DIRECTORY_4),
  129. reportCallRecord("visitAny", COMPONENT_TREE),
  130. reportCallRecord("visitProject", COMPONENT_TREE));
  131. }
  132. @Test
  133. public void verify_visit_call_when_visit_tree_with_depth_PROJECT() {
  134. projectCrawler.visit(COMPONENT_TREE);
  135. assertThat(projectVisitor.callsRecords).containsExactly(
  136. reportCallRecord("visitAny", COMPONENT_TREE),
  137. reportCallRecord("visitProject", COMPONENT_TREE));
  138. }
  139. private static Component component(final Component.Type type, final int ref, final Component... children) {
  140. return ReportComponent.builder(type, ref).addChildren(children).build();
  141. }
  142. private static CallRecord reportCallRecord(String methodName, Component component) {
  143. return CallRecord.reportCallRecord(methodName, component.getReportAttributes().getRef());
  144. }
  145. }