From 2788eaa1bfdc4ccc76c2dbfa670a2bf88cf33608 Mon Sep 17 00:00:00 2001 From: Jiri Danek Date: Thu, 31 Mar 2016 23:18:17 +0200 Subject: [PATCH] Put parameters into query string in a predictable order Previously, the order of query parameters depended on the iteration order of items in a Set. This order is undefined for some Sets. Nevertheless, unit test for createQueryString expected that query parameters are in a particular order. --- .../apache/activemq/artemis/utils/uri/URISupport.java | 9 ++++++--- .../org/apache/activemq/artemis/utils/URIParserTest.java | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java index 7cbfeaa0a7..d03f2f0934 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java @@ -463,7 +463,7 @@ public class URISupport { /** * Given a key / value mapping, create and return a URI formatted query string that is valid and - * can be appended to a URI. + * can be appended to a URI. Query parameters in the string are sorted by key. * * @param options The Mapping that will create the new Query string. * @return a URI formatted query string. @@ -472,9 +472,12 @@ public class URISupport { public static String createQueryString(Map options) throws URISyntaxException { try { if (options.size() > 0) { - StringBuffer rc = new StringBuffer(); + StringBuilder rc = new StringBuilder(); boolean first = true; - for (String key : options.keySet()) { + List keys = new ArrayList(); + keys.addAll(options.keySet()); + Collections.sort(keys); + for (String key : keys) { if (first) { first = false; } diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/URIParserTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/URIParserTest.java index 71a783344b..d4e9950811 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/URIParserTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/URIParserTest.java @@ -114,12 +114,12 @@ public class URIParserTest { System.out.println("queryString2: " + queryString); Assert.assertEquals("key1=value1", queryString); - query.put("key2", "value2"); + query.put("key3", "value3"); queryString = URISupport.createQueryString(query); System.out.println("queryString3: " + queryString); - Assert.assertEquals("key1=value1&key2=value2", queryString); + Assert.assertEquals("key1=value1&key3=value3", queryString); - query.put("key3", "value3"); + query.put("key2", "value2"); queryString = URISupport.createQueryString(query); System.out.println("queryString4: " + queryString); Assert.assertEquals("key1=value1&key2=value2&key3=value3", queryString);