URLTool { public Map<String, List<String>> parseQuery(String query) { Map<String, List<String>> queryParams = new LinkedHashMap<>(); if (query != null) { for (NameValuePair params : URLEncodedUtils.parse(query, StandardCharsets.UTF_8)) { String name = params.getName(); List<String> values = queryParams.get(name); if (values == null) { values = new ArrayList<>(); queryParams.put(name, values); } values.add(params.getValue()); } } return queryParams; }  Map<String, List<String>> parseQuery(String query);  }
@Test void parseQuerySimple() { Map<String, ?> query = tool.parseQuery("a=b%26c"); assertEquals(Collections.singleton("a"), query.keySet(), "should have one parameter"); assertEquals(Collections.singletonList("b&c"), query.get("a"), "should have one parameter value"); }
@Test void parseQueryMultipleValues() { Map<String, ?> query = tool.parseQuery("a=b+c&a=b%3Dc&ab=a+%26+b"); assertEquals(2, query.size(), "should have two parameter"); assertEquals(Collections.singletonList("a & b"), query.get("ab"), "should have one parameter value for ab"); assertEquals(Arrays.asList(new String[] {"b c", "b=c"}), query.get("a"), "should have two parameter values for a"); }
@Test void parseQueryWhenInvalidURL() { Map<String, ?> query = tool.parseQuery("a=b ' onclick='foo"); assertEquals(Collections.singleton("a"), query.keySet(), "should have one parameter"); assertEquals(Collections.singletonList("b ' onclick='foo"), query.get("a"), "should have one parameter"); }
@Test void parseQueryWhenNull() { Map<String, ?> query = tool.parseQuery(null); assertNotNull(query, "null query results in empty map"); assertTrue(query.isEmpty(), "null query results in empty map"); }
@Test void preserveParameterOrder() { EscapeTool escapeTool = new EscapeTool(); String queryString = "x=5&x=4&r=3&a=2"; assertEquals(queryString, escapeTool.url(tool.parseQuery(queryString))); }