forked from spring-projects/spring-framework
/
ErrorsMethodArgumentResolverTests.java
192 lines (151 loc) · 6.54 KB
/
ErrorsMethodArgumentResolverTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method.annotation;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.method.ResolvableMethod;
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Unit tests for {@link ErrorsMethodArgumentResolver}.
*
* @author Rossen Stoyanchev
*/
class ErrorsMethodArgumentResolverTests {
private final ErrorsMethodArgumentResolver resolver =
new ErrorsMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance());
private final BindingContext bindingContext = new BindingContext();
private final MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.post("/path"));
private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();
@Test
void supports() {
MethodParameter parameter = this.testMethod.arg(Errors.class);
assertThat(this.resolver.supportsParameter(parameter)).isTrue();
parameter = this.testMethod.arg(BindingResult.class);
assertThat(this.resolver.supportsParameter(parameter)).isTrue();
parameter = this.testMethod.arg(ResolvableType.forClassWithGenerics(Mono.class, Errors.class));
assertThat(this.resolver.supportsParameter(parameter)).isTrue();
parameter = this.testMethod.arg(String.class);
assertThat(this.resolver.supportsParameter(parameter)).isFalse();
}
@Test
void resolve() {
BindingResult bindingResult = createBindingResult(new Foo(), "foo");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", bindingResult);
MethodParameter parameter = this.testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000));
assertThat(actual).isSameAs(bindingResult);
}
@Test
void resolveOnBindingResultAndModelAttributeWithCustomValue() {
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", bindingResult);
ResolvableMethod testMethod = ResolvableMethod.on(getClass())
.named("handleWithModelAttributeValue").build();
MethodParameter parameter = testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000));
assertThat(actual).isSameAs(bindingResult);
}
private BindingResult createBindingResult(Foo target, String name) {
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
return binder.getBindingResult();
}
@Test
void resolveWithMono() {
BindingResult bindingResult = createBindingResult(new Foo(), "foo");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", Mono.just(bindingResult));
MethodParameter parameter = this.testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000));
assertThat(actual).isSameAs(bindingResult);
}
@Test
void resolveWithMonoOnBindingResultAndModelAttributeWithCustomValue() {
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", Mono.just(bindingResult));
ResolvableMethod testMethod = ResolvableMethod.on(getClass())
.named("handleWithModelAttributeValue").build();
MethodParameter parameter = testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000));
assertThat(actual).isSameAs(bindingResult);
}
@Test
void resolveWithMonoOnBindingResultAndModelAttribute() {
MethodParameter parameter = this.testMethod.arg(BindingResult.class);
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000)))
.withMessageContaining("An @ModelAttribute and an Errors/BindingResult argument " +
"cannot both be declared with an async type wrapper.");
}
@Test // SPR-16187
void resolveWithBindingResultNotFound() {
MethodParameter parameter = this.testMethod.arg(Errors.class);
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000)))
.withMessageContaining("An Errors/BindingResult argument is expected " +
"immediately after the @ModelAttribute argument");
}
@SuppressWarnings("unused")
private static class Foo {
private String name;
public Foo() {
}
public Foo(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@SuppressWarnings("unused")
void handle(
@ModelAttribute Foo foo,
Errors errors,
@ModelAttribute Mono<Foo> fooMono,
BindingResult bindingResult,
Mono<Errors> errorsMono,
String string) {
}
@SuppressWarnings("unused")
void handleWithModelAttributeValue(
@ModelAttribute("custom") Foo foo,
Errors errors,
@ModelAttribute Mono<Foo> fooMono,
BindingResult bindingResult,
Mono<Errors> errorsMono,
String string) {
}
}