Polish
Updates to synchronize with newly created reactive equivalents.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -16,23 +16,21 @@
|
||||
|
||||
package org.springframework.messaging.handler.annotation.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable;
|
||||
import org.springframework.messaging.handler.invocation.ResolvableMethod;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.messaging.handler.annotation.MessagingPredicates.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link DestinationVariableMethodArgumentResolver} tests.
|
||||
@@ -41,33 +39,17 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class DestinationVariableMethodArgumentResolverTests {
|
||||
|
||||
private DestinationVariableMethodArgumentResolver resolver;
|
||||
private final DestinationVariableMethodArgumentResolver resolver =
|
||||
new DestinationVariableMethodArgumentResolver(new DefaultConversionService());
|
||||
|
||||
private MethodParameter paramAnnotated;
|
||||
private MethodParameter paramAnnotatedValue;
|
||||
private MethodParameter paramNotAnnotated;
|
||||
private final ResolvableMethod resolvable =
|
||||
ResolvableMethod.on(getClass()).named("handleMessage").build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.resolver = new DestinationVariableMethodArgumentResolver(new DefaultConversionService());
|
||||
|
||||
Method method = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class);
|
||||
this.paramAnnotated = new MethodParameter(method, 0);
|
||||
this.paramAnnotatedValue = new MethodParameter(method, 1);
|
||||
this.paramNotAnnotated = new MethodParameter(method, 2);
|
||||
|
||||
this.paramAnnotated.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
GenericTypeResolver.resolveParameterType(this.paramAnnotated, DestinationVariableMethodArgumentResolver.class);
|
||||
this.paramAnnotatedValue.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
GenericTypeResolver.resolveParameterType(this.paramAnnotatedValue, DestinationVariableMethodArgumentResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
assertTrue(resolver.supportsParameter(paramAnnotated));
|
||||
assertTrue(resolver.supportsParameter(paramAnnotatedValue));
|
||||
assertFalse(resolver.supportsParameter(paramNotAnnotated));
|
||||
assertTrue(resolver.supportsParameter(this.resolvable.annot(destinationVar().noValue()).arg()));
|
||||
assertFalse(resolver.supportsParameter(this.resolvable.annotNotPresent(DestinationVariable.class).arg()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,17 +62,19 @@ public class DestinationVariableMethodArgumentResolverTests {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeader(
|
||||
DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars).build();
|
||||
|
||||
Object result = this.resolver.resolveArgument(this.paramAnnotated, message);
|
||||
MethodParameter param = this.resolvable.annot(destinationVar().noValue()).arg();
|
||||
Object result = this.resolver.resolveArgument(param, message);
|
||||
assertEquals("bar", result);
|
||||
|
||||
result = this.resolver.resolveArgument(this.paramAnnotatedValue, message);
|
||||
param = this.resolvable.annot(destinationVar("name")).arg();
|
||||
result = this.resolver.resolveArgument(param, message);
|
||||
assertEquals("value", result);
|
||||
}
|
||||
|
||||
@Test(expected = MessageHandlingException.class)
|
||||
public void resolveArgumentNotFound() throws Exception {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
|
||||
this.resolver.resolveArgument(this.paramAnnotated, message);
|
||||
this.resolver.resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.messaging.handler.annotation.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -25,19 +24,17 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.handler.invocation.ResolvableMethod;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.messaging.handler.annotation.MessagingPredicates.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link HeaderMethodArgumentResolver} tests.
|
||||
@@ -50,46 +47,27 @@ public class HeaderMethodArgumentResolverTests {
|
||||
|
||||
private HeaderMethodArgumentResolver resolver;
|
||||
|
||||
private MethodParameter paramRequired;
|
||||
private MethodParameter paramNamedDefaultValueStringHeader;
|
||||
private MethodParameter paramSystemPropertyDefaultValue;
|
||||
private MethodParameter paramSystemPropertyName;
|
||||
private MethodParameter paramNotAnnotated;
|
||||
private MethodParameter paramOptional;
|
||||
private MethodParameter paramNativeHeader;
|
||||
private final ResolvableMethod resolvable = ResolvableMethod.on(getClass()).named("handleMessage").build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@SuppressWarnings("resource")
|
||||
GenericApplicationContext cxt = new GenericApplicationContext();
|
||||
cxt.refresh();
|
||||
this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
|
||||
|
||||
Method method = ReflectionUtils.findMethod(getClass(), "handleMessage", (Class<?>[]) null);
|
||||
this.paramRequired = new SynthesizingMethodParameter(method, 0);
|
||||
this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1);
|
||||
this.paramSystemPropertyDefaultValue = new SynthesizingMethodParameter(method, 2);
|
||||
this.paramSystemPropertyName = new SynthesizingMethodParameter(method, 3);
|
||||
this.paramNotAnnotated = new SynthesizingMethodParameter(method, 4);
|
||||
this.paramOptional = new SynthesizingMethodParameter(method, 5);
|
||||
this.paramNativeHeader = new SynthesizingMethodParameter(method, 6);
|
||||
|
||||
this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.refresh();
|
||||
this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), context.getBeanFactory());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
assertTrue(resolver.supportsParameter(paramNamedDefaultValueStringHeader));
|
||||
assertFalse(resolver.supportsParameter(paramNotAnnotated));
|
||||
assertTrue(this.resolver.supportsParameter(this.resolvable.annot(headerPlain()).arg()));
|
||||
assertFalse(this.resolver.supportsParameter(this.resolvable.annotNotPresent(Header.class).arg()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgument() throws Exception {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeader("param1", "foo").build();
|
||||
Object result = this.resolver.resolveArgument(this.paramRequired, message);
|
||||
Object result = this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
|
||||
assertEquals("foo", result);
|
||||
}
|
||||
|
||||
@@ -98,7 +76,7 @@ public class HeaderMethodArgumentResolverTests {
|
||||
TestMessageHeaderAccessor headers = new TestMessageHeaderAccessor();
|
||||
headers.setNativeHeader("param1", "foo");
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
assertEquals("foo", this.resolver.resolveArgument(this.paramRequired, message));
|
||||
assertEquals("foo", this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,20 +86,23 @@ public class HeaderMethodArgumentResolverTests {
|
||||
headers.setNativeHeader("param1", "native-foo");
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
|
||||
assertEquals("foo", this.resolver.resolveArgument(this.paramRequired, message));
|
||||
assertEquals("native-foo", this.resolver.resolveArgument(this.paramNativeHeader, message));
|
||||
assertEquals("foo", this.resolver.resolveArgument(
|
||||
this.resolvable.annot(headerPlain()).arg(), message));
|
||||
|
||||
assertEquals("native-foo", this.resolver.resolveArgument(
|
||||
this.resolvable.annot(header("nativeHeaders.param1")).arg(), message));
|
||||
}
|
||||
|
||||
@Test(expected = MessageHandlingException.class)
|
||||
public void resolveArgumentNotFound() throws Exception {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
|
||||
this.resolver.resolveArgument(this.paramRequired, message);
|
||||
this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentDefaultValue() throws Exception {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
|
||||
Object result = this.resolver.resolveArgument(this.paramNamedDefaultValueStringHeader, message);
|
||||
Object result = this.resolver.resolveArgument(this.resolvable.annot(header("name", "bar")).arg(), message);
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
|
||||
@@ -130,7 +111,8 @@ public class HeaderMethodArgumentResolverTests {
|
||||
System.setProperty("systemProperty", "sysbar");
|
||||
try {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
|
||||
Object result = resolver.resolveArgument(paramSystemPropertyDefaultValue, message);
|
||||
MethodParameter param = this.resolvable.annot(header("name", "#{systemProperties.systemProperty}")).arg();
|
||||
Object result = resolver.resolveArgument(param, message);
|
||||
assertEquals("sysbar", result);
|
||||
}
|
||||
finally {
|
||||
@@ -143,7 +125,8 @@ public class HeaderMethodArgumentResolverTests {
|
||||
System.setProperty("systemProperty", "sysbar");
|
||||
try {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeader("sysbar", "foo").build();
|
||||
Object result = resolver.resolveArgument(paramSystemPropertyName, message);
|
||||
MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg();
|
||||
Object result = resolver.resolveArgument(param, message);
|
||||
assertEquals("foo", result);
|
||||
}
|
||||
finally {
|
||||
@@ -153,31 +136,22 @@ public class HeaderMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveOptionalHeaderWithValue() throws Exception {
|
||||
GenericApplicationContext cxt = new GenericApplicationContext();
|
||||
cxt.refresh();
|
||||
|
||||
HeaderMethodArgumentResolver resolver =
|
||||
new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
|
||||
|
||||
Message<String> message = MessageBuilder.withPayload("foo").setHeader("foo", "bar").build();
|
||||
Object result = resolver.resolveArgument(paramOptional, message);
|
||||
MethodParameter param = this.resolvable.annot(header("foo")).arg(Optional.class, String.class);
|
||||
Object result = resolver.resolveArgument(param, message);
|
||||
assertEquals(Optional.of("bar"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveOptionalHeaderAsEmpty() throws Exception {
|
||||
GenericApplicationContext cxt = new GenericApplicationContext();
|
||||
cxt.refresh();
|
||||
|
||||
HeaderMethodArgumentResolver resolver =
|
||||
new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
|
||||
|
||||
Message<String> message = MessageBuilder.withPayload("foo").build();
|
||||
Object result = resolver.resolveArgument(paramOptional, message);
|
||||
MethodParameter param = this.resolvable.annot(header("foo")).arg(Optional.class, String.class);
|
||||
Object result = resolver.resolveArgument(param, message);
|
||||
assertEquals(Optional.empty(), result);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"unused", "OptionalUsedAsFieldOrParameterType"})
|
||||
public void handleMessage(
|
||||
@Header String param1,
|
||||
@Header(name = "name", defaultValue = "bar") String param2,
|
||||
@@ -191,7 +165,7 @@ public class HeaderMethodArgumentResolverTests {
|
||||
|
||||
public static class TestMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
||||
|
||||
protected TestMessageHeaderAccessor() {
|
||||
TestMessageHeaderAccessor() {
|
||||
super((Map<String, List<String>>) null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -16,17 +16,16 @@
|
||||
|
||||
package org.springframework.messaging.handler.annotation.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.invocation.ResolvableMethod;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.messaging.support.MessageHeaderAccessor;
|
||||
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
|
||||
@@ -41,47 +40,31 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
private HeadersMethodArgumentResolver resolver;
|
||||
private final HeadersMethodArgumentResolver resolver = new HeadersMethodArgumentResolver();
|
||||
|
||||
private MethodParameter paramAnnotated;
|
||||
private MethodParameter paramAnnotatedNotMap;
|
||||
private MethodParameter paramMessageHeaders;
|
||||
private MethodParameter paramMessageHeaderAccessor;
|
||||
private MethodParameter paramMessageHeaderAccessorSubclass;
|
||||
private Message<byte[]> message =
|
||||
MessageBuilder.withPayload(new byte[0]).copyHeaders(Collections.singletonMap("foo", "bar")).build();
|
||||
|
||||
private Message<byte[]> message;
|
||||
private final ResolvableMethod resolvable = ResolvableMethod.on(getClass()).named("handleMessage").build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.resolver = new HeadersMethodArgumentResolver();
|
||||
|
||||
Method method = getClass().getDeclaredMethod("handleMessage", Map.class, String.class,
|
||||
MessageHeaders.class, MessageHeaderAccessor.class, TestMessageHeaderAccessor.class);
|
||||
|
||||
this.paramAnnotated = new MethodParameter(method, 0);
|
||||
this.paramAnnotatedNotMap = new MethodParameter(method, 1);
|
||||
this.paramMessageHeaders = new MethodParameter(method, 2);
|
||||
this.paramMessageHeaderAccessor = new MethodParameter(method, 3);
|
||||
this.paramMessageHeaderAccessorSubclass = new MethodParameter(method, 4);
|
||||
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("foo", "bar");
|
||||
this.message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
assertTrue(this.resolver.supportsParameter(this.paramAnnotated));
|
||||
assertFalse(this.resolver.supportsParameter(this.paramAnnotatedNotMap));
|
||||
assertTrue(this.resolver.supportsParameter(this.paramMessageHeaders));
|
||||
assertTrue(this.resolver.supportsParameter(this.paramMessageHeaderAccessor));
|
||||
assertTrue(this.resolver.supportsParameter(this.paramMessageHeaderAccessorSubclass));
|
||||
|
||||
assertTrue(this.resolver.supportsParameter(
|
||||
this.resolvable.annotPresent(Headers.class).arg(Map.class, String.class, Object.class)));
|
||||
|
||||
assertTrue(this.resolver.supportsParameter(this.resolvable.arg(MessageHeaders.class)));
|
||||
assertTrue(this.resolver.supportsParameter(this.resolvable.arg(MessageHeaderAccessor.class)));
|
||||
assertTrue(this.resolver.supportsParameter(this.resolvable.arg(TestMessageHeaderAccessor.class)));
|
||||
|
||||
assertFalse(this.resolver.supportsParameter(this.resolvable.annotPresent(Headers.class).arg(String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentAnnotated() throws Exception {
|
||||
Object resolved = this.resolver.resolveArgument(this.paramAnnotated, this.message);
|
||||
MethodParameter param = this.resolvable.annotPresent(Headers.class).arg(Map.class, String.class, Object.class);
|
||||
Object resolved = this.resolver.resolveArgument(param, this.message);
|
||||
|
||||
assertTrue(resolved instanceof Map);
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -91,12 +74,12 @@ public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void resolveArgumentAnnotatedNotMap() throws Exception {
|
||||
this.resolver.resolveArgument(this.paramAnnotatedNotMap, this.message);
|
||||
this.resolver.resolveArgument(this.resolvable.annotPresent(Headers.class).arg(String.class), this.message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentMessageHeaders() throws Exception {
|
||||
Object resolved = this.resolver.resolveArgument(this.paramMessageHeaders, this.message);
|
||||
Object resolved = this.resolver.resolveArgument(this.resolvable.arg(MessageHeaders.class), this.message);
|
||||
|
||||
assertTrue(resolved instanceof MessageHeaders);
|
||||
MessageHeaders headers = (MessageHeaders) resolved;
|
||||
@@ -105,7 +88,8 @@ public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveArgumentMessageHeaderAccessor() throws Exception {
|
||||
Object resolved = this.resolver.resolveArgument(this.paramMessageHeaderAccessor, this.message);
|
||||
MethodParameter param = this.resolvable.arg(MessageHeaderAccessor.class);
|
||||
Object resolved = this.resolver.resolveArgument(param, this.message);
|
||||
|
||||
assertTrue(resolved instanceof MessageHeaderAccessor);
|
||||
MessageHeaderAccessor headers = (MessageHeaderAccessor) resolved;
|
||||
@@ -114,7 +98,8 @@ public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveArgumentMessageHeaderAccessorSubclass() throws Exception {
|
||||
Object resolved = this.resolver.resolveArgument(this.paramMessageHeaderAccessorSubclass, this.message);
|
||||
MethodParameter param = this.resolvable.arg(TestMessageHeaderAccessor.class);
|
||||
Object resolved = this.resolver.resolveArgument(param, this.message);
|
||||
|
||||
assertTrue(resolved instanceof TestMessageHeaderAccessor);
|
||||
TestMessageHeaderAccessor headers = (TestMessageHeaderAccessor) resolved;
|
||||
@@ -124,7 +109,7 @@ public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handleMessage(
|
||||
@Headers Map<String, ?> param1,
|
||||
@Headers Map<String, Object> param1,
|
||||
@Headers String param2,
|
||||
MessageHeaders param3,
|
||||
MessageHeaderAccessor param4,
|
||||
@@ -134,7 +119,7 @@ public class HeadersMethodArgumentResolverTests {
|
||||
|
||||
public static class TestMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
||||
|
||||
protected TestMessageHeaderAccessor(Message<?> message) {
|
||||
TestMessageHeaderAccessor(Message<?> message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user