|
| 1 | +/* |
| 2 | + * Copyright 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.amqp.rabbit.listener.adapter; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | + |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.lang.reflect.UndeclaredThrowableException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.beans.factory.config.BeanExpressionContext; |
| 30 | +import org.springframework.beans.factory.config.BeanExpressionResolver; |
| 31 | +import org.springframework.format.support.DefaultFormattingConversionService; |
| 32 | +import org.springframework.messaging.converter.GenericMessageConverter; |
| 33 | +import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; |
| 34 | +import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; |
| 35 | +import org.springframework.messaging.handler.invocation.InvocableHandlerMethod; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Gary Russell |
| 39 | + * @since 2.4.12 |
| 40 | + * |
| 41 | + */ |
| 42 | +public class DelegatingInvocableHandlerTests { |
| 43 | + |
| 44 | + @Test |
| 45 | + void multiNoMatch() throws Exception { |
| 46 | + List<InvocableHandlerMethod> methods = new ArrayList<>(); |
| 47 | + Object bean = new Multi(); |
| 48 | + Method method = Multi.class.getDeclaredMethod("listen", Integer.class); |
| 49 | + methods.add(messageHandlerFactory().createInvocableHandlerMethod(bean, method)); |
| 50 | + BeanExpressionResolver resolver = mock(BeanExpressionResolver.class); |
| 51 | + BeanExpressionContext context = mock(BeanExpressionContext.class); |
| 52 | + DelegatingInvocableHandler handler = new DelegatingInvocableHandler(methods, bean, resolver, context); |
| 53 | + assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(() -> |
| 54 | + handler.getHandlerForPayload(Long.class)) |
| 55 | + .withCauseExactlyInstanceOf(NoSuchMethodException.class) |
| 56 | + .withStackTraceContaining("No listener method found in"); |
| 57 | + } |
| 58 | + |
| 59 | + private MessageHandlerMethodFactory messageHandlerFactory() { |
| 60 | + DefaultMessageHandlerMethodFactory defaultFactory = new DefaultMessageHandlerMethodFactory(); |
| 61 | + DefaultFormattingConversionService cs = new DefaultFormattingConversionService(); |
| 62 | + defaultFactory.setConversionService(cs); |
| 63 | + GenericMessageConverter messageConverter = new GenericMessageConverter(cs); |
| 64 | + defaultFactory.setMessageConverter(messageConverter); |
| 65 | + defaultFactory.afterPropertiesSet(); |
| 66 | + return defaultFactory; |
| 67 | + } |
| 68 | + |
| 69 | + public static class Multi { |
| 70 | + |
| 71 | + void listen(Integer in) { |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments