org.springframework.web.bind.support.ConfigurableWebBindingInitializer Java Examples

The following examples show how to use org.springframework.web.bind.support.ConfigurableWebBindingInitializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamList() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, List.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123", "456");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(Arrays.asList("123", "456"), ((Optional) result).get());
}
 
Example #2
Source File: RequestAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolveOptional() {
	MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	assertFalse(((Optional<?>) mono.block()).isPresent());

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	BindingContext bindingContext = new BindingContext(initializer);

	Foo foo = new Foo();
	this.exchange.getAttributes().put("foo", foo);
	mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	Optional<?> optional = (Optional<?>) mono.block();
	assertTrue(optional.isPresent());
	assertSame(foo, optional.get());
}
 
Example #3
Source File: DelegatingWebFluxConfigurationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
	ReactiveAdapterRegistry reactiveAdapterRegistry = delegatingConfig.webFluxAdapterRegistry();
	ServerCodecConfigurer serverCodecConfigurer = delegatingConfig.serverCodecConfigurer();
	FormattingConversionService formattingConversionService = delegatingConfig.webFluxConversionService();
	Validator validator = delegatingConfig.webFluxValidator();

	ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)
			this.delegatingConfig.requestMappingHandlerAdapter(reactiveAdapterRegistry, serverCodecConfigurer,
					formattingConversionService, validator).getWebBindingInitializer();

	verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
	verify(webFluxConfigurer).getValidator();
	verify(webFluxConfigurer).getMessageCodesResolver();
	verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
	verify(webFluxConfigurer).configureArgumentResolvers(any());

	assertNotNull(initializer);
	assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
	assertSame(formatterRegistry.getValue(), initializer.getConversionService());
	assertEquals(13, codecsConfigurer.getValue().getReaders().size());
}
 
Example #4
Source File: SessionAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void resolveOptional() {
	MethodParameter param = initMethodParameter(3);
	Optional<Object> actual = (Optional<Object>) this.resolver
			.resolveArgument(param, new BindingContext(), this.exchange).block();

	assertNotNull(actual);
	assertFalse(actual.isPresent());

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	BindingContext bindingContext = new BindingContext(initializer);

	Foo foo = new Foo();
	given(this.session.getAttribute("foo")).willReturn(foo);
	actual = (Optional<Object>) this.resolver.resolveArgument(param, bindingContext, this.exchange).block();

	assertNotNull(actual);
	assertTrue(actual.isPresent());
	assertSame(foo, actual.get());
}
 
Example #5
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12903
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}
 
Example #7
Source File: RequestHeaderMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example #8
Source File: RequestMappingHandlerAdapterIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setValidator(new StubValidator());

	List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));

	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();

	handlerAdapter = new RequestMappingHandlerAdapter();
	handlerAdapter.setWebBindingInitializer(bindingInitializer);
	handlerAdapter.setCustomArgumentResolvers(customResolvers);
	handlerAdapter.setApplicationContext(context);
	handlerAdapter.setBeanFactory(context.getBeanFactory());
	handlerAdapter.afterPropertiesSet();

	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();

	request.setMethod("POST");

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
 
Example #9
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext context) {
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			context.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}
 
Example #10
Source File: Spr7766Tests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@Deprecated
public void test() throws Exception {
	AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
	ConfigurableWebBindingInitializer binder = new ConfigurableWebBindingInitializer();
	GenericConversionService service = new DefaultConversionService();
	service.addConverter(new ColorConverter());
	binder.setConversionService(service);
	adapter.setWebBindingInitializer(binder);
	Spr7766Controller controller = new Spr7766Controller();
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/colors");
	request.setPathInfo("/colors");
	request.addParameter("colors", "#ffffff,000000");
	MockHttpServletResponse response = new MockHttpServletResponse();
	adapter.handle(request, response, controller);
}
 
Example #11
Source File: ServletModelAttributeMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	processor = new ServletModelAttributeMethodProcessor(false);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	binderFactory = new ServletRequestDataBinderFactory(null, initializer);

	mavContainer = new ModelAndViewContainer();
	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
	testBeanModelAttr = new MethodParameter(method, 0);
	testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
 
Example #12
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void typeNestedSetBinding() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext context) {
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			csDef.getPropertyValues().add("converters", new TestBeanConverter());
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			context.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, NestedSetController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("testBeanSet", new String[] {"1", "2"});
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
 
Example #13
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(123, ((Optional) result).get());
}
 
Example #14
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamList() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, List.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123", "456");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(Arrays.asList("123", "456"), ((Optional) result).get());
}
 
Example #15
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolveOptionalMultipartFile() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
	MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
	request.addFile(expected);
	webRequest = new ServletWebRequest(request);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);

	assertTrue(result instanceof Optional);
	assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
 
Example #16
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123", "456");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get());
}
 
