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.

ModuleSensorContextTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.sensor;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.junit.rules.TemporaryFolder;
  26. import org.sonar.api.SonarEdition;
  27. import org.sonar.api.SonarQubeSide;
  28. import org.sonar.api.SonarRuntime;
  29. import org.sonar.api.batch.fs.InputModule;
  30. import org.sonar.api.batch.measure.MetricFinder;
  31. import org.sonar.api.batch.rule.ActiveRules;
  32. import org.sonar.api.batch.sensor.internal.SensorStorage;
  33. import org.sonar.api.config.internal.MapSettings;
  34. import org.sonar.api.batch.fs.internal.DefaultFileSystem;
  35. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  36. import org.sonar.api.internal.SonarRuntimeImpl;
  37. import org.sonar.api.measures.CoreMetrics;
  38. import org.sonar.api.utils.Version;
  39. import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.when;
  43. public class ModuleSensorContextTest {
  44. @Rule
  45. public TemporaryFolder temp = new TemporaryFolder();
  46. @Rule
  47. public ExpectedException thrown = ExpectedException.none();
  48. private ActiveRules activeRules;
  49. private DefaultFileSystem fs;
  50. private ModuleSensorContext adaptor;
  51. private MapSettings settings;
  52. private SensorStorage sensorStorage;
  53. private SonarRuntime runtime;
  54. @Before
  55. public void prepare() throws Exception {
  56. activeRules = new ActiveRulesBuilder().build();
  57. fs = new DefaultFileSystem(temp.newFolder().toPath());
  58. MetricFinder metricFinder = mock(MetricFinder.class);
  59. when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
  60. when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
  61. settings = new MapSettings();
  62. sensorStorage = mock(SensorStorage.class);
  63. runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
  64. adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), fs, activeRules, sensorStorage, runtime);
  65. }
  66. @Test
  67. public void shouldProvideComponents() {
  68. assertThat(adaptor.activeRules()).isEqualTo(activeRules);
  69. assertThat(adaptor.fileSystem()).isEqualTo(fs);
  70. assertThat(adaptor.getSonarQubeVersion()).isEqualTo(Version.parse("5.5"));
  71. assertThat(adaptor.runtime()).isEqualTo(runtime);
  72. assertThat(adaptor.newIssue()).isNotNull();
  73. assertThat(adaptor.newExternalIssue()).isNotNull();
  74. assertThat(adaptor.newAdHocRule()).isNotNull();
  75. assertThat(adaptor.newMeasure()).isNotNull();
  76. assertThat(adaptor.newAnalysisError()).isEqualTo(ModuleSensorContext.NO_OP_NEW_ANALYSIS_ERROR);
  77. assertThat(adaptor.isCancelled()).isFalse();
  78. assertThat(adaptor.newSignificantCode()).isNotNull();
  79. }
  80. }