You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ta...@apache.org on 2017/01/06 17:38:34 UTC
qpid-jms git commit: QPIDJMS-245 Ensure that encoded values on the
URI are handled properly
Repository: qpid-jms
Updated Branches:
refs/heads/master 84c95aa1e -> 8485ada97
QPIDJMS-245 Ensure that encoded values on the URI are handled properly
In some cases the parameters where being encoded or decoded twice or
even an unbalanced number of times leading to corruption of the original
values. Refactor a bit to ensure that we encode values when we create
the URIs and decode when we parse out the options consistently. Adds
some additional tests to cover some of these cases.
Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/8485ada9
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/8485ada9
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/8485ada9
Branch: refs/heads/master
Commit: 8485ada970296635fd5868bd73938b27ba0f5a61
Parents: 84c95aa
Author: Timothy Bish <ta...@gmail.com>
Authored: Fri Jan 6 12:38:21 2017 -0500
Committer: Timothy Bish <ta...@gmail.com>
Committed: Fri Jan 6 12:38:21 2017 -0500
----------------------------------------------------------------------
.../apache/qpid/jms/JmsConnectionFactory.java | 12 +-
.../org/apache/qpid/jms/util/PropertyUtil.java | 178 +++++-----------
.../org/apache/qpid/jms/util/URISupport.java | 9 +-
.../jms/integration/SaslIntegrationTest.java | 31 +++
.../apache/qpid/jms/util/PropertyUtilTest.java | 208 ++++++++-----------
.../apache/qpid/jms/util/URISupportTest.java | 31 ++-
6 files changed, 210 insertions(+), 259 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
index 5ff596e..644f960 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
@@ -116,7 +116,7 @@ public class JmsConnectionFactory extends JNDIStorable implements ConnectionFact
}
public JmsConnectionFactory(String remoteURI) {
- this(createURI(remoteURI));
+ setRemoteURI(remoteURI);
}
public JmsConnectionFactory(URI remoteURI) {
@@ -386,14 +386,14 @@ public class JmsConnectionFactory extends JNDIStorable implements ConnectionFact
}
try {
- if (this.remoteURI.getQuery() != null) {
- Map<String, String> map = PropertyUtil.parseQuery(this.remoteURI.getQuery());
- applyURIOptions(map);
- this.remoteURI = PropertyUtil.replaceQuery(this.remoteURI, map);
- } else if (URISupport.isCompositeURI(this.remoteURI)) {
+ if (URISupport.isCompositeURI(this.remoteURI)) {
CompositeData data = URISupport.parseComposite(this.remoteURI);
applyURIOptions(data.getParameters());
this.remoteURI = data.toURI();
+ } else if (this.remoteURI.getRawQuery() != null) {
+ Map<String, String> map = PropertyUtil.parseQuery(this.remoteURI);
+ applyURIOptions(map);
+ this.remoteURI = PropertyUtil.replaceQuery(this.remoteURI, map);
}
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java
index cf0c465..83d370a 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/PropertyUtil.java
@@ -41,25 +41,32 @@ import javax.net.ssl.SSLContext;
*/
public class PropertyUtil {
+ private static final String UTF8 = "UTF-8";
+
/**
* Creates a URI from the original URI and the given parameters.
*
+ * The string values in the Map will be URL Encoded by this method which means that if an
+ * already encoded value is passed it will be double encoded resulting in corrupt values
+ * in the newly created URI.
+ *
* @param originalURI
* The URI whose current parameters are removed and replaced with the given remainder value.
- * @param params
- * The URI params that should be used to replace the current ones in the target.
+ * @param properties
+ * The URI properties that should be used to replace the current ones in the target.
*
* @return a new URI that matches the original one but has its query options replaced with
* the given ones.
*
* @throws URISyntaxException if the given URI is invalid.
*/
- public static URI replaceQuery(URI originalURI, Map<String, String> params) throws URISyntaxException {
- String s = createQueryString(params);
- if (s.length() == 0) {
- s = null;
+ public static URI replaceQuery(URI originalURI, Map<String, String> properties) throws URISyntaxException {
+ String queryString = createQueryString(properties);
+ if (queryString.length() == 0) {
+ queryString = null;
}
- return replaceQuery(originalURI, s);
+
+ return replaceQuery(originalURI, queryString);
}
/**
@@ -75,20 +82,40 @@ public class PropertyUtil {
* @throws URISyntaxException if the given URI is invalid.
*/
public static URI replaceQuery(URI uri, String query) throws URISyntaxException {
- String schemeSpecificPart = uri.getRawSchemeSpecificPart();
+ String uriAsString = uri.toString();
+
+ String fragment = null;
+
+ // Strip existing fragment if any
+ int hashMark = uriAsString.lastIndexOf("#");
+ // make sure hash mark is not within parentheses
+ if (hashMark < uriAsString.lastIndexOf(")")) {
+ hashMark = -1;
+ }
+ if (hashMark > 0) {
+ fragment = uriAsString.substring(hashMark);
+ uriAsString = uriAsString.substring(0, hashMark);
+ }
+
// strip existing query if any
- int questionMark = schemeSpecificPart.lastIndexOf("?");
+ int questionMark = uriAsString.lastIndexOf("?");
// make sure question mark is not within parentheses
- if (questionMark < schemeSpecificPart.lastIndexOf(")")) {
+ if (questionMark < uriAsString.lastIndexOf(")")) {
questionMark = -1;
}
if (questionMark > 0) {
- schemeSpecificPart = schemeSpecificPart.substring(0, questionMark);
+ uriAsString = uriAsString.substring(0, questionMark);
}
+
if (query != null && query.length() > 0) {
- schemeSpecificPart += "?" + query;
+ uriAsString += "?" + query;
+ }
+
+ if (fragment != null && fragment.length() > 0) {
+ uriAsString += fragment;
}
- return new URI(uri.getScheme(), schemeSpecificPart, uri.getFragment());
+
+ return new URI(uriAsString);
}
/**
@@ -107,7 +134,9 @@ public class PropertyUtil {
/**
* Given a key / value mapping, create and return a URI formatted query string that is valid
- * and can be appended to a URI.
+ * and can be appended to a URI string. The values in the given Map are URL Encoded during
+ * construction of the query string, if the original values were previously encoded this can
+ * result in double encoding.
*
* @param options
* The Mapping that will create the new Query string.
@@ -127,9 +156,10 @@ public class PropertyUtil {
} else {
rc.append("&");
}
- rc.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+
+ rc.append(URLEncoder.encode(entry.getKey(), UTF8));
rc.append("=");
- rc.append(URLEncoder.encode((String) entry.getValue(), "UTF-8"));
+ rc.append(URLEncoder.encode((String) entry.getValue(), UTF8));
}
return rc.toString();
} else {
@@ -141,49 +171,29 @@ public class PropertyUtil {
}
/**
- * Get properties from a URI and return them in a new {@code Map<String, String>} instance.
- *
- * If the URI is null or the query string of the URI is null an empty Map is returned.
- *
- * @param uri
- * the URI whose parameters are to be parsed.
- *
- * @return <Code>Map</Code> of properties
- *
- * @throws Exception if an error occurs while parsing the query options.
- */
- public static Map<String, String> parseParameters(URI uri) throws Exception {
- if (uri == null || uri.getQuery() == null) {
- return Collections.emptyMap();
- }
-
- return parseQuery(stripPrefix(uri.getQuery(), "?"));
- }
-
- /**
- * Parse properties from a named resource -eg. a URI or a simple name e.g.
- * {@literal foo?name="fred"&size=2}
+ * Get properties from the Query portion of the given URI.
*
* @param uri
- * the URI whose parameters are to be parsed.
+ * the URI whose Query string should be parsed.
*
- * @return <Code>Map</Code> of properties
+ * @return <Code>Map</Code> of properties from the parsed string.
*
* @throws Exception if an error occurs while parsing the query options.
*/
- public static Map<String, String> parseParameters(String uri) throws Exception {
+ public static Map<String, String> parseQuery(URI uri) throws Exception {
if (uri == null) {
return Collections.emptyMap();
}
- return parseQuery(stripUpto(uri, '?'));
+ return parseQuery(uri.getRawQuery());
}
/**
- * Get properties from a URI query string.
+ * Get properties from a URI Query String obtained by calling the getRawQuery method
+ * on a given URI or from some other source.
*
* @param queryString
- * the string value returned from a call to the URI class getQuery method.
+ * the query string obtained from called getRawQuery on a given URI.
*
* @return <Code>Map</Code> of properties from the parsed string.
*
@@ -196,8 +206,9 @@ public class PropertyUtil {
for (int i = 0; i < parameters.length; i++) {
int p = parameters[i].indexOf("=");
if (p >= 0) {
- String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
- String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
+ String name = URLDecoder.decode(parameters[i].substring(0, p), UTF8);
+ String value = URLDecoder.decode(parameters[i].substring(p + 1), UTF8);
+
rc.put(name, value);
} else {
rc.put(parameters[i], null);
@@ -240,79 +251,6 @@ public class PropertyUtil {
}
/**
- * Enumerate the properties of the target object and add them as additional entries
- * to the query string of the given string URI.
- *
- * @param uri
- * The string URI value to append the object properties to.
- * @param bean
- * The Object whose properties will be added to the target URI.
- *
- * @return a new String value that is the original URI with the added bean properties.
- *
- * @throws Exception if an error occurs while enumerating the bean properties.
- */
- public static String addPropertiesToURIFromBean(String uri, Object bean) throws Exception {
- Map<String, String> properties = PropertyUtil.getProperties(bean);
- return PropertyUtil.addPropertiesToURI(uri, properties);
- }
-
- /**
- * Enumerate the properties of the target object and add them as additional entries
- * to the query string of the given URI.
- *
- * @param uri
- * The URI value to append the object properties to.
- * @param properties
- * The Object whose properties will be added to the target URI.
- *
- * @return a new String value that is the original URI with the added bean properties.
- *
- * @throws Exception if an error occurs while enumerating the bean properties.
- */
- public static String addPropertiesToURI(URI uri, Map<String, String> properties) throws Exception {
- return addPropertiesToURI(uri.toString(), properties);
- }
-
- /**
- * Append the given properties to the query portion of the given URI.
- *
- * @param uri
- * The string URI value to append the object properties to.
- * @param properties
- * The properties that will be added to the target URI.
- *
- * @return a new String value that is the original URI with the added properties.
- *
- * @throws Exception if an error occurs while building the new URI string.
- */
- public static String addPropertiesToURI(String uri, Map<String, String> properties) throws Exception {
- String result = uri;
- if (uri != null && properties != null) {
- StringBuilder base = new StringBuilder(stripBefore(uri, '?'));
- Map<String, String> map = parseParameters(uri);
- if (!map.isEmpty()) {
- map.putAll(properties);
- } else {
- map = properties;
- }
- if (!map.isEmpty()) {
- base.append('?');
- boolean first = true;
- for (Map.Entry<String, String> entry : map.entrySet()) {
- if (!first) {
- base.append('&');
- }
- first = false;
- base.append(entry.getKey()).append("=").append(entry.getValue());
- }
- result = base.toString();
- }
- }
- return result;
- }
-
- /**
* Set properties on an object using the provided map. The return value
* indicates if all properties from the given map were set on the target object.
*
@@ -342,8 +280,6 @@ public class PropertyUtil {
return Collections.unmodifiableMap(unmatched);
}
- //TODO: common impl for above and below methods.
-
/**
* Set properties on an object using the provided Properties object. The return value
* indicates if all properties from the given map were set on the target object.
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/URISupport.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/URISupport.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/URISupport.java
index fe075cb..2dc54f4 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/URISupport.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/util/URISupport.java
@@ -208,6 +208,7 @@ public class URISupport {
if (ssp.indexOf('(') == 0 && checkParenthesis(ssp)) {
return true;
}
+
return false;
}
@@ -237,6 +238,7 @@ public class URISupport {
}
result = open == closed;
}
+
return result;
}
@@ -360,11 +362,11 @@ public class URISupport {
*/
public static Map<String, String> parseParameters(URI uri) throws URISyntaxException {
if (!isCompositeURI(uri)) {
- if (uri.getQuery() == null) {
+ if (uri.getRawQuery() == null) {
return Collections.emptyMap();
} else {
try {
- return PropertyUtil.parseQuery(PropertyUtil.stripPrefix(uri.getQuery(), "?"));
+ return PropertyUtil.parseQuery(PropertyUtil.stripPrefix(uri.getRawQuery(), "?"));
} catch (Exception e) {
throw new URISyntaxException(uri.toString(), e.getMessage());
}
@@ -384,7 +386,8 @@ public class URISupport {
/**
* Given a Key / Value mapping create and append a URI query value that represents the
* mapped entries, return the newly updated URI that contains the value of the given URI and
- * the appended query value.
+ * the appended query value. The values in the parameters map should not be URL encoded values
+ * as this method will perform an encode on them resulting in double encoded values.
*
* @param uri
* The source URI that will have the Map entries appended as a URI query value.
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/SaslIntegrationTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/SaslIntegrationTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/SaslIntegrationTest.java
index 65e378d..c790457 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/SaslIntegrationTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/SaslIntegrationTest.java
@@ -117,6 +117,37 @@ public class SaslIntegrationTest extends QpidJmsTestCase {
}
@Test(timeout = 20000)
+ public void testSaslPlainConnectionWithURIEncodedCredentials() throws Exception {
+ try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
+
+ // Expect a PLAIN connection with decoded password from URL encoded value.
+ String user = "user";
+ String pass = "CN24tCa+Hn/av";
+ String encodedPass = "CN24tCa%2BHn%2Fav";
+
+ testPeer.expectSaslPlain(user, pass);
+ testPeer.expectOpen();
+
+ // Each connection creates a session for managing temporary destinations etc
+ testPeer.expectBegin();
+
+ ConnectionFactory factory = new JmsConnectionFactory(
+ "amqp://localhost:" + testPeer.getServerPort() +
+ "?jms.username=" + user + "&jms.password=" + encodedPass);
+
+ Connection connection = factory.createConnection();
+ // Set a clientID to provoke the actual AMQP connection process to occur.
+ connection.setClientID("clientName");
+
+ testPeer.waitForAllHandlersToComplete(1000);
+ assertNull(testPeer.getThrowable());
+
+ testPeer.expectClose();
+ connection.close();
+ }
+ }
+
+ @Test(timeout = 20000)
public void testSaslAnonymousConnection() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
// Expect an ANOYMOUS connection
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java
index 68204da..46ee509 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java
@@ -208,9 +208,9 @@ public class PropertyUtilTest {
}
@Test
- public void testParseParametersFromURI() throws Exception {
+ public void testParseQueryFromURI() throws Exception {
URI original = new URI("http://www.example.com?option=true&another=false");
- Map<String, String> result = PropertyUtil.parseParameters(original);
+ Map<String, String> result = PropertyUtil.parseQuery(original);
assertEquals(2, result.size());
assertTrue(result.containsKey("option"));
@@ -220,23 +220,23 @@ public class PropertyUtilTest {
}
@Test
- public void testParseParametersFromURIWithNoQuery() throws Exception {
+ public void testParseQueryFromURIWithNoQuery() throws Exception {
URI original = new URI("http://www.example.com");
- Map<String, String> result = PropertyUtil.parseParameters(original);
+ Map<String, String> result = PropertyUtil.parseQuery(original);
assertNotNull(result);
assertTrue(result.isEmpty());
}
@Test
- public void testParseParametersFromNullURI() throws Exception {
- Map<String, String> result = PropertyUtil.parseParameters((URI) null);
+ public void testParseQueryFromNullURI() throws Exception {
+ Map<String, String> result = PropertyUtil.parseQuery((URI) null);
assertNotNull(result);
assertTrue(result.isEmpty());
}
@Test
- public void testParseParametersFromString() throws Exception {
- Map<String, String> result = PropertyUtil.parseParameters("http://www.example.com?option=true&another=false");
+ public void testParseQueryFromString() throws Exception {
+ Map<String, String> result = PropertyUtil.parseQuery("option=true&another=false");
assertTrue(result.size() == 2);
assertTrue(result.containsKey("option"));
@@ -246,8 +246,8 @@ public class PropertyUtilTest {
}
@Test
- public void testParseParametersFromStringWithNoValues() throws Exception {
- Map<String, String> result = PropertyUtil.parseParameters("http://www.example.com?option=&another=");
+ public void testParseQueryFromStringWithNoValues() throws Exception {
+ Map<String, String> result = PropertyUtil.parseQuery("option=&another=");
assertTrue(result.size() == 2);
assertTrue(result.containsKey("option"));
@@ -257,31 +257,13 @@ public class PropertyUtilTest {
}
@Test
- public void testParseParametersFromURIStringWithNoQuery() throws Exception {
- Map<String, String> result = PropertyUtil.parseParameters("http://www.example.com");
- assertNotNull(result);
- assertTrue(result.isEmpty());
- }
-
- @Test
- public void testParseParametersFromNullURIString() throws Exception {
- Map<String, String> result = PropertyUtil.parseParameters((String) null);
+ public void testParseQueryFromNullURIString() throws Exception {
+ Map<String, String> result = PropertyUtil.parseQuery((String) null);
assertNotNull(result);
assertTrue(result.isEmpty());
}
@Test
- public void testParseQuery() throws Exception {
- Map<String, String> result = PropertyUtil.parseQuery("option=true&another=false");
-
- assertTrue(result.size() == 2);
- assertTrue(result.containsKey("option"));
- assertTrue(result.containsKey("another"));
- assertEquals("true", result.get("option"));
- assertEquals("false", result.get("another"));
- }
-
- @Test
public void testParseQueryWithSingletonProperty() throws Exception {
Map<String, String> result = PropertyUtil.parseQuery("option=true&another=false¬Assigned");
@@ -332,102 +314,6 @@ public class PropertyUtilTest {
}
@Test
- public void testAddPropertiesToURIFromBean() throws Exception {
- String uriBase = "www.example.com";
- Options configObject = new Options("foo", "bar");
-
- String result = PropertyUtil.addPropertiesToURIFromBean(uriBase, configObject);
- assertNotNull(result);
- URI resultURI = new URI(result);
- assertNotNull(resultURI.getQuery());
-
- Map<String, String> props = PropertyUtil.parseQuery(resultURI.getQuery());
- assertFalse(props.isEmpty());
- assertTrue(props.containsKey("firstName"));
- assertTrue(props.containsKey("lastName"));
- }
-
- @Test
- public void testAddPropertiesToURIFromNullBean() throws Exception {
- String uriBase = "www.example.com";
- String result = PropertyUtil.addPropertiesToURIFromBean(uriBase, (Options) null);
- assertNotNull(result);
- URI resultURI = new URI(result);
- assertNull(resultURI.getQuery());
- }
-
- @Test
- public void testAddPropertiesToStringURIFromMap() throws Exception {
- String uriBase = "www.example.com";
-
- Map<String, String> properties = new HashMap<String, String>();
- properties.put("option1", "true");
- properties.put("option2", "false");
-
- String result = PropertyUtil.addPropertiesToURI(uriBase, properties);
- assertNotNull(result);
- URI resultURI = new URI(result);
- assertNotNull(resultURI.getQuery());
-
- Map<String, String> parsed = PropertyUtil.parseQuery(resultURI.getQuery());
- assertEquals(properties, parsed);
- }
-
- @Test
- public void testAddPropertiesToStringURIFromEmptyMap() throws Exception {
- String uriBase = "www.example.com";
-
- String result = PropertyUtil.addPropertiesToURI(uriBase, null);
- assertSame(uriBase, result);
- }
-
- @Test
- public void testAddPropertiesToStringURIWithNullURI() throws Exception {
- Map<String, String> properties = new HashMap<String, String>();
- properties.put("option1", "true");
- properties.put("option2", "false");
-
- String result = PropertyUtil.addPropertiesToURI((String) null, properties);
- assertNull(result);
- }
-
- @Test
- public void testAddPropertiesToStringURIFromMapKeepsExisting() throws Exception {
- String uriBase = "www.example.com?existing=keepMe";
-
- Map<String, String> properties = new HashMap<String, String>();
- properties.put("option1", "true");
- properties.put("option2", "false");
-
- String result = PropertyUtil.addPropertiesToURI(uriBase, properties);
- assertNotNull(result);
- URI resultURI = new URI(result);
- assertNotNull(resultURI.getQuery());
-
- Map<String, String> parsed = PropertyUtil.parseQuery(resultURI.getQuery());
- assertEquals(3, parsed.size());
- assertTrue(parsed.containsKey("existing"));
- assertEquals("keepMe", parsed.get("existing"));
- }
-
- @Test
- public void testAddPropertiesToURIFromMap() throws Exception {
- URI uri = new URI("www.example.com");
-
- Map<String, String> properties = new HashMap<String, String>();
- properties.put("option1", "true");
- properties.put("option2", "false");
-
- String result = PropertyUtil.addPropertiesToURI(uri, properties);
- assertNotNull(result);
- URI resultURI = new URI(result);
- assertNotNull(resultURI.getQuery());
-
- Map<String, String> parsed = PropertyUtil.parseQuery(resultURI.getQuery());
- assertEquals(properties, parsed);
- }
-
- @Test
public void testSetProperties() throws Exception {
Options configObject = new Options();
@@ -702,4 +588,74 @@ public class PropertyUtilTest {
public void testStripUpToNullString() {
assertNull(PropertyUtil.stripUpto((String) null, '.'));
}
+
+ //----- Tests for URI options that are URL Encoded -----------------------//
+
+ @Test
+ public void testReplaceQueryWithStringDoesNotReencode() throws URISyntaxException {
+ URI original = new URI("http://www.example.com?option=X");
+
+ final String encodedValue = "%25Ca%2BHn%2Fav";
+ final String decodedValue = "%Ca+Hn/av";
+
+ final String encodedKey = "user%2Bname";
+ final String decodedKey = "user+name";
+
+ String newQuery = encodedKey + "=" + encodedValue;
+
+ URI updated = PropertyUtil.replaceQuery(original, newQuery);
+
+ assertEquals(encodedKey + "=" + encodedValue, updated.getRawQuery());
+ assertEquals(decodedKey + "=" + decodedValue, updated.getQuery());
+ }
+
+ @Test
+ public void testReplaceQueryUsingMapEncodesParameters() throws URISyntaxException {
+ URI original = new URI("http://www.example.com?option=X");
+
+ final String encodedValue = "%25Ca%2BHn%2Fav";
+ final String decodedValue = "%Ca+Hn/av";
+
+ final String encodedKey = "user%2Bname";
+ final String decodedKey = "user+name";
+
+ Map<String, String> newQuery = new HashMap<String, String>();
+ newQuery.put(decodedKey, decodedValue);
+
+ URI updated = PropertyUtil.replaceQuery(original, newQuery);
+
+ assertEquals(encodedKey + "=" + encodedValue, updated.getRawQuery());
+ assertEquals(decodedKey + "=" + decodedValue, updated.getQuery());
+ }
+
+ @Test
+ public void testParseQueryDecodesParameters() throws Exception {
+ URI original = new URI("http://www.example.com?user%2Bname=%25Ca%2BHn%2Fav");
+
+ final String decodedKey = "user+name";
+ final String decodedValue = "%Ca+Hn/av";
+
+ Map<String, String> result = PropertyUtil.parseQuery(original);
+
+ assertEquals(1, result.size());
+ assertTrue(result.containsKey(decodedKey));
+ assertEquals(decodedValue, result.get(decodedKey));
+ }
+
+ @Test
+ public void testCreateQueryEncodesParameters() throws URISyntaxException {
+
+ final String encodedValue = "%25Ca%2BHn%2Fav";
+ final String decodedValue = "%Ca+Hn/av";
+
+ final String encodedKey = "user%2Bname";
+ final String decodedKey = "user+name";
+
+ Map<String, String> source = new HashMap<String, String>();
+ source.put(decodedKey, decodedValue);
+
+ String result = PropertyUtil.createQueryString(source);
+
+ assertEquals(encodedKey + "=" + encodedValue, result);
+ }
}
http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/8485ada9/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/URISupportTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/URISupportTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/URISupportTest.java
index 5880ae6..b9b556a 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/URISupportTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/URISupportTest.java
@@ -27,6 +27,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.qpid.jms.util.URISupport.CompositeData;
@@ -55,10 +56,34 @@ public class URISupportTest {
}
@Test
- public void testComposite() throws Exception {
+ public void testParseComposite() throws Exception {
URI uri = new URI("test:(part1://host,part2://(sub1://part,sube2:part))");
CompositeData data = URISupport.parseComposite(uri);
assertEquals(2, data.getComponents().size());
+
+ assertFalse(URISupport.isCompositeURI(data.getComponents().get(0)));
+ assertTrue(URISupport.isCompositeURI(data.getComponents().get(1)));
+
+ assertTrue(data.getParameters().isEmpty());
+ }
+
+ @Test
+ public void testParseCompositeWithInnerURIsWithQueryValues() throws Exception {
+ URI uri = new URI("failover://(amqp://127.0.0.1:5678,amqp://127.0.0.2:5677,amqp://127.0.0.3:5676?amqp.traceFrames=false)?failover.useReconnectBackOff=false");
+ CompositeData data = URISupport.parseComposite(uri);
+ assertEquals(3, data.getComponents().size());
+
+ Map<String, String> parameters = data.getParameters();
+ assertEquals(1, parameters.size());
+
+ assertTrue(parameters.containsKey("failover.useReconnectBackOff"));
+ assertTrue(parameters.get("failover.useReconnectBackOff").equals("false"));
+
+ List<URI> uris = data.getComponents();
+
+ assertEquals(uris.get(0).toString(), "amqp://127.0.0.1:5678");
+ assertEquals(uris.get(1).toString(), "amqp://127.0.0.2:5677");
+ assertEquals(uris.get(2).toString(), "amqp://127.0.0.3:5676?amqp.traceFrames=false");
}
@Test
@@ -105,7 +130,7 @@ public class URISupportTest {
public void testParsingURI() throws Exception {
URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
- Map<String, String> map = PropertyUtil.parseParameters(source);
+ Map<String, String> map = PropertyUtil.parseQuery(source);
assertEquals("Size: " + map, 2, map.size());
assertMapKey(map, "cheese", "Edam");
@@ -120,7 +145,7 @@ public class URISupportTest {
public void testParsingURIWithEmptyValuesInOptions() throws Exception {
URI source = new URI("tcp://localhost:61626/foo/bar?cheese=&x=");
- Map<String, String> map = PropertyUtil.parseParameters(source);
+ Map<String, String> map = PropertyUtil.parseQuery(source);
assertEquals("Size: " + map, 2, map.size());
assertMapKey(map, "cheese", "");
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org