PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/bamboo/plugin/dotnet/tests/nunit/NUnitTestReportCollectorTest.java

https://bitbucket.org/atlassian/bamboo-dotnet-plugin/
Java | 165 lines | 135 code | 27 blank | 3 comment | 1 complexity | 5dcb7dca67d09383b3b82f201c6d7119 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.bamboo.plugin.dotnet.tests.nunit;
  2. import com.atlassian.bamboo.build.test.TestCollectionResult;
  3. import com.atlassian.bamboo.results.tests.TestResults;
  4. import com.atlassian.bamboo.utils.BambooTestUtils;
  5. import com.atlassian.bamboo.utils.FileVisitor;
  6. import org.apache.commons.io.FilenameUtils;
  7. import org.apache.commons.lang3.SystemUtils;
  8. import org.hamcrest.Matchers;
  9. import org.jetbrains.annotations.NotNull;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import java.io.File;
  13. import java.util.Collections;
  14. import java.util.HashSet;
  15. import java.util.Set;
  16. import java.util.concurrent.ExecutorService;
  17. import java.util.concurrent.Executors;
  18. import java.util.concurrent.TimeUnit;
  19. import java.util.concurrent.atomic.AtomicInteger;
  20. import static com.atlassian.bamboo.testutils.matchers.EntityMatchers.testCollectionResult;
  21. import static org.hamcrest.MatcherAssert.assertThat;
  22. import static org.hamcrest.Matchers.is;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.fail;
  25. import static org.junit.Assume.assumeThat;
  26. /**
  27. * This test needs to be run with JDK8 (in order to access public property of private inner class implementing a public interface)
  28. */
  29. public class NUnitTestReportCollectorTest
  30. {
  31. private NUnitTestReportCollector testReportCollector;
  32. @Before
  33. public void setUp() throws Exception
  34. {
  35. testReportCollector = new NUnitTestReportCollector();
  36. }
  37. @Test
  38. public void testBSP_4922_A() throws Exception
  39. {
  40. assumeThat(SystemUtils.IS_JAVA_1_8, is(true));
  41. final TestCollectionResult result = collect("BSP-4922.Enroll.ThickFacade.Tests.nunit-results.xml");
  42. assertThat(result,
  43. testCollectionResult()
  44. .failedTestResults(0)
  45. .skippedTestResults(0)
  46. .successfulTestResults(13)
  47. .build());
  48. }
  49. @Test
  50. public void testBSP_4922_B() throws Exception
  51. {
  52. assumeThat(SystemUtils.IS_JAVA_1_8, is(true));
  53. final TestCollectionResult result = collect("BSP-4922.IDGrid.Tests.nunit-results.xml");
  54. assertThat(result,
  55. testCollectionResult()
  56. .failedTestResults(0)
  57. .skippedTestResults(0)
  58. .successfulTestResults(7)
  59. .build());
  60. }
  61. @Test
  62. public void testBSP_4960() throws Exception
  63. {
  64. assumeThat(SystemUtils.IS_JAVA_1_8, is(true));
  65. final TestCollectionResult result = collect("BSP-4960.testresult_with_no_results.xml");
  66. assertThat(result,
  67. testCollectionResult()
  68. .failedTestResults(0)
  69. .skippedTestResults(0)
  70. .successfulTestResults(0)
  71. .build());
  72. }
  73. @Test
  74. public void testMultiThreadedCollation() throws Exception
  75. {
  76. final AtomicInteger numberOfFilesFound = new AtomicInteger(0);
  77. final Set<TestResults> failedTestResults = Collections.synchronizedSet(new HashSet<TestResults>());
  78. final Set<TestResults> successfulTestResults = Collections.synchronizedSet(new HashSet<TestResults>());
  79. File rootDirectory = BambooTestUtils.getFileFromResourceDirectory(this, "empty_file.xml").getParentFile();
  80. final ExecutorService executorService = Executors.newFixedThreadPool(8);
  81. try
  82. {
  83. FileVisitor fileVisitor = new FileVisitor(rootDirectory)
  84. {
  85. @Override
  86. public void visitFile(final File file)
  87. {
  88. final String fileName = file.getName();
  89. if (FilenameUtils.isExtension(fileName, testReportCollector.getSupportedFileExtensions()))
  90. {
  91. executorService.submit(new Runnable()
  92. {
  93. @Override
  94. public void run()
  95. {
  96. try
  97. {
  98. numberOfFilesFound.incrementAndGet();
  99. TestCollectionResult result = testReportCollector.collect(file);
  100. failedTestResults.addAll(result.getFailedTestResults());
  101. successfulTestResults.addAll(result.getSuccessfulTestResults());
  102. }
  103. catch (Exception e)
  104. {
  105. fail();
  106. }
  107. }
  108. });
  109. }
  110. }
  111. };
  112. fileVisitor.visitFilesThatMatch("BSP*.xml");
  113. }
  114. finally
  115. {
  116. executorService.shutdown();
  117. executorService.awaitTermination(2, TimeUnit.MINUTES);
  118. }
  119. assertEquals(20, successfulTestResults.size());
  120. assertEquals(0, failedTestResults.size());
  121. }
  122. @Test
  123. public void testNUnitOrg25Sample() throws Exception
  124. {
  125. assumeThat(SystemUtils.IS_JAVA_1_8, is(true));
  126. final TestCollectionResult result = collect("nunit_org_2_5_sample.xml");
  127. assertThat(result,
  128. testCollectionResult()
  129. .failedTestResults(Matchers.<TestResults>iterableWithSize(3))
  130. .skippedTestResults(Matchers.<TestResults>iterableWithSize(7))
  131. .successfulTestResults(Matchers.<TestResults>iterableWithSize(18))
  132. .build());
  133. }
  134. private TestCollectionResult collect(@NotNull String filename) throws Exception
  135. {
  136. return testReportCollector.collect(BambooTestUtils.getFileFromResourceDirectory(this, filename));
  137. }
  138. }