Skip to content

Commit

Permalink
Advertise lambdas as arguments to then / runRemotely
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 2, 2024
1 parent 2940c2f commit 1f09a19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,8 @@ public String[] getTruststoreJavaOptions() {
/**
* One step to run.
* <p>Since this thunk will be sent to a different JVM, it must be serializable.
* The test class will certainly not be serializable, so you cannot use an anonymous inner class,
* and regular lambdas also risk accidentally capturing non-serializable objects from scope.
* The friendliest idiom is a static method reference:
* The test class will certainly not be serializable, so you cannot use an anonymous inner class.
* One idiom is a static method reference:
* <pre>
* &#64;Test public void stuff() throws Throwable {
* rr.then(YourTest::_stuff);
Expand All @@ -759,6 +758,15 @@ public String[] getTruststoreJavaOptions() {
* If you need to pass and/or return values, you can still use a static method reference:
* 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):
* <pre>
* &#64;Test public void stuff() throws Throwable {
* rr.then(r -> {
* // as needed
* });
* }
* </pre>
*/
@FunctionalInterface
public interface Step extends Serializable {
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,14 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
// Neither ParametersDefinitionProperty nor ParametersAction could be passed directly.
// (In this case, ParameterDefinition and ParameterValue could have been used raw.
// But even List<ParameterValue> cannot be typed here, only e.g. ArrayList<ParameterValue>.)
var a = rr.runRemotely(RealJenkinsRuleTest::_xStreamSerializable, new XStreamSerializable<>(new ParametersDefinitionProperty(new StringParameterDefinition("X", "dflt"))));
assertThat(a.object().getAllParameters(), hasSize(1));
assertThat(rr.runRemotely(RealJenkinsRuleTest::_xStreamSerializable, new XStreamSerializable<>(new ParametersDefinitionProperty(new StringParameterDefinition("X", "dflt")))).object().getAllParameters(), hasSize(1));
var prop = new XStreamSerializable<>(new ParametersDefinitionProperty(new StringParameterDefinition("X", "dflt")));
assertThat(rr.runRemotely(r -> {
var p = r.createFreeStyleProject();
p.addProperty(prop.object());
var b = r.buildAndAssertSuccess(p);
return new XStreamSerializable<>(b.getAction(ParametersAction.class));
}).object().getAllParameters(), hasSize(1));
}

private static XStreamSerializable<ParametersAction> _xStreamSerializable(JenkinsRule r, XStreamSerializable<JobProperty<? super FreeStyleProject>> prop) throws Throwable {
Expand Down

0 comments on commit 1f09a19

Please sign in to comment.