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.

CoveragePerTestMediumTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.mediumtest.tests;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.nio.charset.StandardCharsets;
  25. import org.apache.commons.io.FileUtils;
  26. import org.hamcrest.Description;
  27. import org.hamcrest.TypeSafeMatcher;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonar.api.batch.fs.InputFile;
  33. import org.sonar.scanner.mediumtest.ScannerMediumTester;
  34. import org.sonar.scanner.mediumtest.AnalysisResult;
  35. import org.sonar.xoo.XooPlugin;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. public class CoveragePerTestMediumTest {
  38. @Rule
  39. public TemporaryFolder temp = new TemporaryFolder();
  40. @Rule
  41. public ExpectedException exception = ExpectedException.none();
  42. @Rule
  43. public ScannerMediumTester tester = new ScannerMediumTester()
  44. .registerPlugin("xoo", new XooPlugin())
  45. .addDefaultQProfile("xoo", "Sonar Way");
  46. @Test
  47. // SONAR-6183
  48. public void invalidCoverage() throws IOException {
  49. File baseDir = createTestFiles();
  50. File srcDir = new File(baseDir, "src");
  51. File coverageFile = new File(srcDir, "sample.xoo.coverage");
  52. FileUtils.write(coverageFile, "0:2\n", StandardCharsets.UTF_8);
  53. exception.expect(IllegalStateException.class);
  54. exception.expectMessage("Error processing line 1 of file");
  55. exception.expectCause(new TypeSafeMatcher<Throwable>() {
  56. @Override
  57. public void describeTo(Description description) {
  58. // nothing to do
  59. }
  60. @Override
  61. protected boolean matchesSafely(Throwable item) {
  62. return item.getMessage().contains("Line number must be strictly positive");
  63. }
  64. });
  65. runTask(baseDir);
  66. }
  67. @Test
  68. public void coveragePerTestInReport() throws IOException {
  69. File baseDir = createTestFiles();
  70. File testDir = new File(baseDir, "test");
  71. File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
  72. FileUtils.write(xooTestExecutionFile, "some test:4:::OK:UNIT\n" +
  73. "another test:10:::OK:UNIT\n" +
  74. "test without coverage:10:::OK:UNIT\n", StandardCharsets.UTF_8);
  75. File xooCoveragePerTestFile = new File(testDir, "sampleTest.xoo.testcoverage");
  76. FileUtils.write(xooCoveragePerTestFile, "some test;src/sample.xoo,10,11;src/sample2.xoo,1,2\n" +
  77. "another test;src/sample.xoo,10,20\n", StandardCharsets.UTF_8);
  78. AnalysisResult result = runTask(baseDir);
  79. InputFile file = result.inputFile("test/sampleTest.xoo");
  80. org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail someTest = result.coveragePerTestFor(file, "some test");
  81. assertThat(someTest.getCoveredFileList()).hasSize(2);
  82. assertThat(someTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
  83. assertThat(someTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 11);
  84. assertThat(someTest.getCoveredFile(1).getFileRef()).isGreaterThan(0);
  85. assertThat(someTest.getCoveredFile(1).getCoveredLineList()).containsExactly(1, 2);
  86. org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail anotherTest = result.coveragePerTestFor(file, "another test");
  87. assertThat(anotherTest.getCoveredFileList()).hasSize(1);
  88. assertThat(anotherTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
  89. assertThat(anotherTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 20);
  90. }
  91. private AnalysisResult runTask(File baseDir) {
  92. return tester.newAnalysis()
  93. .properties(ImmutableMap.<String, String>builder()
  94. .put("sonar.task", "scan")
  95. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  96. .put("sonar.projectKey", "com.foo.project")
  97. .put("sonar.projectName", "Foo Project")
  98. .put("sonar.projectVersion", "1.0-SNAPSHOT")
  99. .put("sonar.projectDescription", "Description of Foo Project")
  100. .put("sonar.sources", "src")
  101. .put("sonar.tests", "test")
  102. .build())
  103. .execute();
  104. }
  105. private File createTestFiles() throws IOException {
  106. File baseDir = temp.getRoot();
  107. File srcDir = new File(baseDir, "src");
  108. srcDir.mkdir();
  109. File testDir = new File(baseDir, "test");
  110. testDir.mkdir();
  111. File xooFile = new File(srcDir, "sample.xoo");
  112. FileUtils.write(xooFile, "foo", StandardCharsets.UTF_8);
  113. File xooFile2 = new File(srcDir, "sample2.xoo");
  114. FileUtils.write(xooFile2, "foo", StandardCharsets.UTF_8);
  115. File xooTestFile = new File(testDir, "sampleTest.xoo");
  116. FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped", StandardCharsets.UTF_8);
  117. File xooTestFile2 = new File(testDir, "sample2Test.xoo");
  118. FileUtils.write(xooTestFile2, "test file tests", StandardCharsets.UTF_8);
  119. return baseDir;
  120. }
  121. }