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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.lang;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import org.apache.commons.io.FileUtils;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  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.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. public class MeasureSensorTest {
  40. private MeasureSensor sensor;
  41. private SensorContextTester context;
  42. @Rule
  43. public TemporaryFolder temp = new TemporaryFolder();
  44. @Rule
  45. public ExpectedException thrown = ExpectedException.none();
  46. private File baseDir;
  47. private MetricFinder metricFinder;
  48. @Before
  49. public void prepare() throws IOException {
  50. baseDir = temp.newFolder();
  51. metricFinder = mock(MetricFinder.class);
  52. sensor = new MeasureSensor(metricFinder);
  53. context = SensorContextTester.create(baseDir);
  54. }
  55. @Test
  56. public void testDescriptor() {
  57. sensor.describe(new DefaultSensorDescriptor());
  58. }
  59. @Test
  60. public void testNoExecutionIfNoMeasureFile() {
  61. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").build();
  62. context.fileSystem().add(inputFile);
  63. sensor.execute(context);
  64. }
  65. @Test
  66. public void testExecution() throws IOException {
  67. File measures = new File(baseDir, "src/foo.xoo.measures");
  68. FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment");
  69. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
  70. context.fileSystem().add(inputFile);
  71. Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL)
  72. .create();
  73. when(metricFinder.<Integer>findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
  74. when(metricFinder.<Double>findByKey("branch_coverage")).thenReturn(CoreMetrics.BRANCH_COVERAGE);
  75. when(metricFinder.<Long>findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT);
  76. when(metricFinder.<Boolean>findByKey("bool")).thenReturn(booleanMetric);
  77. sensor.execute(context);
  78. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.NCLOC).value()).isEqualTo(12);
  79. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.BRANCH_COVERAGE).value()).isEqualTo(5.3);
  80. assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.TECHNICAL_DEBT).value()).isEqualTo(300L);
  81. assertThat(context.measure("foo:src/foo.xoo", booleanMetric).value()).isTrue();
  82. }
  83. @Test
  84. public void failIfMetricNotFound() throws IOException {
  85. File measures = new File(baseDir, "src/foo.xoo.measures");
  86. FileUtils.write(measures, "unknow:12\n\n#comment");
  87. InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
  88. context.fileSystem().add(inputFile);
  89. thrown.expect(IllegalStateException.class);
  90. sensor.execute(context);
  91. }
  92. }