Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Choose the XSL file based on the report version
Starting with cpptest 9.0, the report file format changed slightly,
fortunately the tool version can be found at the same address xml-wise.

This implementation uses a stream reader to avoid completely reading the
report twice: once for the version, and once for the conversion. This is
convenient since the tool version is in the very first element.

The file `cpptestunit-9.0-to-junit-1.0.xsl` isn't actually part of this
commit.

refs JENKINS-18727
  • Loading branch information
Dridi Boukelmoune committed Feb 11, 2014
1 parent d22a006 commit e607aff
Show file tree
Hide file tree
Showing 4 changed files with 6,335 additions and 6 deletions.
Expand Up @@ -4,28 +4,40 @@
import com.thalesgroup.dtkit.metrics.model.InputMetricXSL;
import com.thalesgroup.dtkit.metrics.model.InputType;
import com.thalesgroup.dtkit.metrics.model.OutputMetric;
import com.thalesgroup.dtkit.util.validator.ValidationException;

import hudson.util.IOUtils;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import java.io.File;
import java.io.IOException;
import java.io.FileReader;


public class CpptestInputMetric extends InputMetricXSL {

private final XMLInputFactory factory = XMLInputFactory.newInstance();

@Override
public InputType getToolType() {
return InputType.TEST;
}

@Override
public String getToolVersion() {
return "7.x";
}

@Override
public String getToolName() {
return "CppTest";
}

@Override
public String getXslName() {
return "cpptestunit-2.0-to-junit-1.0.xsl";
String version = getToolVersion();
if (VersionComparator.INSTANCE.compare(version, "9.0") < 0) {
return "cpptestunit-2.0-to-junit-1.0.xsl";
}
return "cpptestunit-9.0-to-junit-1.0.xsl";
}

@Override
Expand All @@ -37,4 +49,52 @@ public OutputMetric getOutputFormatType() {
return JUnitModel.OUTPUT_JUNIT_1_0;
}

@Override
public boolean validateInputFile(File inputXMLFile) throws ValidationException {
if (! super.validateInputFile(inputXMLFile)) {
return false;
}

String version = null;
FileReader fileReader = null;
XMLStreamReader streamReader = null;
try {
fileReader = new FileReader(inputXMLFile);
streamReader = factory.createXMLStreamReader(fileReader);

while (streamReader.hasNext() && version == null) {
streamReader.next();
if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
&& streamReader.getLocalName().equals("ResultsSession")) {

version = streamReader.getAttributeValue(null, "toolVer");
break;
}
}
}
catch (IOException e) {
throw new ValidationException(e);
}
catch (XMLStreamException e) {
throw new ValidationException(e);
}
finally {
closeStreamReaderQuietly(streamReader);
IOUtils.closeQuietly(fileReader);
}

this.setToolVersion(version);
return version != null;
}

private void closeStreamReaderQuietly(XMLStreamReader streamReader) {
try {
if (streamReader != null) {
streamReader.close();
}
}
catch (XMLStreamException e) {
// ignoring on purpose
}
}
}
Expand Up @@ -33,4 +33,10 @@ public void testcase1() throws Exception {

convertAndValidate(CpptestInputMetric.class, "testcase1/result.xml", "testcase1/junit-result.xml");
}

@Test
public void testcase2() throws Exception {

convertAndValidate(CpptestInputMetric.class, "testcase2/result.xml", "testcase2/junit-result.xml");
}
}

0 comments on commit e607aff

Please sign in to comment.