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.

OneIssuePerLineSensorTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.xoo.rule;
  21. import java.io.IOException;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.TemporaryFolder;
  25. import org.sonar.api.SonarEdition;
  26. import org.sonar.api.SonarQubeSide;
  27. import org.sonar.api.batch.rule.Severity;
  28. import org.sonar.api.batch.sensor.issue.Issue;
  29. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  30. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  31. import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
  32. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  33. import org.sonar.api.internal.SonarRuntimeImpl;
  34. import org.sonar.api.utils.Version;
  35. import org.sonar.xoo.Xoo;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. public class OneIssuePerLineSensorTest {
  38. @Rule
  39. public TemporaryFolder temp = new TemporaryFolder();
  40. private OneIssuePerLineSensor sensor = new OneIssuePerLineSensor();
  41. @Test
  42. public void testDescriptor() {
  43. DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
  44. sensor.describe(descriptor);
  45. assertThat(descriptor.ruleRepositories()).containsOnly(XooRulesDefinition.XOO_REPOSITORY, XooRulesDefinition.XOO2_REPOSITORY);
  46. }
  47. @Test
  48. public void testRule() throws IOException {
  49. DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
  50. .setLanguage(Xoo.KEY)
  51. .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
  52. .build();
  53. SensorContextTester context = SensorContextTester.create(temp.newFolder());
  54. context.fileSystem().add(inputFile);
  55. sensor.execute(context);
  56. assertThat(context.allIssues()).hasSize(10); // One issue per line
  57. for (Issue issue : context.allIssues()) {
  58. assertThat(issue.gap()).isNull();
  59. }
  60. }
  61. @Test
  62. public void testForceSeverity() throws IOException {
  63. DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
  64. .setLanguage(Xoo.KEY)
  65. .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
  66. .build();
  67. SensorContextTester context = SensorContextTester.create(temp.newFolder());
  68. context.fileSystem().add(inputFile);
  69. context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR");
  70. sensor.execute(context);
  71. assertThat(context.allIssues()).hasSize(10); // One issue per line
  72. for (Issue issue : context.allIssues()) {
  73. assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR);
  74. }
  75. }
  76. @Test
  77. public void testProvideGap() throws IOException {
  78. DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
  79. .setLanguage(Xoo.KEY)
  80. .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
  81. .build();
  82. SensorContextTester context = SensorContextTester.create(temp.newFolder());
  83. context.fileSystem().add(inputFile);
  84. context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2");
  85. sensor.execute(context);
  86. assertThat(context.allIssues()).hasSize(10); // One issue per line
  87. for (Issue issue : context.allIssues()) {
  88. assertThat(issue.gap()).isEqualTo(1.2d);
  89. }
  90. }
  91. @Test
  92. public void testProvideGap_before_5_5() throws IOException {
  93. DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
  94. .setLanguage(Xoo.KEY)
  95. .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
  96. .build();
  97. SensorContextTester context = SensorContextTester.create(temp.newFolder());
  98. context.fileSystem().add(inputFile);
  99. context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2");
  100. context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.parse("5.4"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
  101. sensor.execute(context);
  102. assertThat(context.allIssues()).hasSize(10); // One issue per line
  103. for (Issue issue : context.allIssues()) {
  104. assertThat(issue.gap()).isEqualTo(1.2d);
  105. }
  106. }
  107. }