|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.messaging.simp.config; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.core.task.TaskExecutor; |
| 25 | +import org.springframework.messaging.support.ChannelInterceptor; |
| 26 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.BDDMockito.given; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.verify; |
| 32 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link ChannelRegistration}. |
| 36 | + * |
| 37 | + * @author Stephane Nicoll |
| 38 | + */ |
| 39 | +class ChannelRegistrationTests { |
| 40 | + |
| 41 | + private final Supplier<TaskExecutor> fallback = mock(); |
| 42 | + |
| 43 | + private final Consumer<TaskExecutor> customizer = mock(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void emptyRegistrationUsesFallback() { |
| 47 | + TaskExecutor fallbackTaskExecutor = mock(TaskExecutor.class); |
| 48 | + given(this.fallback.get()).willReturn(fallbackTaskExecutor); |
| 49 | + ChannelRegistration registration = new ChannelRegistration(); |
| 50 | + assertThat(registration.hasTaskExecutor()).isFalse(); |
| 51 | + TaskExecutor actual = registration.getTaskExecutor(this.fallback, this.customizer); |
| 52 | + assertThat(actual).isSameAs(fallbackTaskExecutor); |
| 53 | + verify(this.fallback).get(); |
| 54 | + verify(this.customizer).accept(fallbackTaskExecutor); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void emptyRegistrationDoesNotHaveInterceptors() { |
| 59 | + ChannelRegistration registration = new ChannelRegistration(); |
| 60 | + assertThat(registration.hasInterceptors()).isFalse(); |
| 61 | + assertThat(registration.getInterceptors()).isEmpty(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void taskRegistrationCreatesDefaultInstance() { |
| 66 | + ChannelRegistration registration = new ChannelRegistration(); |
| 67 | + registration.taskExecutor(); |
| 68 | + assertThat(registration.hasTaskExecutor()).isTrue(); |
| 69 | + TaskExecutor taskExecutor = registration.getTaskExecutor(this.fallback, this.customizer); |
| 70 | + assertThat(taskExecutor).isInstanceOf(ThreadPoolTaskExecutor.class); |
| 71 | + verifyNoInteractions(this.fallback); |
| 72 | + verify(this.customizer).accept(taskExecutor); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void taskRegistrationWithExistingThreadPoolTaskExecutor() { |
| 77 | + ThreadPoolTaskExecutor existingTaskExecutor = mock(ThreadPoolTaskExecutor.class); |
| 78 | + ChannelRegistration registration = new ChannelRegistration(); |
| 79 | + registration.taskExecutor(existingTaskExecutor); |
| 80 | + assertThat(registration.hasTaskExecutor()).isTrue(); |
| 81 | + TaskExecutor taskExecutor = registration.getTaskExecutor(this.fallback, this.customizer); |
| 82 | + assertThat(taskExecutor).isSameAs(existingTaskExecutor); |
| 83 | + verifyNoInteractions(this.fallback); |
| 84 | + verify(this.customizer).accept(taskExecutor); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void configureExecutor() { |
| 89 | + ChannelRegistration registration = new ChannelRegistration(); |
| 90 | + TaskExecutor taskExecutor = mock(TaskExecutor.class); |
| 91 | + registration.executor(taskExecutor); |
| 92 | + assertThat(registration.hasTaskExecutor()).isTrue(); |
| 93 | + TaskExecutor taskExecutor1 = registration.getTaskExecutor(this.fallback, this.customizer); |
| 94 | + assertThat(taskExecutor1).isSameAs(taskExecutor); |
| 95 | + verifyNoInteractions(this.fallback, this.customizer); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void configureExecutorTakesPrecedenceOverTaskRegistration() { |
| 100 | + ChannelRegistration registration = new ChannelRegistration(); |
| 101 | + TaskExecutor taskExecutor = mock(TaskExecutor.class); |
| 102 | + registration.executor(taskExecutor); |
| 103 | + ThreadPoolTaskExecutor ignored = mock(ThreadPoolTaskExecutor.class); |
| 104 | + registration.taskExecutor(ignored); |
| 105 | + assertThat(registration.hasTaskExecutor()).isTrue(); |
| 106 | + assertThat(registration.getTaskExecutor(this.fallback, this.customizer)).isSameAs(taskExecutor); |
| 107 | + verifyNoInteractions(ignored, this.fallback, this.customizer); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void configureInterceptors() { |
| 113 | + ChannelRegistration registration = new ChannelRegistration(); |
| 114 | + ChannelInterceptor interceptor1 = mock(ChannelInterceptor.class); |
| 115 | + registration.interceptors(interceptor1); |
| 116 | + ChannelInterceptor interceptor2 = mock(ChannelInterceptor.class); |
| 117 | + registration.interceptors(interceptor2); |
| 118 | + assertThat(registration.getInterceptors()).containsExactly(interceptor1, interceptor2); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments