Search in sources :

Example 1 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder in project thymeleaf-tests by thymeleaf.

the class DecoupledSTSMTest method test.

private static void test(final String templateName, final String expectedResultLocation, final Map<String, Object> vars) throws Exception {
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, Locale.US);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
    appCtx.setServletContext(mockServletContext);
    appCtx.setConfigLocation("classpath:templateparser/decoupled/stsm/applicationContext.xml");
    appCtx.refresh();
    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    // can be null!
    final ConversionService conversionService = getConversionService(appCtx);
    mockRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    final ServletWebRequest servletWebRequest = new ServletWebRequest(mockRequest, mockResponse);
    RequestContextHolder.setRequestAttributes(servletWebRequest);
    final RequestContext requestContext = new RequestContext(mockRequest, mockResponse, mockServletContext, vars);
    vars.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext);
    vars.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext);
    vars.put(SpringContextVariableNames.THYMELEAF_REQUEST_CONTEXT, new SpringWebMvcThymeleafRequestContext(requestContext, mockRequest));
    final List<String> variableNames = new ArrayList<String>(vars.keySet());
    for (final String variableName : variableNames) {
        final Object bindingObject = vars.get(variableName);
        if (isBindingCandidate(variableName, bindingObject)) {
            final String bindingVariableName = BindingResult.MODEL_KEY_PREFIX + variableName;
            if (!vars.containsKey(bindingVariableName)) {
                final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName);
                dataBinder.setConversionService(conversionService);
                vars.put(bindingVariableName, dataBinder.getBindingResult());
            }
        }
    }
    final WebContext ctx = new WebContext(mockRequest, mockResponse, mockServletContext, Locale.US);
    for (final Map.Entry<String, Object> entry : vars.entrySet()) {
        ctx.setVariable(entry.getKey(), entry.getValue());
    }
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templateparser/decoupled/stsm/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setUseDecoupledLogic(true);
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setLinkBuilder(new StandardLinkBuilder() {

        @Override
        protected String computeContextPath(final IExpressionContext context, final String base, final Map<String, Object> parameters) {
            return "";
        }
    });
    appCtx.getAutowireCapableBeanFactory().initializeBean(templateEngine, "templateEngine");
    final String result = templateEngine.process(templateName, ctx);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(DecoupledSTSMTest.class).getResourceAsStream(expectedResultLocation), "ISO-8859-1", true);
    Assert.assertEquals(ResourceUtils.normalize(expected), ResourceUtils.normalize(result));
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) WebContext(org.thymeleaf.context.WebContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDataBinder(org.springframework.web.bind.WebDataBinder) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) IExpressionContext(org.thymeleaf.context.IExpressionContext) ConversionService(org.springframework.core.convert.ConversionService) ServletContext(javax.servlet.ServletContext) RequestContext(org.springframework.web.servlet.support.RequestContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.

the class AbstractRequestAttributesArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    WebDataBinder dataBinder = new WebRequestDataBinder(null);
    dataBinder.setConversionService(new DefaultConversionService());
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
    MethodParameter param = initMethodParameter(3);
    Object actual = testResolveArgument(param, factory);
    assertThat(actual).isNotNull();
    assertThat(actual.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional<?>) actual).isPresent()).isFalse();
    Foo foo = new Foo();
    this.webRequest.setAttribute("foo", foo, getScope());
    actual = testResolveArgument(param, factory);
    assertThat(actual).isNotNull();
    assertThat(actual.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional<?>) actual).isPresent()).isTrue();
    assertThat(((Optional<?>) actual).get()).isSameAs(foo);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 3 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.

the class InitBinderBindingContextTests method createBinderWithAttrName.

@Test
public void createBinderWithAttrName() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
    BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, "foo");
    assertThat(dataBinder.getDisallowedFields()).isNotNull();
    assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id");
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.jupiter.api.Test)

Example 4 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.

the class InitBinderBindingContextTests method createBinder.

@Test
public void createBinder() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
    BindingContext context = createBindingContext("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
    assertThat(dataBinder.getDisallowedFields()).isNotNull();
    assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id");
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.jupiter.api.Test)

Example 5 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder in project spring-framework by spring-projects.

the class InitBinderDataBinderFactoryTests method createBinderWithAttrNameNoMatch.

@Test
public void createBinderWithAttrNameNoMatch() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "invalidName");
    assertThat(dataBinder.getDisallowedFields()).isNull();
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Aggregations

WebDataBinder (org.springframework.web.bind.WebDataBinder)43 Test (org.junit.jupiter.api.Test)23 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)16 MethodParameter (org.springframework.core.MethodParameter)7 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)6 BindingContext (org.springframework.web.reactive.BindingContext)6 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ConversionService (org.springframework.core.convert.ConversionService)4 Nullable (org.springframework.lang.Nullable)4 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)4 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)4 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 ArrayList (java.util.ArrayList)2 TypeMismatchException (org.springframework.beans.TypeMismatchException)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 BindException (org.springframework.validation.BindException)2