@Test public void testLongParameter() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param1", "5"); request.addParameter("param2", "e"); request.addParameter("paramEmpty", ""); assertEquals(ServletRequestUtils.getLongParameter(request, "param1"), new Long(5L)); assertEquals(ServletRequestUtils.getLongParameter(request, "param1", 6L), 5L); assertEquals(ServletRequestUtils.getRequiredIntParameter(request, "param1"), 5L); assertEquals(ServletRequestUtils.getLongParameter(request, "param2", 6L), 6L); try { ServletRequestUtils.getRequiredLongParameter(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } assertEquals(ServletRequestUtils.getLongParameter(request, "param3"), null); assertEquals(ServletRequestUtils.getLongParameter(request, "param3", 6L), 6L); try { ServletRequestUtils.getRequiredLongParameter(request, "param3"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } try { ServletRequestUtils.getRequiredLongParameter(request, "paramEmpty"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
@Test public void testDoubleParameter() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param1", "5.5"); request.addParameter("param2", "e"); request.addParameter("paramEmpty", ""); assertTrue(ServletRequestUtils.getDoubleParameter(request, "param1").equals(new Double(5.5))); assertTrue(ServletRequestUtils.getDoubleParameter(request, "param1", 6.5) == 5.5); assertTrue(ServletRequestUtils.getRequiredDoubleParameter(request, "param1") == 5.5); assertTrue(ServletRequestUtils.getDoubleParameter(request, "param2", 6.5) == 6.5); try { ServletRequestUtils.getRequiredDoubleParameter(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } assertTrue(ServletRequestUtils.getDoubleParameter(request, "param3") == null); assertTrue(ServletRequestUtils.getDoubleParameter(request, "param3", 6.5) == 6.5); try { ServletRequestUtils.getRequiredDoubleParameter(request, "param3"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } try { ServletRequestUtils.getRequiredDoubleParameter(request, "paramEmpty"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
@Test public void testLongParameters() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("param", new String[] {"1", "2", "3"}); request.setParameter("param2", "0"); request.setParameter("param2", "1"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); long[] array = new long[] {1L, 2L, 3L}; long[] values = ServletRequestUtils.getRequiredLongParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } try { ServletRequestUtils.getRequiredLongParameters(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } request.setParameter("param2", new String[] {"1", "2"}); values = ServletRequestUtils.getRequiredLongParameters(request, "param2"); assertEquals(2, values.length); assertEquals(1, values[0]); assertEquals(2, values[1]); request.removeParameter("param2"); try { ServletRequestUtils.getRequiredLongParameters(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
@Test public void testIntParameters() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param", new String[] {"1", "2", "3"}); request.addParameter("param2", "1"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); int[] array = new int[] {1, 2, 3}; int[] values = ServletRequestUtils.getRequiredIntParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } try { ServletRequestUtils.getRequiredIntParameters(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
@Test public void testBooleanParameters() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param", new String[] {"true", "yes", "off", "1", "bogus"}); request.addParameter("param2", "false"); request.addParameter("param2", "true"); request.addParameter("param2", ""); boolean[] array = new boolean[] {true, true, false, true, false}; boolean[] values = ServletRequestUtils.getRequiredBooleanParameters(request, "param"); assertEquals(array.length, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } array = new boolean[] {false, true, false}; values = ServletRequestUtils.getRequiredBooleanParameters(request, "param2"); assertEquals(array.length, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } }
@Test public void testGetStringParameterWithDefaultValueHandlingIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); MockHttpServletRequest request = new MockHttpServletRequest(); StopWatch sw = new StopWatch(); sw.start(); for (int i = 0; i < 1000000; i++) { ServletRequestUtils.getStringParameter(request, "nonExistingParam", "defaultValue"); } sw.stop(); System.out.println(sw.getTotalTimeMillis()); assertTrue( "getStringParameter took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 250); }
@Test public void testStringParameter() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param1", "str"); request.addParameter("paramEmpty", ""); assertEquals("str", ServletRequestUtils.getStringParameter(request, "param1")); assertEquals("str", ServletRequestUtils.getStringParameter(request, "param1", "string")); assertEquals("str", ServletRequestUtils.getRequiredStringParameter(request, "param1")); assertEquals(null, ServletRequestUtils.getStringParameter(request, "param3")); assertEquals("string", ServletRequestUtils.getStringParameter(request, "param3", "string")); assertNull(ServletRequestUtils.getStringParameter(request, "param3", null)); try { ServletRequestUtils.getRequiredStringParameter(request, "param3"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } assertEquals("", ServletRequestUtils.getStringParameter(request, "paramEmpty")); assertEquals("", ServletRequestUtils.getRequiredStringParameter(request, "paramEmpty")); }
@Test public void testBooleanParameter() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param1", "true"); request.addParameter("param2", "e"); request.addParameter("param4", "yes"); request.addParameter("param5", "1"); request.addParameter("paramEmpty", ""); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param1").equals(Boolean.TRUE)); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param1", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param1")); assertFalse(ServletRequestUtils.getBooleanParameter(request, "param2", true)); assertFalse(ServletRequestUtils.getRequiredBooleanParameter(request, "param2")); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param3") == null); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param3", true)); try { ServletRequestUtils.getRequiredBooleanParameter(request, "param3"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } assertTrue(ServletRequestUtils.getBooleanParameter(request, "param4", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param4")); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param5", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param5")); assertFalse(ServletRequestUtils.getRequiredBooleanParameter(request, "paramEmpty")); }