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 com.google.common.collect.ImmutableMap;
24 import java.io.IOException;
25 import org.apache.commons.io.FileUtils;
26 import org.hamcrest.Description;
27 import org.hamcrest.TypeSafeMatcher;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.rules.TemporaryFolder;
34 import org.sonar.api.batch.fs.InputFile;
35 import org.sonar.scanner.mediumtest.ScannerMediumTester;
36 import org.sonar.scanner.mediumtest.TaskResult;
37 import org.sonar.xoo.XooPlugin;
39 import static org.assertj.core.api.Assertions.assertThat;
41 public class CoveragePerTestMediumTest {
44 public TemporaryFolder temp = new TemporaryFolder();
47 public ExpectedException exception = ExpectedException.none();
49 public ScannerMediumTester tester = ScannerMediumTester.builder()
50 .registerPlugin("xoo", new XooPlugin())
51 .addDefaultQProfile("xoo", "Sonar Way")
55 public void prepare() {
66 public void invalidCoverage() throws IOException {
67 File baseDir = createTestFiles();
68 File srcDir = new File(baseDir, "src");
70 File coverageFile = new File(srcDir, "sample.xoo.coverage");
71 FileUtils.write(coverageFile, "0:2\n");
73 exception.expect(IllegalStateException.class);
74 exception.expectMessage("Error processing line 1 of file");
75 exception.expectCause(new TypeSafeMatcher<Throwable>() {
78 public void describeTo(Description description) {
83 protected boolean matchesSafely(Throwable item) {
84 return item.getMessage().contains("Line number must be strictly positive");
92 public void coveragePerTestInReport() throws IOException {
93 File baseDir = createTestFiles();
94 File testDir = new File(baseDir, "test");
96 File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
97 FileUtils.write(xooTestExecutionFile, "some test:4:::OK:UNIT\n" +
98 "another test:10:::OK:UNIT\n" +
99 "test without coverage:10:::OK:UNIT\n");
101 File xooCoveragePerTestFile = new File(testDir, "sampleTest.xoo.testcoverage");
102 FileUtils.write(xooCoveragePerTestFile, "some test;src/sample.xoo,10,11;src/sample2.xoo,1,2\n" +
103 "another test;src/sample.xoo,10,20\n");
105 TaskResult result = runTask(baseDir);
107 InputFile file = result.inputFile("test/sampleTest.xoo");
108 org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail someTest = result.coveragePerTestFor(file, "some test");
109 assertThat(someTest.getCoveredFileList()).hasSize(2);
110 assertThat(someTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
111 assertThat(someTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 11);
112 assertThat(someTest.getCoveredFile(1).getFileRef()).isGreaterThan(0);
113 assertThat(someTest.getCoveredFile(1).getCoveredLineList()).containsExactly(1, 2);
115 org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail anotherTest = result.coveragePerTestFor(file, "another test");
116 assertThat(anotherTest.getCoveredFileList()).hasSize(1);
117 assertThat(anotherTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
118 assertThat(anotherTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 20);
121 private TaskResult runTask(File baseDir) {
122 return tester.newTask()
123 .properties(ImmutableMap.<String, String>builder()
124 .put("sonar.task", "scan")
125 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
126 .put("sonar.projectKey", "com.foo.project")
127 .put("sonar.projectName", "Foo Project")
128 .put("sonar.projectVersion", "1.0-SNAPSHOT")
129 .put("sonar.projectDescription", "Description of Foo Project")
130 .put("sonar.sources", "src")
131 .put("sonar.tests", "test")
136 private File createTestFiles() throws IOException {
137 File baseDir = temp.getRoot();
138 File srcDir = new File(baseDir, "src");
140 File testDir = new File(baseDir, "test");
143 File xooFile = new File(srcDir, "sample.xoo");
144 FileUtils.write(xooFile, "foo");
146 File xooFile2 = new File(srcDir, "sample2.xoo");
147 FileUtils.write(xooFile2, "foo");
149 File xooTestFile = new File(testDir, "sampleTest.xoo");
150 FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped");
152 File xooTestFile2 = new File(testDir, "sample2Test.xoo");
153 FileUtils.write(xooTestFile2, "test file tests");