Skip to content
Permalink
Browse files
Fixing Jenkins 2 part of JENKINS-29649. Making the testcase green, mi…
…grating AbstractBuild -> Run to better support Jenkins 2 workflow jobs.
  • Loading branch information
Vladislav Ponomarev committed Aug 23, 2016
1 parent 32d4a1c commit 2fbcc63feba07d583c5ec620fa47f5c509ca0f01
@@ -24,7 +24,6 @@
package org.tap4j.plugin;

import hudson.matrix.MatrixProject;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Run;
@@ -93,7 +92,7 @@ protected Class<TapBuildAction> getBuildActionClass() {

public TapBuildAction getLastBuildAction() {
TapBuildAction action = null;
final AbstractBuild<?, ?> lastBuild = this.getLastBuildWithTap();
final Run<?, ?> lastBuild = this.getLastBuildWithTap();

if (lastBuild != null) {
action = lastBuild.getAction(TapBuildAction.class);
@@ -105,8 +104,8 @@ public TapBuildAction getLastBuildAction() {
/**
* @return
*/
private AbstractBuild<?, ?> getLastBuildWithTap() {
AbstractBuild<?, ?> lastBuild = this.project.getLastBuild();
private Run<?, ?> getLastBuildWithTap() {
Run<?, ?> lastBuild = this.job.getLastBuild();
while (lastBuild != null && lastBuild.getAction(TapBuildAction.class) == null) {
lastBuild = lastBuild.getPreviousBuild();
}
@@ -115,7 +114,7 @@ public TapBuildAction getLastBuildAction() {

public void doIndex( final StaplerRequest request,
final StaplerResponse response ) throws IOException {
AbstractBuild<?, ?> lastBuild = this.getLastBuildWithTap();
Run<?, ?> lastBuild = this.getLastBuildWithTap();
if (lastBuild == null) {
response.sendRedirect2("nodata");
} else {
@@ -169,17 +168,16 @@ protected JFreeChart createGraph() {
* @return value for property 'graphAvailable'
*/
public boolean isGraphActive() {
AbstractBuild<?, ?> build = getProject().getLastBuild();
AbstractProject<?,?> p = getProject();
Run<?, ?> build = this.job.getLastBuild();
// in order to have a graph, we must have at least two points.
int numPoints = 0;
while (numPoints < 2) {
if (build == null) {
return false;
}
if( p instanceof MatrixProject )
if( this.job instanceof MatrixProject )
{
MatrixProject mp = (MatrixProject) p;
MatrixProject mp = (MatrixProject) this.job;

for (Job j : mp.getAllJobs()) {
if (j != mp) { //getAllJobs includes the parent job too, so skip that
@@ -215,10 +213,10 @@ public boolean isGraphActive() {
* @return true, if new image does NOT need to be generated, false otherwise
*/
private boolean newGraphNotNeeded( final StaplerRequest req, StaplerResponse rsp ) {
Calendar t = getProject().getLastCompletedBuild().getTimestamp();
Calendar t = this.job.getLastCompletedBuild().getTimestamp();
Integer prevNumBuilds = requestMap.get(req.getRequestURI());
int numBuilds = 0;
RunList<?> builds = getProject().getBuilds();
RunList<?> builds = this.job.getBuilds();
Iterator<?> it = builds.iterator();
while (it.hasNext()) {
it.next();
@@ -248,10 +246,10 @@ private boolean newGraphNotNeeded( final StaplerRequest req, StaplerResponse rsp

protected void populateDataSetBuilder(DataSetBuilder<String, ChartUtil.NumberOnlyBuildLabel> dataset ) {

AbstractProject<?, ?> p = getProject();
Job<?, ?> p = this.job;

for (Run<?, ?> build = getProject().getLastBuild(); build != null; build = build.getPreviousBuild()) {
ChartUtil.NumberOnlyBuildLabel label = new ChartUtil.NumberOnlyBuildLabel((Run<?, ?>) build);
for (Run<?, ?> build = this.job.getLastBuild(); build != null; build = build.getPreviousBuild()) {
ChartUtil.NumberOnlyBuildLabel label = new ChartUtil.NumberOnlyBuildLabel((Run) build);

Result r = new Result();

@@ -26,11 +26,15 @@
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.HealthReport;
import hudson.model.HealthReportingAction;
import hudson.model.Job;
import hudson.model.Run;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.Messages;
import jenkins.model.RunAction2;
import jenkins.tasks.SimpleBuildStep;
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.StaplerProxy;
import org.kohsuke.stapler.export.Exported;
import org.tap4j.plugin.model.TapStreamResult;
@@ -46,26 +50,31 @@
* @since 0.1
*/
public class TapTestResultAction
extends AbstractTestResultAction<AbstractTestResultAction<?>>
implements StaplerProxy, SimpleBuildStep.LastBuildAction {
implements StaplerProxy, SimpleBuildStep.LastBuildAction, HealthReportingAction, RunAction2 {

public transient Run<?,?> run;
@Deprecated
public transient AbstractBuild<?,?> owner;

private TapResult tapResult;



/**
* @param owner
* @param r
* @param tapResult
*/
@Deprecated
protected TapTestResultAction(AbstractBuild<?, ?> owner, TapResult tapResult) {
this((Run) owner, tapResult);
}

/**
* @param owner
* @param r
* @param tapResult
*/
protected TapTestResultAction(Run owner, TapResult tapResult) {
super(owner);
protected TapTestResultAction(Run r, TapResult tapResult) {
setRunAndOwner(r);

this.tapResult = tapResult;
}

@@ -79,7 +88,6 @@ public TapResult getTapResult() {
/* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getFailCount()
*/
@Override
@Exported(visibility = 2)
public int getFailCount() {
return tapResult.getFailed();
@@ -88,7 +96,6 @@ public int getFailCount() {
/* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getTotalCount()
*/
@Override
@Exported(visibility = 2)
public int getTotalCount() {
return tapResult.getTotal();
@@ -97,7 +104,6 @@ public int getTotalCount() {
/* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getSkipCount()
*/
@Override
@Exported(visibility = 2)
public int getSkipCount() {
return tapResult.getSkipped();
@@ -106,9 +112,7 @@ public int getSkipCount() {
/* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getFailedTests()
*/
@Override
public List<CaseResult> getFailedTests() {
//throw new AssertionError("Not supposed to be called");
return Collections.emptyList();
}

@@ -120,11 +124,6 @@ public Object getTarget() {
return getResult();
}

/*
* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getResult()
*/
@Override
public TapStreamResult getResult() {
return new TapStreamResult(owner, tapResult);
}
@@ -138,9 +137,15 @@ public String getUrlName() {
return "tapTestReport";
}


@Override
public String getIconFileName() {
return "clipboard.png";
}

/* (non-Javadoc)
* @see hudson.tasks.test.AbstractTestResultAction#getDisplayName()
*/
* @see hudson.tasks.test.AbstractTestResultAction#getDisplayName()
*/
@Override
public String getDisplayName() {
return "TAP Test Results";
@@ -159,6 +164,38 @@ public Collection<? extends Action> getProjectActions() {
return Collections.singleton(new TapProjectAction(job));
}

@Override
public HealthReport getBuildHealth() {
final double scaleFactor = 1.0;
final int totalCount = getTotalCount();
final int failCount = getFailCount();
int score = (totalCount == 0)
? 100
: (int) (100.0 * Math.max(0.0, Math.min(1.0, 1.0 - (scaleFactor * failCount) / totalCount)));
Localizable description, displayName = Messages._AbstractTestResultAction_getDisplayName();
if (totalCount == 0) {
description = Messages._AbstractTestResultAction_zeroTestDescription(displayName);
} else {
description = Messages._AbstractTestResultAction_TestsDescription(displayName, failCount, totalCount);
}
return new HealthReport(score, description);
}

@Override
public void onAttached(Run<?, ?> r) {
setRunAndOwner(r);
}

@Override
public void onLoad(Run<?, ?> r) {
setRunAndOwner(r);
}

private void setRunAndOwner(Run<?, ?> r) {
this.run = r;
this.owner = r instanceof AbstractBuild ? (AbstractBuild<?,?>) r : null;
}

void mergeResult(TapResult additionalResult) {
TapStreamResult original = getResult();
original.merge(additionalResult);
@@ -23,7 +23,7 @@
*/
package org.tap4j.plugin.model;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.TabulatedResult;
import hudson.tasks.test.TestObject;
@@ -49,11 +49,11 @@
public class TapStreamResult extends TabulatedResult {

private static final long serialVersionUID = 8337146933697574082L;
private final AbstractBuild<?, ?> owner;
private final Run<?, ?> owner;
private List<TestResult> children = new ArrayList<TestResult>();
private TapResult tapResult;

public TapStreamResult(AbstractBuild<?, ?> owner, TapResult tapResult) {
public TapStreamResult(Run<?, ?> owner, TapResult tapResult) {
this.owner = owner;
this.tapResult = tapResult;
setChildrenInfo();
@@ -70,7 +70,7 @@ public String getDisplayName() {
* @see hudson.tasks.test.TestObject#getOwner()
*/
@Override
public AbstractBuild<?, ?> getOwner() {
public Run<?, ?> getRun() {
return owner;
}

@@ -26,7 +26,7 @@
import hudson.Functions;
import hudson.model.AbstractBuild;
import hudson.model.Item;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.model.Run;
import hudson.tasks.test.TestObject;
import hudson.tasks.test.TestResult;
import jenkins.model.Jenkins;
@@ -37,6 +37,7 @@
import org.tap4j.model.Directive;
import org.tap4j.model.TestSet;
import org.tap4j.plugin.TapResult;
import org.tap4j.plugin.TapTestResultAction;
import org.tap4j.plugin.util.Util;
import org.tap4j.util.DirectiveValues;

@@ -60,14 +61,14 @@ public class TapTestResultResult extends TestResult {
private static final long serialVersionUID = -4499261655602135921L;
private static final Logger LOGGER = Logger.getLogger(TapTestResultResult.class.getName());

private final AbstractBuild<?, ?> owner;
private final Run<?, ?> owner;
private final org.tap4j.model.TestResult tapTestResult;
private final TestSetMap testSetMap;
private final Boolean todoIsFailure;
private final Boolean includeCommentDiagnostics;
private final Boolean validateNumberOfTests;

public TapTestResultResult(AbstractBuild<?, ?> owner, TestSetMap testSetMap, org.tap4j.model.TestResult tapTestResult,
public TapTestResultResult(Run<?, ?> owner, TestSetMap testSetMap, org.tap4j.model.TestResult tapTestResult,
Boolean todoIsFailure, Boolean includeCommentDiagnostics, Boolean validateNumberOfTests) {
this.owner = owner;
this.testSetMap = testSetMap;
@@ -88,7 +89,7 @@ public String getDisplayName() {
* @see hudson.tasks.test.TestObject#getOwner()
*/
@Override
public AbstractBuild<?, ?> getOwner() {
public Run<?, ?> getRun() {
return owner;
}

@@ -192,7 +193,7 @@ public String getRelativePathFrom(TestObject it) {

// Start with the test result action
@SuppressWarnings("rawtypes")
AbstractTestResultAction action = getTestResultAction();
TapTestResultAction action = getTestResultActionDiverged();
if (action==null) {
//LOGGER.warning("trying to get relative path, but we can't determine the action that owns this result.");
return ""; // this won't take us to the right place, but it also won't 404.
@@ -304,4 +305,19 @@ public String toString() {
return pw.toString();
}

/**
* This is mostly a verbatim from {@link TestObject#getTestResultAction()}, with the only difference in a return
* type.
*
* @return associated TAP test result action object
*/
private TapTestResultAction getTestResultActionDiverged() {
Run<?, ?> owner = getRun();
if (owner != null) {
return owner.getAction(TapTestResultAction.class);
} else {
LOGGER.warning("owner is null when trying to getTestResultActionDiverged.");
return null;
}
}
}
@@ -121,14 +121,14 @@ public static JFreeChart createChart(StaplerRequest req, CategoryDataset dataset
@Override
public String generateURL(CategoryDataset dataset, int row, int column) {
NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
return label.build.getNumber() + "/" + AbstractTapProjectAction.URL_NAME + "/";
return label.getRun().getNumber() + "/" + AbstractTapProjectAction.URL_NAME + "/";
}

@Override
public String generateToolTip(CategoryDataset dataset, int row, int column)
{
NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
TapBuildAction build = label.build.getAction(TapBuildAction.class);
TapBuildAction build = label.getRun().getAction(TapBuildAction.class);
TapResult report = build.getResult();
report.tally();

@@ -276,7 +276,7 @@ public String generateURL( CategoryDataset dataset, int series,
return null;
}
// return label.build.getUpUrl() + label.build.getNumber() + "/" + PluginImpl.URL + "/" + methodUrl;
return label.build.getUpUrl() + label.build.getNumber() + "/tap/" + methodUrl;
return label.build.getUpUrl() + label.getRun().getNumber() + "/tap/" + methodUrl;
}
});

0 comments on commit 2fbcc63

Please sign in to comment.