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.

MeasureSensorTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.lang;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import org.apache.commons.io.FileUtils;
  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.TestInputFileBuilder;
  31. import org.sonar.api.batch.measure.MetricFinder;
  32. import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
  33. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  34. import org.sonar.api.measures.CoreMetrics;
  35. import org.sonar.api.measures.Metric;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. public class MeasureSensorTest {
  41. private MeasureSensor sensor;
  42. private SensorContextTester context;
  43. @Rule
  44. public TemporaryFolder temp = new TemporaryFolder();
  45. private File baseDir;
  46. private MetricFinder metricFinder;
  47. @Before
  48. public void prepare() throws IOException {
  49. baseDir = temp.newFolder();
  50. metricFinder = mock(MetricFinder.class);
  51. sensor = new MeasureSensor(metricFinder);
  52. context = SensorContextTester.create(baseDir);
  53. }
  54. @Test
  55. public void testDescriptor() {
  56. sensor.describe(new DefaultSensorDescriptor());
  57. }
  58. @Test
  59. public void testNoExecutionIfNoMeasureFile() {
  60. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").build();
  61. context.fileSystem().add(inputFile);
  62. sensor.execute(context);
  63. }
  64. @Test
  65. public void testExecution() throws IOException {
  66. File measures = new File(baseDir, "src/foo.xoo.measures");
  67. FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment", StandardCharsets.UTF_8);
  68. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
  69. context.fileSystem().add(inputFile);
  70. Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL)
  71. .create();
  72. when(metricFinder.<Integer>findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
  73. when(metricFinder.<Double>findByKey("branch_coverage")).thenReturn(CoreMetrics.BRANCH_COVERAGE);
  74. when(metricFinder.<Long>findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT);
  75. when(metricFinder.<Boolean>findByKey("bool")).thenReturn(booleanMetric);
  76. sensor.execute(context);
  77. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.NCLOC).value()).isEqualTo(12);
  78. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.BRANCH_COVERAGE).value()).isEqualTo(5.3);
  79. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.TECHNICAL_DEBT).value()).isEqualTo(300L);
  80. assertThat(context.measure("foo:src/foo.xoo", booleanMetric).value()).isTrue();
  81. }
  82. @Test
  83. public void testExecutionForFoldersMeasures_no_measures() throws IOException {
  84. File measures = new File(baseDir, "src/folder.measures");
  85. FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment", StandardCharsets.UTF_8);
  86. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
  87. context.fileSystem().add(inputFile);
  88. Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL)
  89. .create();
  90. when(metricFinder.<Integer>findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
  91. when(metricFinder.<Double>findByKey("branch_coverage")).thenReturn(CoreMetrics.BRANCH_COVERAGE);
  92. when(metricFinder.<Long>findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT);
  93. when(metricFinder.<Boolean>findByKey("bool")).thenReturn(booleanMetric);
  94. sensor.execute(context);
  95. assertThat(context.measure("foo:src", CoreMetrics.NCLOC)).isNull();
  96. }
  97. @Test
  98. public void failIfMetricNotFound() throws IOException {
  99. File measures = new File(baseDir, "src/foo.xoo.measures");
  100. FileUtils.write(measures, "unknow:12\n\n#comment");
  101. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
  102. context.fileSystem().add(inputFile);
  103. assertThatThrownBy(() -> sensor.execute(context))
  104. .isInstanceOf(IllegalStateException.class);
  105. }
  106. }