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.7KB

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