|
| 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