Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MultilineIssuesSensorTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.util.List;
  24. import java.util.stream.Collectors;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.api.batch.fs.InputFile;
  30. import org.sonar.api.batch.fs.internal.DefaultFileSystem;
  31. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  32. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  33. import org.sonar.api.batch.rule.ActiveRule;
  34. import org.sonar.api.batch.rule.ActiveRules;
  35. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  36. import org.sonar.api.batch.sensor.issue.Issue;
  37. import org.sonar.api.batch.sensor.issue.internal.DefaultIssueFlow;
  38. import org.sonar.api.batch.sensor.issue.internal.DefaultIssueFlow.Type;
  39. import org.sonar.api.internal.apachecommons.io.IOUtils;
  40. import org.sonar.xoo.Xoo;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.mockito.ArgumentMatchers.any;
  43. import static org.mockito.Mockito.mock;
  44. import static org.mockito.Mockito.when;
  45. public class MultilineIssuesSensorTest {
  46. @Rule
  47. public TemporaryFolder temp = new TemporaryFolder();
  48. private final ActiveRules activeRules = mock(ActiveRules.class);
  49. private final MultilineIssuesSensor sensor = new MultilineIssuesSensor();
  50. @Before
  51. public void before() {
  52. when(activeRules.find(any())).thenReturn(mock(ActiveRule.class));
  53. }
  54. @Test
  55. public void execute_dataAndExecutionFlowsAreDetected() throws IOException {
  56. DefaultInputFile inputFile = newTestFile(IOUtils.toString(getClass().getResource("dataflow.xoo"), StandardCharsets.UTF_8));
  57. DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder());
  58. fs.add(inputFile);
  59. SensorContextTester sensorContextTester = SensorContextTester.create(fs.baseDir());
  60. sensorContextTester.setFileSystem(fs);
  61. sensor.execute(sensorContextTester);
  62. assertThat(sensorContextTester.allIssues()).hasSize(1);
  63. List<Issue.Flow> flows = sensorContextTester.allIssues().iterator().next().flows();
  64. assertThat(flows).hasSize(2);
  65. List<DefaultIssueFlow> defaultIssueFlows = flows.stream().map(DefaultIssueFlow.class::cast).collect(Collectors.toList());
  66. assertThat(defaultIssueFlows).extracting(DefaultIssueFlow::getType).containsExactlyInAnyOrder(Type.DATA, Type.EXECUTION);
  67. }
  68. private DefaultInputFile newTestFile(String content) {
  69. return new TestInputFileBuilder("foo", "src/Foo.xoo")
  70. .setLanguage(Xoo.KEY)
  71. .setType(InputFile.Type.MAIN)
  72. .setContents(content)
  73. .build();
  74. }
  75. }