3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.mediumtest.tests;
22 import org.hamcrest.Description;
24 import org.hamcrest.TypeSafeMatcher;
25 import org.junit.Rule;
26 import org.junit.rules.ExpectedException;
27 import com.google.common.collect.ImmutableMap;
28 import org.apache.commons.io.FileUtils;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33 import org.sonar.api.batch.fs.InputFile;
34 import org.sonar.scanner.mediumtest.ScannerMediumTester;
35 import org.sonar.scanner.mediumtest.TaskResult;
36 import org.sonar.xoo.XooPlugin;
39 import java.io.IOException;
41 import static org.assertj.core.api.Assertions.assertThat;
43 public class CoveragePerTestMediumTest {
46 public TemporaryFolder temp = new TemporaryFolder();
49 public ExpectedException exception = ExpectedException.none();
51 public ScannerMediumTester tester = ScannerMediumTester.builder()
52 .registerPlugin("xoo", new XooPlugin())
53 .addDefaultQProfile("xoo", "Sonar Way")
57 public void prepare() {
68 public void invalidCoverage() throws IOException {
69 File baseDir = createTestFiles();
70 File srcDir = new File(baseDir, "src");
72 File coverageFile = new File(srcDir, "sample.xoo.coverage");
73 FileUtils.write(coverageFile, "0:2\n");
75 exception.expect(IllegalStateException.class);
76 exception.expectMessage("Error processing line 1 of file");
77 exception.expectCause(new TypeSafeMatcher<Throwable>() {
80 public void describeTo(Description description) {
85 protected boolean matchesSafely(Throwable item) {
86 return item.getMessage().contains("Line number must be strictly positive");
94 public void coveragePerTestInReport() throws IOException {
95 File baseDir = createTestFiles();
96 File testDir = new File(baseDir, "test");
98 File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
99 FileUtils.write(xooTestExecutionFile, "some test:4:::OK:UNIT\n" +
100 "another test:10:::OK:UNIT\n" +
101 "test without coverage:10:::OK:UNIT\n");
103 File xooCoveragePerTestFile = new File(testDir, "sampleTest.xoo.testcoverage");
104 FileUtils.write(xooCoveragePerTestFile, "some test;src/sample.xoo,10,11;src/sample2.xoo,1,2\n" +
105 "another test;src/sample.xoo,10,20\n");
107 TaskResult result = runTask(baseDir);
109 InputFile file = result.inputFile("test/sampleTest.xoo");
110 org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail someTest = result.coveragePerTestFor(file, "some test");
111 assertThat(someTest.getCoveredFileList()).hasSize(2);
112 assertThat(someTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
113 assertThat(someTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 11);
114 assertThat(someTest.getCoveredFile(1).getFileRef()).isGreaterThan(0);
115 assertThat(someTest.getCoveredFile(1).getCoveredLineList()).containsExactly(1, 2);
117 org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail anotherTest = result.coveragePerTestFor(file, "another test");
118 assertThat(anotherTest.getCoveredFileList()).hasSize(1);
119 assertThat(anotherTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
120 assertThat(anotherTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 20);
123 private TaskResult runTask(File baseDir) {
124 return tester.newTask()
125 .properties(ImmutableMap.<String, String>builder()
126 .put("sonar.task", "scan")
127 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
128 .put("sonar.projectKey", "com.foo.project")
129 .put("sonar.projectName", "Foo Project")
130 .put("sonar.projectVersion", "1.0-SNAPSHOT")
131 .put("sonar.projectDescription", "Description of Foo Project")
132 .put("sonar.sources", "src")
133 .put("sonar.tests", "test")
138 private File createTestFiles() throws IOException {
139 File baseDir = temp.getRoot();
140 File srcDir = new File(baseDir, "src");
142 File testDir = new File(baseDir, "test");
145 File xooFile = new File(srcDir, "sample.xoo");
146 FileUtils.write(xooFile, "foo");
148 File xooFile2 = new File(srcDir, "sample2.xoo");
149 FileUtils.write(xooFile2, "foo");
151 File xooTestFile = new File(testDir, "sampleTest.xoo");
152 FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped");
154 File xooTestFile2 = new File(testDir, "sample2Test.xoo");
155 FileUtils.write(xooTestFile2, "test file tests");