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.

GenericTestExecutionReportParserTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.genericcoverage;
  21. import java.io.File;
  22. import java.nio.charset.StandardCharsets;
  23. import org.apache.commons.io.FileUtils;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.TemporaryFolder;
  28. import org.sonar.api.batch.fs.InputFile;
  29. import org.sonar.api.test.MutableTestCase;
  30. import org.sonar.api.test.MutableTestPlan;
  31. import org.sonar.api.utils.MessageException;
  32. import org.sonar.scanner.deprecated.test.TestPlanBuilder;
  33. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  34. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  35. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.ArgumentMatchers.anyLong;
  39. import static org.mockito.ArgumentMatchers.anyString;
  40. import static org.mockito.ArgumentMatchers.eq;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. public class GenericTestExecutionReportParserTest {
  45. @Rule
  46. public TemporaryFolder temp = new TemporaryFolder();
  47. private TestPlanBuilder testPlanBuilder;
  48. private DefaultInputFile fileWithBranches;
  49. private DefaultInputFile emptyFile;
  50. private SensorContextTester context;
  51. private MutableTestPlan testPlan;
  52. @Before
  53. public void before() {
  54. context = SensorContextTester.create(new File(""));
  55. fileWithBranches = setupFile("src/main/java/com/example/ClassWithBranches.java");
  56. emptyFile = setupFile("src/main/java/com/example/EmptyClass.java");
  57. testPlanBuilder = mock(TestPlanBuilder.class);
  58. MutableTestCase testCase = mockMutableTestCase();
  59. testPlan = mockMutableTestPlan(testCase);
  60. when(testPlanBuilder.loadPerspective(eq(MutableTestPlan.class), any(InputFile.class))).thenReturn(testPlan);
  61. }
  62. @Test
  63. public void ut_empty_file() throws Exception {
  64. addFileToFs(emptyFile);
  65. GenericTestExecutionReportParser parser = parseReportFile("unittest.xml");
  66. assertThat(parser.numberOfMatchedFiles()).isEqualTo(1);
  67. assertThat(parser.numberOfUnknownFiles()).isEqualTo(1);
  68. assertThat(parser.firstUnknownFiles()).hasSize(1);
  69. }
  70. @Test
  71. public void file_with_unittests() throws Exception {
  72. addFileToFs(fileWithBranches);
  73. GenericTestExecutionReportParser parser = parseReportFile("unittest2.xml");
  74. assertThat(parser.numberOfMatchedFiles()).isEqualTo(1);
  75. verify(testPlan).addTestCase("test1");
  76. verify(testPlan).addTestCase("test2");
  77. verify(testPlan).addTestCase("test3");
  78. }
  79. @Test(expected = MessageException.class)
  80. public void unittest_invalid_root_node_name() throws Exception {
  81. parseUnitTestReport("<mycoverage version=\"1\"></mycoverage>");
  82. }
  83. @Test(expected = MessageException.class)
  84. public void unittest_invalid_report_version() throws Exception {
  85. parseUnitTestReport("<unitTest version=\"2\"></unitTest>");
  86. }
  87. @Test(expected = MessageException.class)
  88. public void unittest_duration_in_testCase_should_be_a_number() throws Exception {
  89. addFileToFs(setupFile("file1"));
  90. parseUnitTestReport("<unitTest version=\"1\"><file path=\"file1\">"
  91. + "<testCase name=\"test1\" duration=\"aaa\"/></file></unitTest>");
  92. }
  93. @Test(expected = MessageException.class)
  94. public void unittest_failure_should_have_a_message() throws Exception {
  95. addFileToFs(setupFile("file1"));
  96. parseUnitTestReport("<unitTest version=\"1\"><file path=\"file1\">"
  97. + "<testCase name=\"test1\" duration=\"2\"><failure /></testCase></file></unitTest>");
  98. }
  99. @Test(expected = MessageException.class)
  100. public void unittest_error_should_have_a_message() throws Exception {
  101. addFileToFs(setupFile("file1"));
  102. parseUnitTestReport("<unitTest version=\"1\"><file path=\"file1\">"
  103. + "<testCase name=\"test1\" duration=\"2\"><error /></testCase></file></unitTest>");
  104. }
  105. @Test(expected = MessageException.class)
  106. public void unittest_skipped_should_have_a_message() throws Exception {
  107. addFileToFs(setupFile("file1"));
  108. parseUnitTestReport("<unitTest version=\"1\"><file path=\"file1\">"
  109. + "<testCase name=\"test1\" duration=\"2\"><skipped notmessage=\"\"/></testCase></file></unitTest>");
  110. }
  111. @Test(expected = MessageException.class)
  112. public void unittest_duration_in_testCase_should_not_be_negative() throws Exception {
  113. addFileToFs(setupFile("file1"));
  114. parseUnitTestReport("<unitTest version=\"1\"><file path=\"file1\">"
  115. + "<testCase name=\"test1\" duration=\"-5\"/></file></unitTest>");
  116. }
  117. private void addFileToFs(DefaultInputFile inputFile) {
  118. context.fileSystem().add(inputFile);
  119. }
  120. private GenericTestExecutionReportParser parseUnitTestReport(String string) throws Exception {
  121. GenericTestExecutionReportParser parser = new GenericTestExecutionReportParser(testPlanBuilder);
  122. File report = temp.newFile();
  123. FileUtils.write(report, string, StandardCharsets.UTF_8);
  124. parser.parse(report, context);
  125. return parser;
  126. }
  127. private GenericTestExecutionReportParser parseReportFile(String reportLocation) throws Exception {
  128. GenericTestExecutionReportParser parser = new GenericTestExecutionReportParser(testPlanBuilder);
  129. parser.parse(new File(this.getClass().getResource(reportLocation).toURI()), context);
  130. return parser;
  131. }
  132. private DefaultInputFile setupFile(String path) {
  133. return new TestInputFileBuilder(context.module().key(), path)
  134. .setLanguage("bla")
  135. .setType(InputFile.Type.TEST)
  136. .initMetadata("1\n2\n3\n4\n5\n6")
  137. .build();
  138. }
  139. private MutableTestPlan mockMutableTestPlan(MutableTestCase testCase) {
  140. MutableTestPlan testPlan = mock(MutableTestPlan.class);
  141. when(testPlan.addTestCase(anyString())).thenReturn(testCase);
  142. return testPlan;
  143. }
  144. private MutableTestCase mockMutableTestCase() {
  145. MutableTestCase testCase = mock(MutableTestCase.class);
  146. when(testCase.setDurationInMs(anyLong())).thenReturn(testCase);
  147. when(testCase.setStatus(any(org.sonar.api.test.TestCase.Status.class))).thenReturn(testCase);
  148. when(testCase.setMessage(anyString())).thenReturn(testCase);
  149. when(testCase.setStackTrace(anyString())).thenReturn(testCase);
  150. when(testCase.setType(anyString())).thenReturn(testCase);
  151. return testCase;
  152. }
  153. }