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.

CoveragePublisherTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.scanner.report;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.TemporaryFolder;
  27. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  28. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  29. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  30. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  31. import org.sonar.api.measures.CoreMetrics;
  32. import org.sonar.core.util.CloseableIterator;
  33. import org.sonar.scanner.protocol.output.ScannerReport.LineCoverage;
  34. import org.sonar.scanner.protocol.output.ScannerReportReader;
  35. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  36. import org.sonar.scanner.scan.branch.BranchConfiguration;
  37. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  38. import org.sonar.scanner.scan.measure.MeasureCache;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.mockito.ArgumentMatchers.anyString;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.when;
  43. public class CoveragePublisherTest {
  44. @Rule
  45. public TemporaryFolder temp = new TemporaryFolder();
  46. private MeasureCache measureCache;
  47. private CoveragePublisher publisher;
  48. private DefaultInputFile inputFile;
  49. @Before
  50. public void prepare() throws IOException {
  51. String moduleKey = "foo";
  52. inputFile = new TestInputFileBuilder(moduleKey, "src/Foo.php").setLines(5).build();
  53. DefaultInputProject rootModule = TestInputFileBuilder.newDefaultInputProject(moduleKey, temp.newFolder());
  54. InputComponentStore componentCache = new InputComponentStore(mock(BranchConfiguration.class));
  55. componentCache.put(moduleKey, inputFile);
  56. measureCache = mock(MeasureCache.class);
  57. when(measureCache.byMetric(anyString(), anyString())).thenReturn(null);
  58. publisher = new CoveragePublisher(componentCache, measureCache);
  59. }
  60. @Test
  61. public void publishCoverage() throws Exception {
  62. DefaultMeasure<String> utLineHits = new DefaultMeasure<String>().forMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA).withValue("2=1;3=1;5=0;6=3");
  63. when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn((DefaultMeasure) utLineHits);
  64. DefaultMeasure<String> conditionsByLine = new DefaultMeasure<String>().forMetric(CoreMetrics.CONDITIONS_BY_LINE).withValue("3=4");
  65. when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn((DefaultMeasure) conditionsByLine);
  66. DefaultMeasure<String> coveredConditionsByUts = new DefaultMeasure<String>().forMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE).withValue("3=2");
  67. when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn((DefaultMeasure) coveredConditionsByUts);
  68. File outputDir = temp.newFolder();
  69. ScannerReportWriter writer = new ScannerReportWriter(outputDir);
  70. publisher.publish(writer);
  71. try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(inputFile.scannerId())) {
  72. assertThat(it.next()).isEqualTo(LineCoverage.newBuilder()
  73. .setLine(2)
  74. .setHits(true)
  75. .build());
  76. assertThat(it.next()).isEqualTo(LineCoverage.newBuilder()
  77. .setLine(3)
  78. .setHits(true)
  79. .setConditions(4)
  80. .setCoveredConditions(2)
  81. .build());
  82. assertThat(it.next()).isEqualTo(LineCoverage.newBuilder()
  83. .setLine(5)
  84. .setHits(false)
  85. .build());
  86. }
  87. }
  88. }