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

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