Skip to content

Commit

Permalink
Add constructor with Callable to CallableTaskletAdapter
Browse files Browse the repository at this point in the history
Issue #3831
  • Loading branch information
benelog authored and fmbenhassine committed Sep 14, 2023
1 parent 058dd3c commit 31955a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,21 @@ public class CallableTaskletAdapter implements Tasklet, InitializingBean {

private Callable<RepeatStatus> callable;

/**
* Create a new {@link CallableTaskletAdapter} instance.
*/
public CallableTaskletAdapter() {
}

/**
* Create a new {@link CallableTaskletAdapter} instance.
* @param callable the {@link Callable} to use
*/
public CallableTaskletAdapter(Callable<RepeatStatus> callable) {
setCallable(callable);
afterPropertiesSet();
}

/**
* Public setter for the {@link Callable}.
* @param callable the {@link Callable} to set
Expand All @@ -48,7 +63,7 @@ public void setCallable(Callable<RepeatStatus> callable) {
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Assert.state(callable != null, "A Callable is required");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@

class CallableTaskletAdapterTests {

private final CallableTaskletAdapter adapter = new CallableTaskletAdapter();
@Test
public void testHandleWithConstructor() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter(() -> RepeatStatus.FINISHED);
assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null));
}

@Test
void testHandle() throws Exception {
void testHandleWithSetter() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter();
adapter.setCallable(() -> RepeatStatus.FINISHED);
assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null));
}

@Test
void testAfterPropertiesSet() {
assertThrows(IllegalStateException.class, adapter::afterPropertiesSet);
assertThrows(IllegalStateException.class, new CallableTaskletAdapter()::afterPropertiesSet);
}

}

0 comments on commit 31955a0

Please sign in to comment.