@Test public void createAndBindToMono() throws Exception { MethodParameter parameter = this.testMethod .annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); testBindFoo("fooMono", parameter, mono -> { assertTrue(mono.getClass().getName(), mono instanceof Mono); Object value = ((Mono<?>) mono).block(Duration.ofSeconds(5)); assertEquals(Foo.class, value.getClass()); return (Foo) value; }); }
@Test public void validationError() throws Exception { MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); testValidationError(parameter, Function.identity()); }
@Test public void supportsWithDefaultResolution() throws Exception { ModelAttributeMethodArgumentResolver resolver = new ModelAttributeMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance(), true); MethodParameter param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(String.class); assertFalse(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, String.class); assertFalse(resolver.supportsParameter(param)); }
@Test public void bindExistingMono() throws Exception { Foo foo = new Foo(); foo.setName("Jim"); this.bindContext.getModel().addAttribute("fooMono", Mono.just(foo)); MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); testBindFoo("foo", parameter, value -> { assertEquals(Foo.class, value.getClass()); return (Foo) value; }); assertSame(foo, this.bindContext.getModel().asMap().get("foo")); }
@Test // SPR-8561 public void resolveSimpleTypeParamToNull() throws Exception { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertNull(result); }
@Test public void bindExisting() throws Exception { Foo foo = new Foo(); foo.setName("Jim"); this.bindContext.getModel().addAttribute(foo); MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); testBindFoo("foo", parameter, value -> { assertEquals(Foo.class, value.getClass()); return (Foo) value; }); assertSame(foo, this.bindContext.getModel().asMap().get("foo")); }
@Test public void bindExistingSingle() throws Exception { Foo foo = new Foo(); foo.setName("Jim"); this.bindContext.getModel().addAttribute("fooSingle", Single.just(foo)); MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); testBindFoo("foo", parameter, value -> { assertEquals(Foo.class, value.getClass()); return (Foo) value; }); assertSame(foo, this.bindContext.getModel().asMap().get("foo")); }
@Test public void resolveEmptyValueWithoutDefault() throws Exception { request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("", result); }
@Test public void resolveMultipartFileListNotannot() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); MultipartFile expected1 = new MockMultipartFile("multipartFileList", "Hello World 1".getBytes()); MultipartFile expected2 = new MockMultipartFile("multipartFileList", "Hello World 2".getBytes()); request.addFile(expected1); request.addFile(expected2); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod .annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof List); assertEquals(Arrays.asList(expected1, expected2), result); }
@Test public void resolveSimpleTypeParam() throws Exception { request.setParameter("stringNotAnnot", "plainValue"); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof String); assertEquals("plainValue", result); }
@Test public void doesNotSupportParameterWithDefaultResolutionTurnedOff() { ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, false); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); assertFalse(this.resolver.supportsParameter(param)); }
@Test // SPR-8561 public void resolveSimpleTypeParamToNull() { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); assertNull(resolve(param, MockServerWebExchange.from(MockServerHttpRequest.get("/")))); }
@Test public void resolveMultipartFileNotAnnot() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes()); request.addFile(expected); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof MultipartFile); assertEquals("Invalid result", expected, result); }
@Test public void resolveSimpleTypeParam() { MockServerHttpRequest request = MockServerHttpRequest.get("/path?stringNotAnnot=plainValue").build(); ServerWebExchange exchange = MockServerWebExchange.from(request); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolve(param, exchange); assertEquals("plainValue", result); }
@Test public void resolveEmptyValueWithoutDefault() { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); MockServerHttpRequest request = MockServerHttpRequest.get("/path?stringNotAnnot=").build(); assertEquals("", resolve(param, MockServerWebExchange.from(request))); }
@Test public void supports() throws Exception { ModelAttributeMethodArgumentResolver resolver = new ModelAttributeMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance(), false); MethodParameter param = this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); assertFalse(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); assertFalse(resolver.supportsParameter(param)); }
@Test public void supports() throws Exception { MethodParameter param; param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(RequestBody.class).arg(String.class); assertFalse(this.resolver.supportsParameter(param)); }
@Test // SPR-10578 public void missingRequestParamEmptyValueConvertedToNull() throws Exception { WebDataBinder binder = new WebRequestDataBinder(null); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder); request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory); assertNull(arg); }
@Test public void supportsParameter() throws Exception { assertTrue(this.resolver.supportsParameter( this.testMethod.annot(requestAttribute().noName()).arg(Foo.class))); // SPR-16158 assertTrue(this.resolver.supportsParameter( this.testMethod.annotPresent(RequestAttribute.class).arg(Mono.class, Foo.class))); assertFalse(this.resolver.supportsParameter( this.testMethod.annotNotPresent(RequestAttribute.class).arg())); }
@Test public void supportsParameter() { MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, String.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class, String.class, String.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annot(requestParam().name("name")).arg(Map.class, String.class, String.class); assertFalse(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(RequestParam.class).arg(Map.class, String.class, String.class); assertFalse(resolver.supportsParameter(param)); }