|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 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 | + * http://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 | +package org.springframework.web.reactive.result.method.annotation; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import reactor.core.publisher.Mono; |
| 21 | +import reactor.core.publisher.MonoProcessor; |
| 22 | + |
| 23 | +import org.springframework.core.MethodParameter; |
| 24 | +import org.springframework.core.ReactiveAdapterRegistry; |
| 25 | +import org.springframework.core.ResolvableType; |
| 26 | +import org.springframework.http.HttpMethod; |
| 27 | +import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; |
| 28 | +import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; |
| 29 | +import org.springframework.validation.BindingResult; |
| 30 | +import org.springframework.validation.Errors; |
| 31 | +import org.springframework.web.bind.WebExchangeDataBinder; |
| 32 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 33 | +import org.springframework.web.reactive.result.ResolvableMethod; |
| 34 | +import org.springframework.web.reactive.result.method.BindingContext; |
| 35 | +import org.springframework.web.server.ServerWebExchange; |
| 36 | +import org.springframework.web.server.adapter.DefaultServerWebExchange; |
| 37 | +import org.springframework.web.server.session.MockWebSessionManager; |
| 38 | +import org.springframework.web.server.session.WebSessionManager; |
| 39 | + |
| 40 | +import static junit.framework.TestCase.assertFalse; |
| 41 | +import static org.junit.Assert.assertSame; |
| 42 | +import static org.junit.Assert.assertTrue; |
| 43 | +import static org.springframework.core.ResolvableType.forClass; |
| 44 | +import static org.springframework.core.ResolvableType.forClassWithGenerics; |
| 45 | + |
| 46 | +/** |
| 47 | + * Unit tests for {@link ErrorsMethodArgumentResolver}. |
| 48 | + * @author Rossen Stoyanchev |
| 49 | + */ |
| 50 | +public class ErrorsArgumentResolverTests { |
| 51 | + |
| 52 | + private ErrorsMethodArgumentResolver resolver ; |
| 53 | + |
| 54 | + private final BindingContext bindingContext = new BindingContext(); |
| 55 | + |
| 56 | + private BindingResult bindingResult; |
| 57 | + |
| 58 | + private ServerWebExchange exchange; |
| 59 | + |
| 60 | + private final ResolvableMethod testMethod = ResolvableMethod.onClass(this.getClass()).name("handle"); |
| 61 | + |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws Exception { |
| 65 | + this.resolver = new ErrorsMethodArgumentResolver(new ReactiveAdapterRegistry()); |
| 66 | + |
| 67 | + MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.POST, "/path"); |
| 68 | + MockServerHttpResponse response = new MockServerHttpResponse(); |
| 69 | + WebSessionManager manager = new MockWebSessionManager(); |
| 70 | + this.exchange = new DefaultServerWebExchange(request, response, manager); |
| 71 | + |
| 72 | + Foo foo = new Foo(); |
| 73 | + WebExchangeDataBinder binder = this.bindingContext.createDataBinder(this.exchange, foo, "foo"); |
| 74 | + this.bindingResult = binder.getBindingResult(); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void supports() throws Exception { |
| 80 | + |
| 81 | + MethodParameter parameter = parameter(forClass(Errors.class)); |
| 82 | + assertTrue(this.resolver.supportsParameter(parameter)); |
| 83 | + |
| 84 | + parameter = parameter(forClass(BindingResult.class)); |
| 85 | + assertTrue(this.resolver.supportsParameter(parameter)); |
| 86 | + |
| 87 | + parameter = parameter(forClassWithGenerics(Mono.class, Errors.class)); |
| 88 | + assertFalse(this.resolver.supportsParameter(parameter)); |
| 89 | + |
| 90 | + parameter = parameter(forClass(String.class)); |
| 91 | + assertFalse(this.resolver.supportsParameter(parameter)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void resolveErrors() throws Exception { |
| 96 | + testResolve(this.bindingResult); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void resolveErrorsMono() throws Exception { |
| 101 | + MonoProcessor<BindingResult> monoProcessor = MonoProcessor.create(); |
| 102 | + monoProcessor.onNext(this.bindingResult); |
| 103 | + testResolve(monoProcessor); |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = IllegalArgumentException.class) |
| 107 | + public void resolveErrorsAfterMonoModelAttribute() throws Exception { |
| 108 | + MethodParameter parameter = parameter(forClass(BindingResult.class)); |
| 109 | + this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange).blockMillis(5000); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + private void testResolve(Object bindingResult) { |
| 114 | + |
| 115 | + String key = BindingResult.MODEL_KEY_PREFIX + "foo"; |
| 116 | + this.bindingContext.getModel().asMap().put(key, bindingResult); |
| 117 | + |
| 118 | + MethodParameter parameter = parameter(forClass(Errors.class)); |
| 119 | + |
| 120 | + Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) |
| 121 | + .blockMillis(5000); |
| 122 | + |
| 123 | + assertSame(this.bindingResult, actual); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + private MethodParameter parameter(ResolvableType type) { |
| 128 | + return this.testMethod.resolveParam(type); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + private static class Foo { |
| 133 | + |
| 134 | + private String name; |
| 135 | + |
| 136 | + public Foo() { |
| 137 | + } |
| 138 | + |
| 139 | + public Foo(String name) { |
| 140 | + this.name = name; |
| 141 | + } |
| 142 | + |
| 143 | + public String getName() { |
| 144 | + return name; |
| 145 | + } |
| 146 | + |
| 147 | + public void setName(String name) { |
| 148 | + this.name = name; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @SuppressWarnings("unused") |
| 153 | + void handle( |
| 154 | + @ModelAttribute Foo foo, |
| 155 | + Errors errors, |
| 156 | + @ModelAttribute Mono<Foo> fooMono, |
| 157 | + BindingResult bindingResult, |
| 158 | + Mono<Errors> errorsMono, |
| 159 | + String string) {} |
| 160 | + |
| 161 | +} |
0 commit comments