Skip to content

Commit

Permalink
Merge pull request #9 from ndeloof/JENKINS-15035
Browse files Browse the repository at this point in the history
[JENKINS-15035] quick check report files identified by pattern to be xml...
  • Loading branch information
ssogabe committed Sep 19, 2012
2 parents dd80f6c + c589b1e commit 875d27d
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/main/java/hudson/plugins/cobertura/CoberturaPublisher.java
Expand Up @@ -18,14 +18,19 @@
import hudson.plugins.cobertura.targets.CoverageMetric;
import hudson.plugins.cobertura.targets.CoverageResult;
import hudson.plugins.cobertura.targets.CoverageTarget;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -40,6 +45,13 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
* Cobertura {@link Publisher}.
*
Expand Down Expand Up @@ -320,7 +332,37 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

FilePath[] reports = new FilePath[0];
try {
reports = moduleRoot.list(coberturaReportFile);
reports = moduleRoot.act(new FilePath.FileCallable<FilePath[]>() {
public FilePath[] invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
FilePath[] r = new FilePath(f).list(coberturaReportFile);

XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty("javax.xml.stream.supportDTD", "false");

for (FilePath filePath : r) {
try {
XMLEventReader reader = factory.createXMLEventReader(filePath.read());
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement start = (StartElement) event;
if (start.getName().getLocalPart().equals("coverage")) {
// This is a cobertura coverage report file
break;
} else {
throw new IOException(filePath + " is not a cobertura coverage report, please check your report pattern");
}
}
}
reader.close();
} catch (XMLStreamException e) {
throw new IOException(filePath + " is not an XML file, please check your report pattern");
}

}
return r;
}
});

// if the build has failed, then there's not
// much point in reporting an error
Expand Down

0 comments on commit 875d27d

Please sign in to comment.