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.

CrawlerDepthLimitTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 com.google.common.base.Function;
  22. import com.google.common.collect.ImmutableSet;
  23. import com.tngtech.java.junit.dataprovider.DataProvider;
  24. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  25. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  26. import java.util.Arrays;
  27. import java.util.Set;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.Nullable;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.sonar.ce.task.projectanalysis.component.Component.Type;
  33. import static com.google.common.base.Predicates.equalTo;
  34. import static com.google.common.base.Predicates.in;
  35. import static com.google.common.base.Predicates.not;
  36. import static com.google.common.collect.FluentIterable.from;
  37. import static java.util.Arrays.asList;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  40. @RunWith(DataProviderRunner.class)
  41. public class CrawlerDepthLimitTest {
  42. private static final Set<Type> REPORT_TYPES = Arrays.stream(Type.values()).filter(Type::isReportType).collect(Collectors.toSet());
  43. private static final Set<Type> VIEWS_TYPES = Arrays.stream(Type.values()).filter(Type::isViewsType).collect(Collectors.toSet());
  44. @Test
  45. public void PROJECT_isSameAs_only_PROJECT_type() {
  46. assertIsSameAs(CrawlerDepthLimit.PROJECT, Type.PROJECT);
  47. }
  48. @Test
  49. public void PROJECT_isDeeper_than_no_type() {
  50. for (Type type : Type.values()) {
  51. assertThat(CrawlerDepthLimit.PROJECT.isDeeperThan(type)).as("isHigherThan(%s)", type).isFalse();
  52. }
  53. }
  54. @Test
  55. public void PROJECT_isHigher_than_all_report_types_but_PROJECT() {
  56. assertThat(CrawlerDepthLimit.PROJECT.isHigherThan(Type.PROJECT)).isFalse();
  57. for (Type reportType : from(REPORT_TYPES).filter(not(equalTo(Type.PROJECT)))) {
  58. assertThat(CrawlerDepthLimit.PROJECT.isHigherThan(reportType)).as("isHigherThan(%s)", reportType).isTrue();
  59. }
  60. }
  61. @Test
  62. public void PROJECT_isDeeper_than_no_views_types() {
  63. for (Type viewsType : VIEWS_TYPES) {
  64. assertThat(CrawlerDepthLimit.PROJECT.isDeeperThan(viewsType)).as("isDeeperThan(%s)", viewsType).isFalse();
  65. }
  66. }
  67. @Test
  68. public void PROJECT_isHigher_than_no_views_types() {
  69. assertIsHigherThanViewsType(CrawlerDepthLimit.PROJECT);
  70. }
  71. @Test
  72. public void DIRECTORY_isSameAs_only_DIRECTORY_type() {
  73. assertIsSameAs(CrawlerDepthLimit.DIRECTORY, Type.DIRECTORY);
  74. }
  75. @Test
  76. public void DIRECTORY_isDeeper_than_no_views_types() {
  77. assertIsDeeperThanViewsType(CrawlerDepthLimit.DIRECTORY);
  78. }
  79. @Test
  80. public void DIRECTORY_isDeeper_than_only_PROJECT_report_type() {
  81. assertIsDeeperThanReportType(CrawlerDepthLimit.DIRECTORY, Type.PROJECT);
  82. }
  83. @Test
  84. public void DIRECTORY_isHigher_than_only_FILE() {
  85. assertIsHigherThanReportType(CrawlerDepthLimit.DIRECTORY, Type.FILE);
  86. }
  87. @Test
  88. public void DIRECTORY_isHigher_than_no_views_type() {
  89. assertIsHigherThanViewsType(CrawlerDepthLimit.DIRECTORY);
  90. }
  91. @Test
  92. public void FILE_isSameAs_only_FILE_type() {
  93. assertIsSameAs(CrawlerDepthLimit.FILE, Type.FILE);
  94. }
  95. @Test
  96. public void FILE_isDeeper_than_no_views_types() {
  97. for (Type viewsType : VIEWS_TYPES) {
  98. assertThat(CrawlerDepthLimit.FILE.isDeeperThan(viewsType)).as("isDeeperThan(%s)", viewsType).isFalse();
  99. }
  100. }
  101. @Test
  102. public void FILE_isHigher_than_no_views_types() {
  103. assertIsHigherThanViewsType(CrawlerDepthLimit.FILE);
  104. }
  105. @Test
  106. public void FILE_isHigher_than_no_report_types() {
  107. assertIsHigherThanReportType(CrawlerDepthLimit.FILE);
  108. }
  109. @Test
  110. public void FILE_isDeeper_than_only_PROJECT_MODULE_and_DIRECTORY_report_types() {
  111. assertIsDeeperThanReportType(CrawlerDepthLimit.FILE, Type.PROJECT, Type.DIRECTORY);
  112. }
  113. @Test
  114. public void VIEW_isSameAs_only_VIEW_type() {
  115. assertIsSameAs(CrawlerDepthLimit.VIEW, Type.VIEW);
  116. }
  117. @Test
  118. public void VIEW_isDeeper_than_no_type() {
  119. for (Type type : Type.values()) {
  120. assertThat(CrawlerDepthLimit.VIEW.isDeeperThan(type)).as("isDeeperThan(%s)", type).isFalse();
  121. }
  122. }
  123. @Test
  124. public void VIEW_isHigher_than_all_views_types_but_VIEW() {
  125. assertThat(CrawlerDepthLimit.VIEW.isHigherThan(Type.VIEW)).isFalse();
  126. for (Type viewsType : from(VIEWS_TYPES).filter(not(equalTo(Type.VIEW)))) {
  127. assertThat(CrawlerDepthLimit.VIEW.isHigherThan(viewsType)).as("isHigherThan(%s)", viewsType).isTrue();
  128. }
  129. }
  130. @Test
  131. public void VIEW_isHigher_than_no_report_types() {
  132. assertIsHigherThanReportType(CrawlerDepthLimit.VIEW);
  133. }
  134. @Test
  135. public void VIEW_isDeeper_than_no_report_types() {
  136. assertIsDeeperThanReportType(CrawlerDepthLimit.VIEW);
  137. }
  138. @Test
  139. public void VIEW_isDeeper_than_no_views_types() {
  140. assertIsDeeperThanViewsType(CrawlerDepthLimit.VIEW);
  141. }
  142. @Test
  143. public void SUBVIEW_isSameAs_only_SUBVIEW_type() {
  144. assertIsSameAs(CrawlerDepthLimit.SUBVIEW, Type.SUBVIEW);
  145. }
  146. @Test
  147. public void SUBVIEW_isHigher_than_no_report_types() {
  148. assertIsHigherThanReportType(CrawlerDepthLimit.SUBVIEW);
  149. }
  150. @Test
  151. public void SUBVIEW_isDeeper_than_no_report_types() {
  152. assertIsDeeperThanReportType(CrawlerDepthLimit.SUBVIEW);
  153. }
  154. @Test
  155. public void SUBVIEW_isDeeper_than_only_VIEW_views_types() {
  156. assertIsDeeperThanReportType(CrawlerDepthLimit.SUBVIEW, Type.VIEW);
  157. }
  158. @Test
  159. public void PROJECT_VIEW_isSameAs_only_PROJECT_VIEW_type() {
  160. assertIsSameAs(CrawlerDepthLimit.PROJECT_VIEW, Type.PROJECT_VIEW);
  161. }
  162. @Test
  163. public void PROJECT_VIEW_isHigher_than_no_report_types() {
  164. assertIsHigherThanReportType(CrawlerDepthLimit.PROJECT_VIEW);
  165. }
  166. @Test
  167. public void PROJECT_VIEW_isDeeper_than_no_report_types() {
  168. assertIsDeeperThanReportType(CrawlerDepthLimit.PROJECT_VIEW);
  169. }
  170. @Test
  171. public void PROJECT_VIEW_isDeeper_than_VIEWS_and_SUBVIEWS_views_types() {
  172. assertIsDeeperThanViewsType(CrawlerDepthLimit.PROJECT_VIEW, Type.VIEW, Type.SUBVIEW);
  173. }
  174. @Test
  175. public void LEAVES_is_same_as_FILE_and_PROJECT_VIEW() {
  176. assertThat(CrawlerDepthLimit.LEAVES.isSameAs(Type.FILE)).isTrue();
  177. assertThat(CrawlerDepthLimit.LEAVES.isSameAs(Type.PROJECT_VIEW)).isTrue();
  178. for (Type type : from(asList(Type.values())).filter(not(in(ImmutableSet.of(Type.FILE, Type.PROJECT_VIEW))))) {
  179. assertThat(CrawlerDepthLimit.LEAVES.isSameAs(type)).isFalse();
  180. }
  181. }
  182. @Test
  183. public void LEAVES_isDeeper_than_PROJECT_MODULE_and_DIRECTORY_report_types() {
  184. assertIsDeeperThanReportType(CrawlerDepthLimit.LEAVES, Type.PROJECT, Type.DIRECTORY);
  185. }
  186. @Test
  187. public void LEAVES_isDeeper_than_VIEW_and_SUBVIEW_views_types() {
  188. assertIsDeeperThanViewsType(CrawlerDepthLimit.LEAVES, Type.VIEW, Type.SUBVIEW);
  189. }
  190. @Test
  191. public void LEAVES_isHigher_than_no_report_types() {
  192. assertIsHigherThanReportType(CrawlerDepthLimit.LEAVES);
  193. }
  194. @Test
  195. public void LEAVES_isHigher_than_no_views_types() {
  196. assertIsHigherThanViewsType(CrawlerDepthLimit.LEAVES);
  197. }
  198. private void assertIsSameAs(CrawlerDepthLimit crawlerDepthLimit, Type expectedType) {
  199. assertThat(crawlerDepthLimit.isSameAs(expectedType)).isTrue();
  200. for (Type type : from(asList(Type.values())).filter(not(equalTo(expectedType)))) {
  201. assertThat(crawlerDepthLimit.isSameAs(type)).isFalse();
  202. }
  203. }
  204. private void assertIsHigherThanReportType(CrawlerDepthLimit depthLimit, Type... types) {
  205. for (Type type : types) {
  206. assertThat(depthLimit.isHigherThan(type)).as("isHigherThan(%s)", type).isTrue();
  207. }
  208. for (Type reportType : from(REPORT_TYPES).filter(not(in(asList(types))))) {
  209. assertThat(depthLimit.isHigherThan(reportType)).as("isHigherThan(%s)", reportType).isFalse();
  210. }
  211. }
  212. private void assertIsHigherThanViewsType(CrawlerDepthLimit depthLimit, Type... types) {
  213. for (Type type : types) {
  214. assertThat(depthLimit.isHigherThan(type)).as("isHigherThan(%s)", type).isTrue();
  215. }
  216. for (Type reportType : from(VIEWS_TYPES).filter(not(in(asList(types))))) {
  217. assertThat(depthLimit.isHigherThan(reportType)).as("isHigherThan(%s)", reportType).isFalse();
  218. }
  219. }
  220. private void assertIsDeeperThanReportType(CrawlerDepthLimit depthLimit, Type... types) {
  221. for (Type type : types) {
  222. assertThat(depthLimit.isDeeperThan(type)).as("isDeeperThan(%s)", type).isTrue();
  223. }
  224. for (Type reportType : from(REPORT_TYPES).filter(not(in(asList(types))))) {
  225. assertThat(depthLimit.isDeeperThan(reportType)).as("isDeeperThan(%s)", reportType).isFalse();
  226. }
  227. }
  228. private void assertIsDeeperThanViewsType(CrawlerDepthLimit depthLimit, Type... types) {
  229. for (Type type : types) {
  230. assertThat(depthLimit.isDeeperThan(type)).as("isDeeperThan(%s)", type).isTrue();
  231. }
  232. for (Type reportType : from(VIEWS_TYPES).filter(not(in(asList(types))))) {
  233. assertThat(depthLimit.isDeeperThan(reportType)).as("isDeeperThan(%s)", reportType).isFalse();
  234. }
  235. }
  236. @Test
  237. @UseDataProvider("viewsTypes")
  238. public void reportMaxDepth_throws_IAE_if_type_is_views(Type viewsType) {
  239. assertThatThrownBy(() -> CrawlerDepthLimit.reportMaxDepth(viewsType))
  240. .isInstanceOf(IllegalArgumentException.class)
  241. .hasMessage("A Report max depth must be a report type");
  242. }
  243. @Test
  244. @UseDataProvider("reportTypes")
  245. public void reportMaxDepth_accepts_type_if_report_type(Type reportType) {
  246. CrawlerDepthLimit.reportMaxDepth(reportType);
  247. }
  248. @Test
  249. @UseDataProvider("reportTypes")
  250. public void withViewsMaxDepth_throws_IAE_if_type_is_report(Type reportType) {
  251. assertThatThrownBy(() -> CrawlerDepthLimit.reportMaxDepth(reportType).withViewsMaxDepth(reportType))
  252. .isInstanceOf(IllegalArgumentException.class)
  253. .hasMessage("A Views max depth must be a views type");
  254. }
  255. @DataProvider
  256. public static Object[][] viewsTypes() {
  257. return from(VIEWS_TYPES).transform(new Function<Type, Object[]>() {
  258. @Nullable
  259. @Override
  260. public Object[] apply(Type input) {
  261. return new Object[] {input};
  262. }
  263. }).toArray(Object[].class);
  264. }
  265. @DataProvider
  266. public static Object[][] reportTypes() {
  267. return from(REPORT_TYPES).transform(new Function<Type, Object[]>() {
  268. @Nullable
  269. @Override
  270. public Object[] apply(Type input) {
  271. return new Object[] {input};
  272. }
  273. }).toArray(Object[].class);
  274. }
  275. }