Repository: qpid-jms Updated Branches: refs/heads/master 3bcaf7dc7 -> 3fc5f20ab
Allow setting object properties from a Properties object as well as a Map<String, String> Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/698a2eac Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/698a2eac Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/698a2eac Branch: refs/heads/master Commit: 698a2eac71d5b211c39fe6c4209ab7222785871e Parents: 3bcaf7d Author: Timothy Bish <[email protected]> Authored: Tue Jan 20 16:08:26 2015 -0500 Committer: Timothy Bish <[email protected]> Committed: Tue Jan 20 16:08:26 2015 -0500 ---------------------------------------------------------------------- .../org/apache/qpid/jms/util/PropertyUtil.java | 31 ++++++++++++++++ .../apache/qpid/jms/util/PropertyUtilTest.java | 37 +++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/698a2eac/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 6184487..4423ad6 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 @@ -34,6 +34,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import javax.net.ssl.SSLContext; @@ -329,6 +330,36 @@ public class PropertyUtil { } /** + * 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. + * + * @param target + * the object whose properties are to be set from the map options. + * @param props + * the properties that should be applied to the given object. + * + * @return true if all values in the props map were applied to the target object. + */ + public static boolean setProperties(Object target, Properties props) { + if (target == null) { + throw new IllegalArgumentException("target was null."); + } + if (props == null) { + throw new IllegalArgumentException("props was null."); + } + + int setCounter = 0; + + for (Map.Entry<Object, Object> entry : props.entrySet()) { + if (setProperty(target, (String) entry.getKey(), entry.getValue())) { + setCounter++; + } + } + + return setCounter == props.size(); + } + + /** * Get properties from an object using reflection. If the passed object is null an * empty <code>Map</code> is returned. * http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/698a2eac/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 38bb946..13ec06f 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 @@ -28,6 +28,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import javax.net.ssl.SSLContext; @@ -421,6 +422,20 @@ public class PropertyUtilTest { } @Test + public void testSetPropertiesUsingPropertiesObject() throws Exception { + Options configObject = new Options(); + + Properties properties = new Properties(); + properties.put("firstName", "foo"); + properties.put("lastName", "bar"); + + assertTrue(PropertyUtil.setProperties(configObject, properties)); + + assertEquals("foo", configObject.getFirstName()); + assertEquals("bar", configObject.getLastName()); + } + + @Test public void testSetPropertiesWithUnusedOptions() throws Exception { Options configObject = new Options(); @@ -436,6 +451,21 @@ public class PropertyUtilTest { } @Test + public void testSetPropertiesWithUnusedOptionsUsingPropertiesObject() throws Exception { + Options configObject = new Options(); + + Properties properties = new Properties(); + properties.put("firstName", "foo"); + properties.put("lastName", "bar"); + properties.put("unused", "absent"); + + assertFalse(PropertyUtil.setProperties(configObject, properties)); + + assertEquals("foo", configObject.getFirstName()); + assertEquals("bar", configObject.getLastName()); + } + + @Test public void testGetProperties() throws Exception { Options configObject = new Options("foo", "bar"); @@ -561,7 +591,12 @@ public class PropertyUtilTest { @Test(expected=IllegalArgumentException.class) public void testSetPropertiesWithNullMap() { - PropertyUtil.setProperties(new Options(), null); + PropertyUtil.setProperties(new Options(), (Map<String, String>) null); + } + + @Test(expected=IllegalArgumentException.class) + public void testSetPropertiesWithNullProperties() { + PropertyUtil.setProperties(new Options(), (Properties) null); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
