您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CrawlerDepthLimitTest.java 11KB

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