EscapeTool extends org.apache.velocity.tools.generic.EscapeTool { public String url(Map<String, ?> parametersMap) { StringBuilder queryStringBuilder = new StringBuilder(); for (Map.Entry<String, ?> entry : parametersMap.entrySet()) { if (entry.getKey() == null) { continue; } String cleanKey = this.url(entry.getKey()); Object mapValues = entry.getValue(); if (mapValues != null && mapValues.getClass().isArray()) { Object[] values = (Object[]) mapValues; for (Object value : values) { addQueryStringPair(cleanKey, value, queryStringBuilder); } } else if (mapValues != null && Collection.class.isAssignableFrom(mapValues.getClass())) { Collection<?> values = (Collection<?>) mapValues; for (Object value : values) { addQueryStringPair(cleanKey, value, queryStringBuilder); } } else { addQueryStringPair(cleanKey, mapValues, queryStringBuilder); } } return queryStringBuilder.toString(); }  @Override String xml(Object content); String json(Object string); String quotedPrintable(Object content); String q(Object content); String b(Object content); String url(Map<String, ?> parametersMap); String css(String identifier); @Override String url(Object string);  }
@Test void url() { HashMap<String, String> map = new LinkedHashMap<>(); map.put("hello", "world"); map.put(null, "value"); map.put("B& B", "yes"); map.put("empty", null); assertEquals("hello=world&B%26%20B=yes&empty=", this.tool.url(map)); }
@Test void urlWithDouble() { HashMap<String, Double> map = new LinkedHashMap<>(); map.put("A&A", 1.5); map.put("B&B", 1.2); assertEquals("A%26A=1.5&B%26B=1.2", this.tool.url(map)); }
@Test void urlWithArray() { HashMap<String, String[]> map = new HashMap<>(); String[] array = {"M&M", null, "Astronomy&Astrophysics"}; map.put("couple", array); assertEquals("couple=M%26M&couple=&couple=Astronomy%26Astrophysics", this.tool.url(map)); }
@Test void urlWithCollection() { HashMap<String, ArrayList<String>> map = new HashMap<>(); ArrayList<String> collection1 = new ArrayList<>(); collection1.add("test"); map.put("alice", collection1); ArrayList<String> collection2 = new ArrayList<>(); collection2.add(null); collection2.add("t&t"); collection2.add("R&D"); map.put("bob", collection2); assertEquals("bob=&bob=t%26t&bob=R%26D&alice=test", this.tool.url(map)); }