]> source.dussan.org Git - sonarqube.git/blob
462d3d1f7eae0671288ecf9a5489048b375c17a9
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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
22 import com.google.common.collect.ImmutableMap;
23 import java.io.File;
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;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.tuple;
40
41 public class GenericTestExecutionMediumTest {
42
43   @org.junit.Rule
44   public TemporaryFolder temp = new TemporaryFolder();
45
46   public ScannerMediumTester tester = ScannerMediumTester.builder()
47     .registerPlugin("xoo", new XooPlugin())
48     .addDefaultQProfile("xoo", "Sonar Way")
49     .build();
50
51   @Before
52   public void prepare() {
53     tester.start();
54   }
55
56   @After
57   public void stop() {
58     tester.stop();
59   }
60
61   @Test
62   public void unitTests() throws IOException {
63
64     File baseDir = temp.getRoot();
65     File srcDir = new File(baseDir, "src");
66     srcDir.mkdir();
67     File testDir = new File(baseDir, "test");
68     testDir.mkdir();
69
70     File xooFile = new File(srcDir, "sample.xoo");
71     FileUtils.write(xooFile, "foo");
72
73     File xooTestFile = new File(testDir, "sampleTest.xoo");
74     FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped");
75
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");
81
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")
92         .build())
93       .start();
94
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);
99
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");
105   }
106
107   @Test
108   public void singleReport() throws IOException {
109
110     File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-test-exec");
111
112     TaskResult result = tester
113       .newScanTask(new File(projectDir, "sonar-project.properties"))
114       .property("sonar.testExecutionReportPaths", "unittest.xml")
115       .start();
116
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);
121
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");
127
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");
133
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");
139
140     assertThat(result.allMeasures().get(testFile.key())).extracting("metricKey", "intValue.value", "longValue.value")
141       .containsOnly(
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));
147   }
148
149   @Test
150   public void twoReports() throws IOException {
151
152     File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-test-exec");
153
154     TaskResult result = tester
155       .newScanTask(new File(projectDir, "sonar-project.properties"))
156       .property("sonar.testExecutionReportPaths", "unittest.xml,unittest2.xml")
157       .start();
158
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);
163
164     ScannerReport.Test success2 = result.firstTestExecutionForName(testFile, "test1b");
165     assertThat(success2.getDurationInMs()).isEqualTo(5);
166     assertThat(success2.getStatus()).isEqualTo(TestStatus.OK);
167
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");
173
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");
179
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");
185
186     assertThat(result.allMeasures().get(testFile.key())).extracting("metricKey", "intValue.value", "longValue.value")
187       .containsOnly(
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));
193   }
194
195 }