Skip to content
Permalink
Browse files

[FIXED JENKINS-8520] integrated saaadhu's change from saaadhu@717b5d1

  • Loading branch information
kohsuke
kohsuke committed Jun 17, 2011
1 parent 29cca4d commit 8e8ca6b4cf8339c00d1726d9569b7c85cd9341ca
@@ -68,7 +68,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

boolean result = true;
try {
listener.getLogger().println("Processing tests results in file " + testResultsFile);
listener.getLogger().println("Processing tests results in file(s) " + testResultsFile);
MSTestTransformer transformer = new MSTestTransformer(testResultsFile, new MSTestReportConverter(), listener);
result = build.getWorkspace().act(transformer);

@@ -6,6 +6,8 @@
import java.io.InputStream;
import java.io.Serializable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
@@ -14,21 +16,32 @@
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
* Transforms a MSTest report into a JUnit report.
*/
public class MSTestReportConverter implements Serializable {


private static final String JUNIT_OUTPUT_FILE_STR = "TEST-mstest.xml";
private static final String ILLEGAL_FILE_CHARS_REGEX = "[\\*/:<>\\?\\|\\\\\";]+";
public static final String JUNIT_FILE_POSTFIX = ".xml";
public static final String JUNIT_FILE_PREFIX = "TEST-";
private static final String TEMP_JUNIT_FILE_STR = "temp-junit.xml";
public static final String MSTEST_TO_JUNIT_XSLFILE_STR = "mstest-to-junit.xsl";

private transient boolean xslIsInitialized;
private transient Transformer mstestTransformer;

private transient Transformer writerTransformer;
private transient DocumentBuilder xmlDocumentBuilder;

private transient int fileCount;

/**
* Transform the MSTest TRX file into a junit XML file in the output path
*
@@ -44,14 +57,16 @@ public void transform(InputStream mstestFileStream, File junitOutputPath) throws

initialize();

File junitTargetFile = new File(junitOutputPath, JUNIT_OUTPUT_FILE_STR);
File junitTargetFile = new File(junitOutputPath, TEMP_JUNIT_FILE_STR);
FileOutputStream fileOutputStream = new FileOutputStream(junitTargetFile);
try {
mstestTransformer.transform(new StreamSource(mstestFileStream), new StreamResult(fileOutputStream));
} finally {
fileOutputStream.close();
}

splitJUnitFile(junitTargetFile, junitOutputPath);
junitTargetFile.delete();
}

private void initialize() throws TransformerFactoryConfigurationError, TransformerConfigurationException,
@@ -60,8 +75,43 @@ private void initialize() throws TransformerFactoryConfigurationError, Transform
TransformerFactory transformerFactory = TransformerFactory.newInstance();
mstestTransformer = transformerFactory.newTransformer(new StreamSource(this.getClass().getResourceAsStream(MSTEST_TO_JUNIT_XSLFILE_STR)));

writerTransformer = transformerFactory.newTransformer();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
xmlDocumentBuilder = factory.newDocumentBuilder();

xslIsInitialized = true;
}
}

/**
* Splits the junit file into several junit files in the output path
*
* @param junitFile report containing one or more junit test suite tags
* @param junitOutputPath the path to put all junit files
* @throws IOException
* @throws SAXException
* @throws TransformerException
*/
private void splitJUnitFile(File junitFile, File junitOutputPath) throws SAXException, IOException,
TransformerException {
Document document = xmlDocumentBuilder.parse(junitFile);

NodeList elementsByTagName = ((Element) document.getElementsByTagName("testsuites").item(0)).getElementsByTagName("testsuite");
for (int i = 0; i < elementsByTagName.getLength(); i++) {
Element element = (Element) elementsByTagName.item(i);
DOMSource source = new DOMSource(element);

String filename = JUNIT_FILE_PREFIX + (fileCount++) + JUNIT_FILE_POSTFIX;
File junitOutputFile = new File(junitOutputPath, filename);
FileOutputStream fileOutputStream = new FileOutputStream(junitOutputFile);
try {
StreamResult result = new StreamResult(fileOutputStream);
writerTransformer.transform(source, result);
} finally {
fileOutputStream.close();
}
}
}
}

@@ -4,6 +4,7 @@
import hudson.model.BuildListener;
import hudson.remoting.VirtualChannel;
import hudson.util.IOException2;
import hudson.Util;

import java.io.File;
import java.io.FileInputStream;
@@ -15,6 +16,8 @@
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;

/**
* Class responsible for transforming the MSTest build report into a JUnit file and then
@@ -42,38 +45,59 @@ public MSTestTransformer(String testResults, MSTestReportConverter unitReportTra

/** {@inheritDoc} */
public Boolean invoke(File ws, VirtualChannel channel) throws IOException {
Boolean retValue = Boolean.TRUE;
String[] mstestFiles = findMSTestReports(ws);

if (mstestFiles.length == 0) {
listener.fatalError("No MSTest TRX test report files were found. Configuration error?");
return Boolean.FALSE;
}


File junitOutputPath = new File(ws, JUNIT_REPORTS_PATH);
junitOutputPath.mkdirs();

FileInputStream fileStream = null;
try{
fileStream = new FileInputStream(new File(ws, testResultsFile));
}
catch(FileNotFoundException e)
for (String mstestFile : mstestFiles)
{
listener.fatalError("No MSTest TRX test report files were found. Configuration error?");
return Boolean.FALSE;
}

try {
unitReportTransformer.transform(fileStream, junitOutputPath);
} catch (TransformerException te) {
throw new IOException2(
listener.getLogger().println(mstestFile);
FileInputStream fileStream = new FileInputStream(new File(ws, mstestFile));
try {
unitReportTransformer.transform(fileStream, junitOutputPath);
} catch (TransformerException te) {
throw new IOException2(
"Could not transform the MSTest report. Please report this issue to the plugin author", te);
} catch (SAXException se) {
throw new IOException2(
} catch (SAXException se) {
throw new IOException2(
"Could not transform the MSTest report. Please report this issue to the plugin author", se);
} catch (ParserConfigurationException pce) {
throw new IOException2(
} catch (ParserConfigurationException pce) {
throw new IOException2(
"Could not initalize the XML parser. Please report this issue to the plugin author", pce);
} finally {
if(fileStream != null)
fileStream.close();
} finally {
if(fileStream != null)
fileStream.close();
}
}

return retValue;
return true;
}

/**
* Returns all MSTest report files matching the pattern given in configuration
*
* @param workspacePath Workspace Path
* @return an array of strings containing filenames of MSTest report files
*/
private String[] findMSTestReports(File workspacePath) {
if (workspacePath == null)
return new String[]{};

FileSet fs = Util.createFileSet(workspacePath, testResultsFile);
DirectoryScanner ds = fs.getDirectoryScanner();

String[] mstestFiles = ds.getIncludedFiles();
if (mstestFiles.length == 0) {
listener.fatalError("No MSTest test report files were found.");
}

return mstestFiles;
}
}

0 comments on commit 8e8ca6b

Please sign in to comment.
You can’t perform that action at this time.