Package org.springframework.web.servlet.mvc.method.annotation

Source Code of org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinderTests

/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.mvc.method.annotation;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.servlet.HandlerMapping;

import static org.junit.Assert.*;

/**
* Test fixture for {@link ExtendedServletRequestDataBinder}.
*
* @author Rossen Stoyanchev
*/
public class ExtendedServletRequestDataBinderTests {

  private MockHttpServletRequest request;

  @Before
  public void setup() {
    this.request = new MockHttpServletRequest();
  }

  @Test
  public void createBinder() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<String, String>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);

    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);

    assertEquals("nameValue", target.getName());
    assertEquals(25, target.getAge());
  }

  @Test
  public void uriTemplateVarAndRequestParam() throws Exception {
    request.addParameter("age", "35");

    Map<String, String> uriTemplateVars = new HashMap<String, String>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);

    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);

    assertEquals("nameValue", target.getName());
    assertEquals(35, target.getAge());
  }

  @Test
  public void noUriTemplateVars() throws Exception {
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);

    assertEquals(null, target.getName());
    assertEquals(0, target.getAge());
  }

}
TOP

Related Classes of org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinderTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.