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.

DefaultFilterableIssueTest.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.issue;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.api.batch.fs.InputComponent;
  24. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  25. import org.sonar.scanner.protocol.Constants.Severity;
  26. import org.sonar.scanner.protocol.output.ScannerReport.Issue;
  27. import org.sonar.scanner.protocol.output.ScannerReport.TextRange;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. public class DefaultFilterableIssueTest {
  32. private DefaultFilterableIssue issue;
  33. private DefaultInputProject mockedProject;
  34. private InputComponent component;
  35. private Issue rawIssue;
  36. @Before
  37. public void setUp() {
  38. mockedProject = mock(DefaultInputProject.class);
  39. component = mock(InputComponent.class);
  40. when(component.key()).thenReturn("foo");
  41. }
  42. private Issue createIssue() {
  43. Issue.Builder builder = Issue.newBuilder();
  44. builder.setGap(3.0);
  45. builder.setTextRange(TextRange.newBuilder()
  46. .setStartLine(30)
  47. .setStartOffset(10)
  48. .setEndLine(31)
  49. .setEndOffset(3));
  50. builder.setSeverity(Severity.MAJOR);
  51. return builder.build();
  52. }
  53. private Issue createIssueWithoutFields() {
  54. Issue.Builder builder = Issue.newBuilder();
  55. builder.setSeverity(Severity.MAJOR);
  56. return builder.build();
  57. }
  58. @Test
  59. public void testRoundTrip() {
  60. rawIssue = createIssue();
  61. issue = new DefaultFilterableIssue(mockedProject, rawIssue, component);
  62. when(mockedProject.key()).thenReturn("projectKey");
  63. assertThat(issue.componentKey()).isEqualTo(component.key());
  64. assertThat(issue.line()).isEqualTo(30);
  65. assertThat(issue.textRange().start().line()).isEqualTo(30);
  66. assertThat(issue.textRange().start().lineOffset()).isEqualTo(10);
  67. assertThat(issue.textRange().end().line()).isEqualTo(31);
  68. assertThat(issue.textRange().end().lineOffset()).isEqualTo(3);
  69. assertThat(issue.projectKey()).isEqualTo("projectKey");
  70. assertThat(issue.gap()).isEqualTo(3.0);
  71. assertThat(issue.severity()).isEqualTo("MAJOR");
  72. }
  73. @Test
  74. public void nullValues() {
  75. rawIssue = createIssueWithoutFields();
  76. issue = new DefaultFilterableIssue(mockedProject, rawIssue, component);
  77. assertThat(issue.line()).isNull();
  78. assertThat(issue.gap()).isNull();
  79. }
  80. }