|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 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 java.time.Duration; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import org.springframework.core.MethodParameter; |
| 24 | +import org.springframework.core.ReactiveAdapterRegistry; |
| 25 | +import org.springframework.mock.web.test.server.MockServerWebExchange; |
| 26 | +import org.springframework.ui.Model; |
| 27 | +import org.springframework.ui.ModelMap; |
| 28 | +import org.springframework.web.method.ResolvableMethod; |
| 29 | +import org.springframework.web.reactive.BindingContext; |
| 30 | +import org.springframework.web.server.ServerWebExchange; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertSame; |
| 34 | +import static org.junit.Assert.assertTrue; |
| 35 | +import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get; |
| 36 | + |
| 37 | +/** |
| 38 | + * Unit tests for {@link ModelArgumentResolver}. |
| 39 | + * @author Rossen Stoyanchev |
| 40 | + */ |
| 41 | +public class ModelArgumentResolverTests { |
| 42 | + |
| 43 | + private final ModelArgumentResolver resolver = new ModelArgumentResolver(new ReactiveAdapterRegistry()); |
| 44 | + |
| 45 | + private final ServerWebExchange exchange = MockServerWebExchange.from(get("/")); |
| 46 | + |
| 47 | + private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build(); |
| 48 | + |
| 49 | + |
| 50 | + @Test |
| 51 | + public void supportsParameter() throws Exception { |
| 52 | + assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Model.class))); |
| 53 | + assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Map.class, String.class, Object.class))); |
| 54 | + assertTrue(this.resolver.supportsParameter(this.testMethod.arg(ModelMap.class))); |
| 55 | + assertFalse(this.resolver.supportsParameter(this.testMethod.arg(Object.class))); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void resolveArgument() throws Exception { |
| 60 | + testResolveArgument(this.testMethod.arg(Model.class)); |
| 61 | + testResolveArgument(this.testMethod.arg(Map.class, String.class, Object.class)); |
| 62 | + testResolveArgument(this.testMethod.arg(ModelMap.class)); |
| 63 | + } |
| 64 | + |
| 65 | + private void testResolveArgument(MethodParameter parameter) { |
| 66 | + BindingContext context = new BindingContext(); |
| 67 | + Object result = this.resolver.resolveArgument(parameter, context, this.exchange).block(Duration.ZERO); |
| 68 | + assertSame(context.getModel(), result); |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("unused") |
| 72 | + void handle(Model model, Map<String, Object> map, ModelMap modelMap, Object object) {} |
| 73 | + |
| 74 | +} |
0 commit comments