use of org.springframework.validation.BindingResult in project spring-mvc-31-demo by rstoyanchev.
the class AccountControllerTests method testSaveFail.
@Test
public void testSaveFail() {
Account account = new Account();
BindingResult result = new BeanPropertyBindingResult(account, "account");
AccountManager mgr = createMock(AccountManager.class);
mgr.saveOrUpdate(account);
replay(mgr);
AccountController contrlr = new AccountController(mgr);
String view = contrlr.save(account, result);
assertEquals("redirect:accounts", view);
verify(mgr);
}
use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class MappingJackson2XmlViewTests method renderSimpleBeanWithJsonView.
@Test
public void renderSimpleBeanWithJsonView() throws Exception {
Object bean = new TestBeanSimple();
Map<String, Object> model = new HashMap<>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", bean);
model.put(JsonView.class.getName(), MyJacksonView1.class);
view.setUpdateContentLength(true);
view.render(model, request, response);
String content = response.getContentAsString();
assertThat(content.length() > 0).isTrue();
assertThat(response.getContentLength()).isEqualTo(content.length());
assertThat(content.contains("foo")).isTrue();
assertThat(content.contains("boo")).isFalse();
assertThat(content.contains(JsonView.class.getName())).isFalse();
}
use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class MappingJackson2XmlViewTests method renderSimpleMap.
@Test
public void renderSimpleMap() throws Exception {
Map<String, Object> model = new HashMap<>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar");
view.setUpdateContentLength(true);
view.render(model, request, response);
assertThat(response.getHeader("Cache-Control")).isEqualTo("no-store");
MediaType mediaType = MediaType.parseMediaType(response.getContentType());
assertThat(mediaType.isCompatibleWith(MediaType.parseMediaType(MappingJackson2XmlView.DEFAULT_CONTENT_TYPE))).isTrue();
String jsonResult = response.getContentAsString();
assertThat(jsonResult.length() > 0).isTrue();
assertThat(response.getContentLength()).isEqualTo(jsonResult.length());
validateResult();
}
use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class ModelAttributeMethodArgumentResolver method resolveArgument.
@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
ResolvableType type = ResolvableType.forMethodParameter(parameter);
Class<?> resolvedType = type.resolve();
ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
ResolvableType valueType = (adapter != null ? type.getGeneric() : type);
Assert.state(adapter == null || !adapter.isMultiValue(), () -> getClass().getSimpleName() + " does not support multi-value reactive type wrapper: " + parameter.getGenericParameterType());
String name = ModelInitializer.getNameForParameter(parameter);
Mono<?> valueMono = prepareAttributeMono(name, valueType, context, exchange);
// unsafe(): we're intercepting, already serialized Publisher signals
Sinks.One<BindingResult> bindingResultSink = Sinks.unsafe().one();
Map<String, Object> model = context.getModel().asMap();
model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResultSink.asMono());
return valueMono.flatMap(value -> {
WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange)).doOnError(bindingResultSink::tryEmitError).doOnSuccess(aVoid -> {
validateIfApplicable(binder, parameter);
BindingResult bindingResult = binder.getBindingResult();
model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResult);
model.put(name, value);
// Ignore result: serialized and buffered (should never fail)
bindingResultSink.tryEmitValue(bindingResult);
}).then(Mono.fromCallable(() -> {
BindingResult errors = binder.getBindingResult();
if (adapter != null) {
return adapter.fromPublisher(errors.hasErrors() ? Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono);
} else {
if (errors.hasErrors() && !hasErrorsArgument(parameter)) {
throw new WebExchangeBindException(parameter, errors);
}
return value;
}
}));
});
}
use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class ErrorsMethodArgumentResolverTests method resolve.
@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);
}
Aggregations