Skip to content

Commit 8695fad

Browse files
committed
Merge pull request #23692 from ttddyy
* pr/23692: Polish "Add a composite for TaskDecorator" Add a composite for TaskDecorator Closes gh-23692
2 parents 8f6d24e + 6e141cc commit 8695fad

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.task.support;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.List;
22+
23+
import org.springframework.core.task.TaskDecorator;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* Composite {@link TaskDecorator} that delegates to other task decorators.
28+
*
29+
* @author Tadaya Tsuyukubo
30+
* @since 6.1
31+
*/
32+
public class CompositeTaskDecorator implements TaskDecorator {
33+
34+
private final List<TaskDecorator> taskDecorators;
35+
36+
/**
37+
* Create a new instance.
38+
* @param taskDecorators the taskDecorators to delegate to
39+
*/
40+
public CompositeTaskDecorator(Collection<? extends TaskDecorator> taskDecorators) {
41+
Assert.notNull(taskDecorators, "TaskDecorators must not be null");
42+
this.taskDecorators = new ArrayList<>(taskDecorators);
43+
}
44+
45+
@Override
46+
public Runnable decorate(Runnable runnable) {
47+
Assert.notNull(runnable, "Runnable must not be null");
48+
for (TaskDecorator taskDecorator : this.taskDecorators) {
49+
runnable = taskDecorator.decorate(runnable);
50+
}
51+
return runnable;
52+
}
53+
54+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.task.support;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.InOrder;
23+
24+
import org.springframework.core.task.TaskDecorator;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.inOrder;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
33+
34+
/**
35+
* Tests for {@link CompositeTaskDecorator}.
36+
*
37+
* @author Tadaya Tsuyukubo
38+
* @author Stephane Nicoll
39+
*/
40+
class CompositeTaskDecoratorTests {
41+
42+
@Test
43+
void createWithNullCollection() {
44+
assertThatIllegalArgumentException().isThrownBy(() -> new CompositeTaskDecorator(null))
45+
.withMessage("TaskDecorators must not be null");
46+
}
47+
48+
@Test
49+
void decorateWithNullRunnable() {
50+
CompositeTaskDecorator taskDecorator = new CompositeTaskDecorator(List.of());
51+
assertThatIllegalArgumentException().isThrownBy(() -> taskDecorator.decorate(null))
52+
.withMessage("Runnable must not be null");
53+
}
54+
55+
@Test
56+
void decorate() {
57+
TaskDecorator first = mockNoOpTaskDecorator();
58+
TaskDecorator second = mockNoOpTaskDecorator();
59+
TaskDecorator third = mockNoOpTaskDecorator();
60+
CompositeTaskDecorator taskDecorator = new CompositeTaskDecorator(List.of(first, second, third));
61+
Runnable runnable = mock(Runnable.class);
62+
taskDecorator.decorate(runnable);
63+
InOrder ordered = inOrder(first, second, third);
64+
ordered.verify(first).decorate(runnable);
65+
ordered.verify(second).decorate(runnable);
66+
ordered.verify(third).decorate(runnable);
67+
}
68+
69+
@Test
70+
void decorateReuseResultOfPreviousRun() {
71+
Runnable original = mock(Runnable.class);
72+
Runnable firstDecorated = mock(Runnable.class);
73+
TaskDecorator first = mock(TaskDecorator.class);
74+
given(first.decorate(original)).willReturn(firstDecorated);
75+
Runnable secondDecorated = mock(Runnable.class);
76+
TaskDecorator second = mock(TaskDecorator.class);
77+
given(second.decorate(firstDecorated)).willReturn(secondDecorated);
78+
Runnable result = new CompositeTaskDecorator(List.of(first, second)).decorate(original);
79+
assertThat(result).isSameAs(secondDecorated);
80+
verify(first).decorate(original);
81+
verify(second).decorate(firstDecorated);
82+
}
83+
84+
private TaskDecorator mockNoOpTaskDecorator() {
85+
TaskDecorator mock = mock(TaskDecorator.class);
86+
given(mock.decorate(any())).willAnswer(invocation -> invocation.getArguments()[0]);
87+
return mock;
88+
}
89+
90+
}

0 commit comments

Comments
 (0)