private void mergeAndUploadTestResult(GoPublisher publisher, List<File> allFiles) { if (allFiles.size() > 0) { File tempFolder = null; try { tempFolder = FileUtil.createTempFolder(); File testResultSource = new File(tempFolder, MERGED_TEST_RESULT_FOLDER); testResultSource.mkdirs(); UnitTestReportGenerator generator = new UnitTestReportGenerator(publisher, testResultSource); generator.generate(allFiles.toArray(new File[allFiles.size()]), "testoutput"); publisher.upload(testResultSource, "testoutput"); } finally { if (tempFolder != null) { FileUtils.deleteQuietly(tempFolder); } } } else { String message = "No files were found in the Test Results folders"; publisher.taggedConsumeLineWithPrefix(GoPublisher.PUBLISH_ERR, message); LOG.warn(message); } }
private Properties extractProperties(File fileSendToServer) { final Properties properties = new Properties(); addProperty(fileSendToServer, "tests_total_count", TOTAL_TEST_COUNT); addProperty(fileSendToServer, "tests_failed_count", FAILED_TEST_COUNT); addProperty(fileSendToServer, "tests_ignored_count", IGNORED_TEST_COUNT); addProperty(fileSendToServer, "tests_total_duration", TEST_TIME); return properties; }
public Properties generate(File[] allTestFiles, String uploadDestPath) { FileOutputStream transformedHtml = null; File mergedResults = new File(folderToUpload.getAbsolutePath() + FileUtil.fileseparator() + TEST_RESULTS_FILE); File mergedResource = null; FileInputStream mergedFileStream = null; try { mergedResource = mergeAllTestResultToSingleFile(allTestFiles); transformedHtml = new FileOutputStream(mergedResults); try { mergedFileStream = new FileInputStream(mergedResource); Source xmlSource = new StreamSource(mergedFileStream); StreamResult result = new StreamResult(transformedHtml); templates.newTransformer().transform(xmlSource, result); } catch (Exception e) { publisher.reportErrorMessage("Unable to publish test properties. Error was " + e.getMessage(), e); } extractProperties(mergedResults); publisher.upload(mergedResults, uploadDestPath); return null; } catch (Exception e) { publisher.reportErrorMessage("Unable to publish test properties. Error was " + e.getMessage(), e); } finally { IOUtils.closeQuietly(mergedFileStream); IOUtils.closeQuietly(transformedHtml); if (mergedResource != null) { mergedResource.delete(); } } return new Properties(); }
private void pumpFileContentIfValid(PrintStream out, File testFile) throws IOException { if (!isValidXml(testFile)) { return; } pumpFileContent(testFile, out); }
@Test public void shouldNotGenerateAnyReportIfNoTestResultsWereFound() throws IOException, ArtifactPublishingException { generator.generate(testFolder.listFiles(), "testoutput"); expectZeroedProperties(); }
@Before public void setUp() throws IOException { temporaryFolder.create(); testFolder = temporaryFolder.newFolder(); publisher = mock(DefaultGoPublisher.class); generator = new UnitTestReportGenerator(publisher, testFolder); }
private File mergeAllTestResultToSingleFile(File[] allTestFiles) throws IOException { FileOutputStream mergedResourcesStream = null; try { File mergedResource = TestFileUtil.createUniqueTempFile("mergedFile.xml"); mergedResourcesStream = new FileOutputStream(mergedResource); merge(allTestFiles, mergedResourcesStream); return mergedResource; } finally { IOUtils.closeQuietly(mergedResourcesStream); } }
private boolean isValidXml(File file) { try { boolean isTestFile = nodeExists(file, "//test-results") || nodeExists(file, "//testsuite"); if (!isTestFile) { publisher.consumeLine(MessageFormat.format("Ignoring file {0} - it is not a recognised test file.", file.getName())); } return isTestFile; } catch (Exception e) { publisher.consumeLine(MessageFormat.format("The file {0} could not be parsed. It seems to be invalid.", file.getName())); return false; } }
@Test public void shouldGenerateReportForNUnit() throws IOException, ArtifactPublishingException { copyAndClose(source("TestResult.xml"), target("test-result.xml")); final Properties properties = generator.generate(testFolder.listFiles(), "testoutput"); assertThat(testFolder.listFiles().length, is(2)); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "206")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "NaN")); }
@Test public void shouldStillUploadResultsIfReportIsIllegalBug2319() throws IOException, ArtifactPublishingException { copyAndClose(source("xml_samples/Coverage.xml"), target("Coverage.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).consumeLine("Ignoring file Coverage.xml - it is not a recognised test file."); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "0.000")); }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File workingDirectory = buildSession.resolveRelativeDir(command.getWorkingDirectory()); String uploadPath = command.getStringArg("uploadPath"); String[] sources = command.getArrayArg("srcs"); ArrayList<File> allFiles = findMatchedSourceFiles(buildSession, workingDirectory, sources); if (allFiles.size() > 0) { File tempFolder = null; try { tempFolder = FileUtil.createTempFolder(); File testResultSource = new File(tempFolder, "result"); testResultSource.mkdirs(); UnitTestReportGenerator generator = new UnitTestReportGenerator(buildSession.getPublisher(), testResultSource); generator.generate(allFiles.toArray(new File[allFiles.size()]), uploadPath); } finally { if (tempFolder != null) { FileUtils.deleteQuietly(tempFolder); } } } else { String message = "No files were found in the Test Results folders"; buildSession.printlnWithPrefix(message); LOG.warn(message); } return true; }
@Test public void shouldGenerateReportForNUnitXmlWithByteOrderMark() throws IOException, ArtifactPublishingException { copyAndClose(source("NunitTestResultWithByteOrderMark.xml"), target("test-result.xml")); generator.generate(testFolder.listFiles(), "testoutput"); assertThat(testFolder.listFiles().length, is(2)); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "18")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "1.570")); }
@Test public void shouldNotGenerateAnyReportIfTestReportIsInvalid() throws IOException, ArtifactPublishingException { copyAndClose(source("InvalidTestResult.xml"), target("Invalid.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).consumeLine("Ignoring file Invalid.xml - it is not a recognised test file."); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "0.000")); verify(publisher).upload(any(File.class), any(String.class)); }
@Test public void shouldNotGenerateAnyReportIfTestResultIsEmpty() throws IOException, ArtifactPublishingException { copyAndClose(source("empty.xml"), target("empty.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).consumeLine("Ignoring file empty.xml - it is not a recognised test file."); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "0.000")); verify(publisher).upload(any(File.class), any(String.class)); }
@Test public void shouldGenerateReportForJUnitAlso() throws IOException, ArtifactPublishingException { copyAndClose(source("SerializableProjectConfigUtilTest.xml"), target("AgentTest.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "1")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "0.456")); }
@Test public void shouldGenerateReportForXmlFilesRecursivelyInAFolder() throws ArtifactPublishingException, IOException { File reports = new File(testFolder.getAbsoluteFile(), "reports"); reports.mkdir(); File module = new File(reports, "module"); module.mkdir(); copyAndClose(source("xml_samples/Coverage.xml"), target("reports/module/Coverage.xml")); copyAndClose(source("xml_samples/TestResult.xml"), target("reports/TestResult.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).consumeLine("Ignoring file Coverage.xml - it is not a recognised test file."); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "204")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "6")); verify(publisher).setProperty(new Property(TEST_TIME, "80.231")); }
@Test public void shouldGenerateReportForJUnitWithMultipleFiles() throws IOException, ArtifactPublishingException { copyAndClose(source("UnitTestReportGeneratorTest.xml"), target("UnitTestReportGeneratorTest.xml")); copyAndClose(source("SerializableProjectConfigUtilTest.xml"), target("SerializableProjectConfigUtilTest.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "5")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "3")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(TEST_TIME, "1.286")); }
@Test public void shouldGenerateReportForNUnitGivenMutipleInputFiles() throws IOException, ArtifactPublishingException { copyAndClose(source("TestReport-Integration.xml"), target("test-result1.xml")); copyAndClose(source("TestReport-Unit.xml"), target("test-result2.xml")); generator.generate(testFolder.listFiles(), "testoutput"); verify(publisher).upload(any(File.class), any(String.class)); verify(publisher).setProperty(new Property(TOTAL_TEST_COUNT, "2762")); verify(publisher).setProperty(new Property(FAILED_TEST_COUNT, "0")); verify(publisher).setProperty(new Property(IGNORED_TEST_COUNT, "120")); verify(publisher).setProperty(new Property(TEST_TIME, "221.766")); }