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.

AnalysisErrorSensorTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 java.nio.charset.StandardCharsets;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.TemporaryFolder;
  31. import org.sonar.api.batch.sensor.error.AnalysisError;
  32. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  33. import org.sonar.api.batch.fs.internal.DefaultTextPointer;
  34. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  35. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. public class AnalysisErrorSensorTest {
  38. @Rule
  39. public TemporaryFolder temp = new TemporaryFolder();
  40. private AnalysisErrorSensor sensor;
  41. @Before
  42. public void setUp() {
  43. sensor = new AnalysisErrorSensor();
  44. }
  45. private void createErrorFile(Path baseDir) throws IOException {
  46. Path srcDir = baseDir.resolve("src");
  47. Files.createDirectories(srcDir);
  48. Path errorFile = srcDir.resolve("foo.xoo.error");
  49. List<String> errors = new ArrayList<>();
  50. errors.add("1,4,my error");
  51. Files.write(errorFile, errors, StandardCharsets.UTF_8);
  52. }
  53. @Test
  54. public void test() throws IOException {
  55. Path baseDir = temp.newFolder().toPath().toAbsolutePath();
  56. createErrorFile(baseDir);
  57. int[] startOffsets = {10, 20, 30, 40};
  58. int[] endOffsets = {19, 29, 39, 49};
  59. DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
  60. .setLanguage("xoo")
  61. .setOriginalLineStartOffsets(startOffsets)
  62. .setOriginalLineEndOffsets(endOffsets)
  63. .setModuleBaseDir(baseDir)
  64. .setLines(4)
  65. .build();
  66. SensorContextTester context = SensorContextTester.create(baseDir);
  67. context.fileSystem().add(inputFile);
  68. sensor.execute(context);
  69. assertThat(context.allAnalysisErrors()).hasSize(1);
  70. AnalysisError error = context.allAnalysisErrors().iterator().next();
  71. assertThat(error.inputFile()).isEqualTo(inputFile);
  72. assertThat(error.location()).isEqualTo(new DefaultTextPointer(1, 4));
  73. assertThat(error.message()).isEqualTo("my error");
  74. }
  75. }