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.

MeasuresPublisherTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 java.util.Collections;
  24. import org.apache.commons.lang.exception.ExceptionUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  31. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  32. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  33. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  34. import org.sonar.api.measures.CoreMetrics;
  35. import org.sonar.core.util.CloseableIterator;
  36. import org.sonar.scanner.deprecated.test.TestPlanBuilder;
  37. import org.sonar.scanner.protocol.output.ScannerReport;
  38. import org.sonar.scanner.protocol.output.ScannerReportReader;
  39. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  40. import org.sonar.scanner.scan.branch.BranchConfiguration;
  41. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  42. import org.sonar.scanner.scan.measure.MeasureCache;
  43. import static java.util.Arrays.asList;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.junit.Assert.fail;
  46. import static org.mockito.ArgumentMatchers.anyString;
  47. import static org.mockito.Mockito.mock;
  48. import static org.mockito.Mockito.when;
  49. public class MeasuresPublisherTest {
  50. @Rule
  51. public ExpectedException thrown = ExpectedException.none();
  52. @Rule
  53. public TemporaryFolder temp = new TemporaryFolder();
  54. private MeasureCache measureCache;
  55. private MeasuresPublisher publisher;
  56. private File outputDir;
  57. private ScannerReportWriter writer;
  58. private DefaultInputFile inputFile;
  59. private DefaultInputProject project;
  60. @Before
  61. public void prepare() throws IOException {
  62. String projectKey = "foo";
  63. project = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder());
  64. inputFile = new TestInputFileBuilder(projectKey, "src/Foo.php").setPublish(true).build();
  65. InputComponentStore componentCache = new InputComponentStore(mock(BranchConfiguration.class));
  66. componentCache.put(projectKey, inputFile);
  67. measureCache = mock(MeasureCache.class);
  68. when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList());
  69. publisher = new MeasuresPublisher(componentCache, measureCache, mock(TestPlanBuilder.class));
  70. outputDir = temp.newFolder();
  71. writer = new ScannerReportWriter(outputDir);
  72. }
  73. @Test
  74. public void publishMeasures() throws Exception {
  75. DefaultMeasure<Integer> measure = new DefaultMeasure<Integer>().forMetric(CoreMetrics.LINES_TO_COVER)
  76. .withValue(2);
  77. // String value
  78. DefaultMeasure<String> stringMeasure = new DefaultMeasure<String>().forMetric(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)
  79. .withValue("foo bar");
  80. when(measureCache.byComponentKey(inputFile.key())).thenReturn(asList(measure, stringMeasure));
  81. publisher.publish(writer);
  82. ScannerReportReader reader = new ScannerReportReader(outputDir);
  83. assertThat(reader.readComponentMeasures(project.scannerId())).hasSize(0);
  84. try (CloseableIterator<ScannerReport.Measure> componentMeasures = reader.readComponentMeasures(inputFile.scannerId())) {
  85. assertThat(componentMeasures).hasSize(2);
  86. }
  87. }
  88. @Test
  89. public void fail_with_IAE_when_measure_has_no_value() throws Exception {
  90. DefaultMeasure<Integer> measure = new DefaultMeasure<Integer>().forMetric(CoreMetrics.LINES_TO_COVER);
  91. when(measureCache.byComponentKey(inputFile.key())).thenReturn(Collections.singletonList(measure));
  92. try {
  93. publisher.publish(writer);
  94. fail();
  95. } catch (RuntimeException e) {
  96. assertThat(ExceptionUtils.getFullStackTrace(e)).contains("Measure on metric 'lines_to_cover' and component 'foo:src/Foo.php' has no value, but it's not allowed");
  97. }
  98. }
  99. }