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.

TreeRootHolderImplTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Component.Type.PROJECT_VIEW;
  28. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  29. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
  30. public class TreeRootHolderImplTest {
  31. private static final ReportComponent SOME_REPORT_COMPONENT_TREE = ReportComponent.builder(PROJECT, 1)
  32. .addChildren(ReportComponent.builder(DIRECTORY, 2)
  33. .addChildren(
  34. ReportComponent.builder(FILE, 3).build())
  35. .build())
  36. .build();
  37. private static final ViewsComponent SOME_VIEWS_COMPONENT_TREE = ViewsComponent.builder(VIEW, 1)
  38. .addChildren(
  39. ViewsComponent.builder(VIEW, 2)
  40. .addChildren(ViewsComponent.builder(PROJECT_VIEW, 3).build())
  41. .build())
  42. .build();
  43. private TreeRootHolderImpl underTest = new TreeRootHolderImpl();
  44. @Test
  45. public void setRoots_throws_NPE_if_root_is_null() {
  46. assertThatThrownBy(() -> underTest.setRoots(null, DUMB_PROJECT))
  47. .isInstanceOf(NullPointerException.class)
  48. .hasMessage("root can not be null");
  49. }
  50. @Test
  51. public void setRoots_throws_NPE_if_reportRoot_is_null() {
  52. assertThatThrownBy(() -> underTest.setRoots(DUMB_PROJECT, null))
  53. .isInstanceOf(NullPointerException.class)
  54. .hasMessageContaining("root can not be null");
  55. }
  56. @Test
  57. public void setRoot_throws_ISE_when_called_twice() {
  58. underTest.setRoots(DUMB_PROJECT, DUMB_PROJECT);
  59. assertThatThrownBy(() -> underTest.setRoots(null, DUMB_PROJECT))
  60. .isInstanceOf(IllegalStateException.class)
  61. .hasMessage("root can not be set twice in holder");
  62. }
  63. @Test
  64. public void getRoot_throws_ISE_if_root_has_not_been_set_yet() {
  65. assertThatThrownBy(() -> underTest.getRoot())
  66. .isInstanceOf(IllegalStateException.class)
  67. .hasMessage("Holder has not been initialized yet");
  68. }
  69. @Test
  70. public void getComponentByRef_throws_ISE_if_root_has_not_been_set() {
  71. assertThatThrownBy(() -> underTest.getComponentByRef(12))
  72. .isInstanceOf(IllegalStateException.class)
  73. .hasMessage("Holder has not been initialized yet");
  74. }
  75. @Test
  76. public void getComponentByRef_returns_any_report_component_in_the_tree() {
  77. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  78. for (int i = 1; i <= 3; i++) {
  79. assertThat(underTest.getComponentByRef(i).getReportAttributes().getRef()).isEqualTo(i);
  80. }
  81. }
  82. @Test
  83. public void getOptionalComponentByRef_returns_any_report_component_in_the_tree() {
  84. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  85. for (int i = 1; i <= 3; i++) {
  86. assertThat(underTest.getOptionalComponentByRef(i).get().getReportAttributes().getRef()).isEqualTo(i);
  87. }
  88. }
  89. @Test
  90. public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() {
  91. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  92. assertThatThrownBy(() -> underTest.getComponentByRef(6))
  93. .isInstanceOf(IllegalArgumentException.class)
  94. .hasMessage("Component with ref '6' can't be found");
  95. }
  96. @Test
  97. public void getOptionalComponentByRef_returns_empty_if_holder_does_not_contain_specified_component() {
  98. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  99. assertThat(underTest.getOptionalComponentByRef(6)).isEmpty();
  100. }
  101. @Test
  102. public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() {
  103. underTest.setRoots(SOME_VIEWS_COMPONENT_TREE, DUMB_PROJECT);
  104. assertThatThrownBy(() -> underTest.getComponentByRef(1))
  105. .isInstanceOf(IllegalArgumentException.class)
  106. .hasMessage("Component with ref '1' can't be found");
  107. }
  108. @Test
  109. public void verify_setRoots_getRoot() {
  110. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  111. assertThat(underTest.getRoot()).isSameAs(SOME_REPORT_COMPONENT_TREE);
  112. }
  113. @Test
  114. public void verify_setRoots_getReportRoot() {
  115. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  116. assertThat(underTest.getReportTreeRoot()).isSameAs(DUMB_PROJECT);
  117. }
  118. @Test
  119. public void getSize_throws_ISE_if_not_initialized() {
  120. assertThatThrownBy(() -> underTest.getSize())
  121. .isInstanceOf(IllegalStateException.class)
  122. .hasMessage("Holder has not been initialized yet");
  123. }
  124. @Test
  125. public void getSize_counts_number_of_components() {
  126. underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
  127. assertThat(underTest.getSize()).isEqualTo(3);
  128. }
  129. }