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.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.rules.TemporaryFolder;
30 import org.sonar.api.batch.fs.InputFile;
31 import org.sonar.api.measures.CoreMetrics;
32 import org.sonar.scanner.mediumtest.ScannerMediumTester;
33 import org.sonar.scanner.mediumtest.TaskResult;
34 import org.sonar.scanner.protocol.output.ScannerReport;
35 import org.sonar.scanner.protocol.output.ScannerReport.Test.TestStatus;
36 import org.sonar.xoo.XooPlugin;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.tuple;
41 public class GenericTestExecutionMediumTest {
44 public TemporaryFolder temp = new TemporaryFolder();
46 public ScannerMediumTester tester = ScannerMediumTester.builder()
47 .registerPlugin("xoo", new XooPlugin())
48 .addDefaultQProfile("xoo", "Sonar Way")
52 public void prepare() {
62 public void unitTests() throws IOException {
64 File baseDir = temp.getRoot();
65 File srcDir = new File(baseDir, "src");
67 File testDir = new File(baseDir, "test");
70 File xooFile = new File(srcDir, "sample.xoo");
71 FileUtils.write(xooFile, "foo");
73 File xooTestFile = new File(testDir, "sampleTest.xoo");
74 FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped");
76 File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
77 FileUtils.write(xooTestExecutionFile, "skipped::::SKIPPED:UNIT\n" +
78 "failure:2:Failure::FAILURE:UNIT\n" +
79 "error:2:Error:The stack:ERROR:UNIT\n" +
80 "success:4:::OK:INTEGRATION");
82 TaskResult result = tester.newTask()
83 .properties(ImmutableMap.<String, String>builder()
84 .put("sonar.task", "scan")
85 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
86 .put("sonar.projectKey", "com.foo.project")
87 .put("sonar.projectName", "Foo Project")
88 .put("sonar.projectVersion", "1.0-SNAPSHOT")
89 .put("sonar.projectDescription", "Description of Foo Project")
90 .put("sonar.sources", "src")
91 .put("sonar.tests", "test")
95 InputFile file = result.inputFile("test/sampleTest.xoo");
96 org.sonar.scanner.protocol.output.ScannerReport.Test success = result.firstTestExecutionForName(file, "success");
97 assertThat(success.getDurationInMs()).isEqualTo(4);
98 assertThat(success.getStatus()).isEqualTo(TestStatus.OK);
100 org.sonar.scanner.protocol.output.ScannerReport.Test error = result.firstTestExecutionForName(file, "error");
101 assertThat(error.getDurationInMs()).isEqualTo(2);
102 assertThat(error.getStatus()).isEqualTo(TestStatus.ERROR);
103 assertThat(error.getMsg()).isEqualTo("Error");
104 assertThat(error.getStacktrace()).isEqualTo("The stack");
108 public void singleReport() throws IOException {
110 File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-test-exec");
112 TaskResult result = tester
113 .newScanTask(new File(projectDir, "sonar-project.properties"))
114 .property("sonar.testExecutionReportPaths", "unittest.xml")
117 InputFile testFile = result.inputFile("testx/ClassOneTest.xoo");
118 ScannerReport.Test success = result.firstTestExecutionForName(testFile, "test1");
119 assertThat(success.getDurationInMs()).isEqualTo(5);
120 assertThat(success.getStatus()).isEqualTo(TestStatus.OK);
122 ScannerReport.Test skipped = result.firstTestExecutionForName(testFile, "test2");
123 assertThat(skipped.getDurationInMs()).isEqualTo(500);
124 assertThat(skipped.getStatus()).isEqualTo(TestStatus.SKIPPED);
125 assertThat(skipped.getMsg()).isEqualTo("short message");
126 assertThat(skipped.getStacktrace()).isEqualTo("other");
128 ScannerReport.Test failed = result.firstTestExecutionForName(testFile, "test3");
129 assertThat(failed.getDurationInMs()).isEqualTo(100);
130 assertThat(failed.getStatus()).isEqualTo(TestStatus.FAILURE);
131 assertThat(failed.getMsg()).isEqualTo("short");
132 assertThat(failed.getStacktrace()).isEqualTo("stacktrace");
134 ScannerReport.Test error = result.firstTestExecutionForName(testFile, "test4");
135 assertThat(error.getDurationInMs()).isEqualTo(500);
136 assertThat(error.getStatus()).isEqualTo(TestStatus.ERROR);
137 assertThat(error.getMsg()).isEqualTo("short");
138 assertThat(error.getStacktrace()).isEqualTo("stacktrace");
140 assertThat(result.allMeasures().get(testFile.key())).extracting("metricKey", "intValue.value", "longValue.value")
142 tuple(CoreMetrics.TESTS_KEY, 3, 0L),
143 tuple(CoreMetrics.SKIPPED_TESTS_KEY, 1, 0L),
144 tuple(CoreMetrics.TEST_ERRORS_KEY, 1, 0L),
145 tuple(CoreMetrics.TEST_EXECUTION_TIME_KEY, 0, 1105L),
146 tuple(CoreMetrics.TEST_FAILURES_KEY, 1, 0L));
150 public void twoReports() throws IOException {
152 File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-test-exec");
154 TaskResult result = tester
155 .newScanTask(new File(projectDir, "sonar-project.properties"))
156 .property("sonar.testExecutionReportPaths", "unittest.xml,unittest2.xml")
159 InputFile testFile = result.inputFile("testx/ClassOneTest.xoo");
160 ScannerReport.Test success = result.firstTestExecutionForName(testFile, "test1");
161 assertThat(success.getDurationInMs()).isEqualTo(5);
162 assertThat(success.getStatus()).isEqualTo(TestStatus.OK);
164 ScannerReport.Test success2 = result.firstTestExecutionForName(testFile, "test1b");
165 assertThat(success2.getDurationInMs()).isEqualTo(5);
166 assertThat(success2.getStatus()).isEqualTo(TestStatus.OK);
168 ScannerReport.Test skipped = result.firstTestExecutionForName(testFile, "test2");
169 assertThat(skipped.getDurationInMs()).isEqualTo(500);
170 assertThat(skipped.getStatus()).isEqualTo(TestStatus.SKIPPED);
171 assertThat(skipped.getMsg()).isEqualTo("short message");
172 assertThat(skipped.getStacktrace()).isEqualTo("other");
174 ScannerReport.Test failed = result.firstTestExecutionForName(testFile, "test3");
175 assertThat(failed.getDurationInMs()).isEqualTo(100);
176 assertThat(failed.getStatus()).isEqualTo(TestStatus.FAILURE);
177 assertThat(failed.getMsg()).isEqualTo("short");
178 assertThat(failed.getStacktrace()).isEqualTo("stacktrace");
180 ScannerReport.Test error = result.firstTestExecutionForName(testFile, "test4");
181 assertThat(error.getDurationInMs()).isEqualTo(500);
182 assertThat(error.getStatus()).isEqualTo(TestStatus.ERROR);
183 assertThat(error.getMsg()).isEqualTo("short");
184 assertThat(error.getStacktrace()).isEqualTo("stacktrace");
186 assertThat(result.allMeasures().get(testFile.key())).extracting("metricKey", "intValue.value", "longValue.value")
188 tuple(CoreMetrics.TESTS_KEY, 4, 0L),
189 tuple(CoreMetrics.SKIPPED_TESTS_KEY, 2, 0L),
190 tuple(CoreMetrics.TEST_ERRORS_KEY, 1, 0L),
191 tuple(CoreMetrics.TEST_EXECUTION_TIME_KEY, 0, 1610L),
192 tuple(CoreMetrics.TEST_FAILURES_KEY, 1, 0L));