LocationParser { public static String getParameter(String search, String parameter) { String[] keyValues = search.substring(1).split("&"); for (String keyValue : keyValues) { String[] param = keyValue.split("=", 2); if (param[0].equals(parameter)) { if (param.length != 2) { return ""; } else { return param[1]; } } } return null; } private  LocationParser(); static String getParameter(String search, String parameter);  }
@Test public void testParameterParsing() { Assert.assertEquals(null, LocationParser.getParameter("?", "foo")); Assert.assertEquals(null, LocationParser.getParameter("?bar", "foo")); Assert.assertEquals("", LocationParser.getParameter("?foo", "foo")); Assert.assertEquals("", LocationParser.getParameter("?foo=", "foo")); Assert.assertEquals("bar", LocationParser.getParameter("?foo=bar", "foo")); Assert.assertEquals("bar", LocationParser.getParameter("?foo=bar&", "foo")); Assert.assertEquals("", LocationParser.getParameter("?foo&bar", "foo")); Assert.assertEquals("", LocationParser.getParameter("?bar&foo", "foo")); Assert.assertEquals("", LocationParser.getParameter("?bar&foo=", "foo")); Assert.assertEquals("a", LocationParser.getParameter("?bar&foo=a", "foo")); }