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

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