ServletUtils { public static String getSafeParameter(HttpServletRequest request, String key, String defaultValue) { String ret = request.getParameter(key); if (ret == null) return defaultValue; return ret; } private  ServletUtils(); static String getSafeParameterDecoded(HttpServletRequest request, String key, String defaultValue); static String getSafeParameter(HttpServletRequest request, String key, String defaultValue); static int getSafeParameter(HttpServletRequest request, String key, int defaultValue); static double getSafeParameter(HttpServletRequest request, String key, double defaultValue); static String getCookieValueByName(HttpServletRequest request, String name, String defaultValue); static int getCookieValueByName(HttpServletRequest request, String name, int defaultValue); static Cookie getCookieByName(HttpServletRequest request, String name, String defaultValue); static Vector<String> getAllCookiesStartWith(HttpServletRequest request, String startString); static int getCookieCount(HttpServletRequest request); static String stringToHTMLCode(String string); static String getSessionId(HttpServletRequest request, String defaultId); static String getIP(HttpServletRequest request);  }
@Test public void testGetSafeParameterString() { final HttpServletRequest request = context.mock(HttpServletRequest.class); context.checking(new Expectations() { { Sequence getParams = new NamedSequence("getParams"); for (Map.Entry<String, String> entry : stringParams.entrySet()) { oneOf(request).getParameter(entry.getKey()); inSequence(getParams); will(returnValue(entry.getValue())); } } }); String value = ServletUtils.getSafeParameter(request, "param1", "default"); assertThat("value1", equalTo(value)); value = ServletUtils.getSafeParameter(request, "param2", "default"); assertThat("value2", equalTo(value)); value = ServletUtils.getSafeParameter(request, "param3", "default"); assertThat("default", equalTo(value)); }
@Test public void testGetSafeParameterInt() { final HttpServletRequest request = context.mock(HttpServletRequest.class); context.checking(new Expectations() { { Sequence getParams = new NamedSequence("getParams"); for (Map.Entry<String, String> entry : intParams.entrySet()) { oneOf(request).getParameter(entry.getKey()); inSequence(getParams); will(returnValue(entry.getValue())); } } }); int value = ServletUtils.getSafeParameter(request, "param1", 0); assertEquals(1, value); value = ServletUtils.getSafeParameter(request, "param2", 0); assertEquals(2, value); value = ServletUtils.getSafeParameter(request, "param3", 0); assertEquals(0, value); }