Example #17
Source File: RequestMappingHandlerAdapterIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setValidator(new StubValidator());

	List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<HandlerMethodArgumentResolver>();
	customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));

	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();

	handlerAdapter = new RequestMappingHandlerAdapter();
	handlerAdapter.setWebBindingInitializer(bindingInitializer);
	handlerAdapter.setCustomArgumentResolvers(customResolvers);
	handlerAdapter.setApplicationContext(context);
	handlerAdapter.setBeanFactory(context.getBeanFactory());
	handlerAdapter.afterPropertiesSet();

	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();

	request.setMethod("POST");

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
 
Example #18
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123", "456");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get());
}
 
Example #19
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(123, ((Optional) result).get());
}
 
Example #20
Source File: DelegatingWebFluxConfigurationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));

	ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)
			this.delegatingConfig.requestMappingHandlerAdapter().getWebBindingInitializer();

	verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
	verify(webFluxConfigurer).getValidator();
	verify(webFluxConfigurer).getMessageCodesResolver();
	verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
	verify(webFluxConfigurer).configureArgumentResolvers(any());

	assertNotNull(initializer);
	assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
	assertSame(formatterRegistry.getValue(), initializer.getConversionService());
	assertEquals(13, codecsConfigurer.getValue().getReaders().size());
}
 
Example #21
Source File: DelegatingWebMvcConfigurationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
	RequestMappingHandlerAdapter adapter = this.delegatingConfig.requestMappingHandlerAdapter();

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).addFormatters(conversionService.capture());
	verify(webMvcConfigurer).addArgumentResolvers(resolvers.capture());
	verify(webMvcConfigurer).addReturnValueHandlers(handlers.capture());
	verify(webMvcConfigurer).configureAsyncSupport(asyncConfigurer.capture());

	assertNotNull(initializer);
	assertSame(conversionService.getValue(), initializer.getConversionService());
	assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
	assertEquals(0, resolvers.getValue().size());
	assertEquals(0, handlers.getValue().size());
	assertEquals(converters.getValue(), adapter.getMessageConverters());
	assertNotNull(asyncConfigurer);
}
 
Example #22
Source File: RequestAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolveOptional() throws Exception {
	MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	assertFalse(((Optional<?>) mono.block()).isPresent());

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	BindingContext bindingContext = new BindingContext(initializer);

	Foo foo = new Foo();
	this.exchange.getAttributes().put("foo", foo);
	mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	Optional<?> optional = (Optional<?>) mono.block();
	assertTrue(optional.isPresent());
	assertSame(foo, optional.get());
}
 
Example #23
Source File: WebMvcConfigurationSupportExtensionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void webBindingInitializer() throws Exception {
	RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter();

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();

	assertNotNull(initializer);

	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, "");
	initializer.getValidator().validate(null, bindingResult);
	assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode());

	String[] codes = initializer.getMessageCodesResolver().resolveMessageCodes("invalid", null);
	assertEquals("custom.invalid", codes[0]);
}
 
Example #24
Source File: PathVariableMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolveArgumentWrappedAsOptional() throws Exception {
	Map<String, String> uriTemplateVars = new HashMap<>();
	uriTemplateVars.put("name", "value");
	request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	@SuppressWarnings("unchecked")
	Optional<String> result = (Optional<String>)
			resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory);
	assertEquals("PathVariable not resolved correctly", "value", result.get());

	@SuppressWarnings("unchecked")
	Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(View.PATH_VARIABLES);
	assertNotNull(pathVars);
	assertEquals(1, pathVars.size());
	assertEquals(Optional.of("value"), pathVars.get("name"));
}
 
Example #25
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example #26
Source File: RequestParamMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptional() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(Optional.empty(), result);

	this.request.addParameter("name", "123");
	result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(123, ((Optional) result).get());
}
 
Example #27
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #28
Source File: RequestMappingHandlerAdapterIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setValidator(new StubValidator());

	List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));

	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();

	handlerAdapter = new RequestMappingHandlerAdapter();
	handlerAdapter.setWebBindingInitializer(bindingInitializer);
	handlerAdapter.setCustomArgumentResolvers(customResolvers);
	handlerAdapter.setApplicationContext(context);
	handlerAdapter.setBeanFactory(context.getBeanFactory());
	handlerAdapter.afterPropertiesSet();

	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();

	request.setMethod("POST");

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
 
Example #29
Source File: ServletModelAttributeMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.processor = new ServletModelAttributeMethodProcessor(false);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);

	this.testBeanModelAttr = new MethodParameter(method, 0);
	this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	this.testBeanWithOptionalModelAttr = new MethodParameter(method, 2);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());

	this.binderFactory = new ServletRequestDataBinderFactory(null, initializer);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request);
}
 
Example #30
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12903
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}