Skip to content

Commit

Permalink
Noting inner class issue; see also #882 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 4, 2024
1 parent 319a535 commit b9e40f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,16 @@ public String[] getTruststoreJavaOptions() {
* try {@link #runRemotely(Step2)} or {@link #runRemotely(StepWithReturnAndOneArg, Serializable)} etc.
* (using {@link XStreamSerializable} as needed).
* <p>
* Alternately, you could use a lambda (taking care not to capture non-serializable objects from scope):
* Alternately, you could use a lambda:
* <pre>
* &#64;Test public void stuff() throws Throwable {
* rr.then(r -> {
* // as needed
* });
* }
* </pre>
* In this case you must take care not to capture non-serializable objects from scope;
* in particular, the body must not use (named or anonymous) inner classes.
*/
@FunctionalInterface
public interface Step extends Serializable {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jvnet/hudson/test/TestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,23 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
}

protected Object writeReplace() { return new Object(); }

@FunctionalInterface
public interface Body {
void perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException;
}

/**
* More convenient form that can be used with a lambda.
*/
public static Builder of(Body body) {
return new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
body.perform(build, launcher, listener);
return true;
}
};
}

}
34 changes: 20 additions & 14 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import jenkins.model.JenkinsLocationConfiguration;
import static org.junit.Assume.assumeThat;
import org.junit.AssumptionViolatedException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.recipes.LocalData;
Expand Down Expand Up @@ -181,23 +182,17 @@ private static void _error(JenkinsRule r) throws Throwable {

@Test public void agentBuild() throws Throwable {
try (TailLog tailLog = new TailLog(rr, "p", 1).withColor(PrefixedOutputStream.Color.MAGENTA)) {
rr.then(RealJenkinsRuleTest::_agentBuild);
rr.then(r -> {
FreeStyleProject p = r.createFreeStyleProject("p");
AtomicReference<Boolean> ran = new AtomicReference<>(false);
p.getBuildersList().add(TestBuilder.of((build, launcher, listener) -> ran.set(true)));
p.setAssignedNode(r.createOnlineSlave());
r.buildAndAssertSuccess(p);
assertTrue(ran.get());
});
tailLog.waitForCompletion();
}
}
private static void _agentBuild(JenkinsRule r) throws Throwable {
FreeStyleProject p = r.createFreeStyleProject("p");
AtomicReference<Boolean> ran = new AtomicReference<>(false);
p.getBuildersList().add(new TestBuilder() {
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
ran.set(true);
return true;
}
});
p.setAssignedNode(r.createOnlineSlave());
r.buildAndAssertSuccess(p);
assertTrue(ran.get());
}

@Test public void htmlUnit() throws Throwable {
rr.startJenkins();
Expand Down Expand Up @@ -417,4 +412,15 @@ private static XStreamSerializable<ParametersAction> _xStreamSerializable(Jenkin
return XStreamSerializable.of(b.getAction(ParametersAction.class));
}

@Ignore("inner class inside lambda breaks with an opaque NotSerializableException: RealJenkinsRuleTest; use TestBuilder.of instead")
@Test public void lambduh() throws Throwable {
rr.then(r -> {
r.createFreeStyleProject().getBuildersList().add(new TestBuilder() {
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
return true;
}
});
});
}

}

0 comments on commit b9e40f5

Please sign in to comment.