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.

ComponentIssuesRepositoryImplTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.issue;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.component.Component;
  25. import org.sonar.core.issue.DefaultIssue;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  28. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  29. public class ComponentIssuesRepositoryImplTest {
  30. static final Component FILE_1 = builder(Component.Type.FILE, 1).build();
  31. static final Component FILE_2 = builder(Component.Type.FILE, 2).build();
  32. static final DefaultIssue DUMB_ISSUE = new DefaultIssue().setKey("ISSUE");
  33. ComponentIssuesRepositoryImpl sut = new ComponentIssuesRepositoryImpl();
  34. @Test
  35. public void get_issues() {
  36. sut.setIssues(FILE_1, Arrays.asList(DUMB_ISSUE));
  37. assertThat(sut.getIssues(FILE_1)).containsOnly(DUMB_ISSUE);
  38. }
  39. @Test
  40. public void no_issues_on_dir() {
  41. assertThat(sut.getIssues(builder(Component.Type.DIRECTORY, 1).build())).isEmpty();
  42. }
  43. @Test
  44. public void set_empty_issues() {
  45. sut.setIssues(FILE_1, Collections.emptyList());
  46. assertThat(sut.getIssues(FILE_1)).isEmpty();
  47. }
  48. @Test
  49. public void fail_with_NPE_when_setting_issues_with_null_component() {
  50. assertThatThrownBy(() -> sut.setIssues(null, Arrays.asList(DUMB_ISSUE)))
  51. .isInstanceOf(NullPointerException.class)
  52. .hasMessage("component cannot be null");
  53. }
  54. @Test
  55. public void fail_with_NPE_when_setting_issues_with_null_issues() {
  56. assertThatThrownBy(() -> sut.setIssues(FILE_1, null))
  57. .isInstanceOf(NullPointerException.class)
  58. .hasMessage("issues cannot be null");
  59. }
  60. @Test
  61. public void fail_with_IAE_when_getting_issues_on_different_component() {
  62. assertThatThrownBy(() -> {
  63. sut.setIssues(FILE_1, Arrays.asList(DUMB_ISSUE));
  64. sut.getIssues(FILE_2);
  65. })
  66. .isInstanceOf(IllegalArgumentException.class)
  67. .hasMessage("Only issues from component '1' are available, but wanted component is '2'.");
  68. }
  69. @Test
  70. public void fail_with_ISE_when_getting_issues_but_issues_are_null() {
  71. assertThatThrownBy(() -> {
  72. sut.getIssues(FILE_1);
  73. })
  74. .isInstanceOf(IllegalStateException.class)
  75. .hasMessage("Issues have not been initialized");
  76. }
  77. }