Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
[JENKINS-30551] Make ResultAction serializable.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed May 19, 2018
1 parent abd38ca commit 17c4d9f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
@@ -1,5 +1,6 @@
package io.jenkins.plugins.analysis.core.views;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -32,14 +33,16 @@
*
* @author Ulli Hafner
*/
public class ResultAction implements HealthReportingAction, LastBuildAction, RunAction2, StaplerProxy {
public class ResultAction implements HealthReportingAction, LastBuildAction, RunAction2, StaplerProxy, Serializable {
private static final long serialVersionUID = 6683647181785654908L;

private transient Run<?, ?> owner;

private final AnalysisResult result;
private final HealthDescriptor healthDescriptor;
private final String id;
private final String name;
private final Charset charset;
private final String charset;

/**
* Creates a new instance of {@link ResultAction}.
Expand All @@ -64,7 +67,7 @@ public ResultAction(final Run<?, ?> owner, final AnalysisResult result, final He
this.healthDescriptor = healthDescriptor;
this.id = id;
this.name = name;
this.charset = charset;
this.charset = charset.name();
}

/**
Expand Down Expand Up @@ -128,6 +131,25 @@ public String getIconFileName() {
return null;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ResultAction that = (ResultAction) o;

return id.equals(that.id);
}

@Override
public int hashCode() {
return id.hashCode();
}

/**
* Returns whether a large image is defined.
*
Expand Down Expand Up @@ -200,6 +222,6 @@ private StaticAnalysisLabelProvider getLabelProvider() {
*/
@Override
public Object getTarget() {
return new IssuesDetail(owner, result, getLabelProvider(), charset);
return new IssuesDetail(owner, result, getLabelProvider(), Charset.forName(charset));
}
}
@@ -0,0 +1,39 @@
package io.jenkins.plugins.analysis.core.views;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;

import edu.hm.hafner.analysis.Priority;
import edu.hm.hafner.util.SerializableTest;
import io.jenkins.plugins.analysis.core.model.AnalysisResult;
import io.jenkins.plugins.analysis.core.quality.HealthDescriptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import hudson.model.Run;

/**
* Tests the class {@link ResultAction}.
*
* @author Ullrich Hafner
*/
class ResultActionTest extends SerializableTest<ResultAction> {
@Test
void shouldRestoreRun() {
ResultAction action = createSerializable();

assertThat(action.getOwner()).isNull();

Run run = mock(Run.class);
action.onAttached(run);
assertThat(action.getOwner()).isSameAs(run);
}

@Override
protected ResultAction createSerializable() {
return new ResultAction(null, mock(AnalysisResult.class),
new HealthDescriptor(0, 0, Priority.HIGH),
"ID", "Name", StandardCharsets.UTF_8);
}
}

0 comments on commit 17c4d9f

Please sign in to comment.