ServletRequestParameterPropertyValuesTestSuite.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.bind;
  17. import java.util.Arrays;
  18. import org.springframework.beans.AbstractPropertyValuesTests;
  19. import org.springframework.mock.web.MockHttpServletRequest;
  20. /**
  21.  * @author Rod Johnson
  22.  */
  23. public class ServletRequestParameterPropertyValuesTestSuite extends AbstractPropertyValuesTests {
  24. public void testNoPrefix() throws Exception {
  25. MockHttpServletRequest request = new MockHttpServletRequest();
  26. request.addParameter("forname", "Tony");
  27. request.addParameter("surname", "Blair");
  28. request.addParameter("age", "" + 50);
  29. ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
  30. testTony(pvs);
  31. }
  32. public void testPrefix() throws Exception {
  33. MockHttpServletRequest request = new MockHttpServletRequest();
  34. request.addParameter("test_forname", "Tony");
  35. request.addParameter("test_surname", "Blair");
  36. request.addParameter("test_age", "" + 50);
  37. ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
  38. assertTrue("Didn't fidn normal when given prefix", !pvs.contains("forname"));
  39. assertTrue("Did treat prefix as normal when not given prefix", pvs.contains("test_forname"));
  40. pvs = new ServletRequestParameterPropertyValues(request, "test");
  41. testTony(pvs);
  42. }
  43. public void testNoParameters() throws Exception {
  44. MockHttpServletRequest request = new MockHttpServletRequest();
  45. ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
  46. assertTrue("Found no parameters", pvs.getPropertyValues().length == 0);
  47. }
  48. public void testMultipleValuesForParameter() throws Exception {
  49. MockHttpServletRequest request = new MockHttpServletRequest();
  50. String[] original = new String[] {"Tony", "Rod"};
  51. request.addParameter("forname", original);
  52. ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
  53. assertTrue("Found 1 parameter", pvs.getPropertyValues().length == 1);
  54. assertTrue("Found array value", pvs.getPropertyValue("forname").getValue() instanceof String[]);
  55. String[] values = (String[]) pvs.getPropertyValue("forname").getValue();
  56. assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original));
  57. }
  58. }