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 2014/10/06 22:39:14 UTC

[1/2] git commit: Tests for IdGenerator

Repository: qpid-jms
Updated Branches:
  refs/heads/master 7a7bec710 -> 43c8be481


Tests for IdGenerator

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/43c8be48
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/43c8be48
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/43c8be48

Branch: refs/heads/master
Commit: 43c8be4819242dc5205af6f6b66966b9ea3049b1
Parents: 601d8f5
Author: Timothy Bish <ta...@gmail.com>
Authored: Mon Oct 6 16:37:02 2014 -0400
Committer: Timothy Bish <ta...@gmail.com>
Committed: Mon Oct 6 16:37:13 2014 -0400

----------------------------------------------------------------------
 .../apache/qpid/jms/util/IdGeneratorTest.java   | 114 +++++++++++++++++++
 1 file changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/43c8be48/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/IdGeneratorTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/IdGeneratorTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/IdGeneratorTest.java
new file mode 100644
index 0000000..21fd110
--- /dev/null
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/IdGeneratorTest.java
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.qpid.jms.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class IdGeneratorTest {
+
+    IdGenerator generator;
+
+    @Before
+    public void setUp() {
+        generator = new IdGenerator();
+    }
+
+    @Test
+    public void testSanitizeHostName() throws Exception {
+        assertEquals("somehost.lan", IdGenerator.sanitizeHostName("somehost.lan"));
+        // include a UTF-8 char in the text \u0E08 is a Thai elephant
+        assertEquals("otherhost.lan", IdGenerator.sanitizeHostName("other\u0E08host.lan"));
+    }
+
+    @Test
+    public void testGetHostName() {
+        assertNotNull(IdGenerator.getHostName());
+    }
+
+    @Test
+    public void testGenerateSanitizedId() {
+        IdGenerator generator = new IdGenerator("A:B.C");
+        assertNotNull(generator.generateSanitizedId());
+        String newId = generator.generateSanitizedId();
+        assertFalse(newId.contains(":"));
+        assertFalse(newId.contains("."));
+    }
+
+    @Test
+    public void testGetSequenceFromId() {
+        assertNotNull(IdGenerator.getSequenceFromId(generator.generateId()));
+    }
+
+    @Test
+    public void testGetSequenceFromNullId() {
+        assertEquals(-1, IdGenerator.getSequenceFromId(null));
+    }
+
+    @Test
+    public void testGetSequenceFromNonConformingId() {
+        assertEquals(-1, IdGenerator.getSequenceFromId("SomeIdValue"));
+    }
+
+    @Test
+    public void testGetSequenceFromNonConformingId2() {
+        assertEquals(-1, IdGenerator.getSequenceFromId("SomeIdValue:"));
+    }
+
+    @Test
+    public void testGetSeedFromId() {
+        assertNotNull(IdGenerator.getSeedFromId(generator.generateId()));
+    }
+
+    @Test
+    public void testGetSeedFromNullId() {
+        assertNull(IdGenerator.getSeedFromId(null));
+    }
+
+    @Test
+    public void testGetSeedFromNonConformingId() {
+        assertEquals("SomeIdValue", IdGenerator.getSeedFromId("SomeIdValue"));
+    }
+
+    @Test
+    public void testGetSeedFromNonConformingId2() {
+        assertEquals("SomeIdValue:", IdGenerator.getSeedFromId("SomeIdValue:"));
+    }
+
+    @Test
+    public void testCompareIds() {
+        IdGenerator gen = new IdGenerator();
+
+        String id1 = generator.generateId();
+        String id2 = generator.generateId();
+        String id3 = generator.generateId();
+
+        assertEquals(0, IdGenerator.compare(id1, id1));
+        assertEquals(1, IdGenerator.compare(id2, id1));
+        assertEquals(-1, IdGenerator.compare(id2, id3));
+        assertEquals(-1, IdGenerator.compare(id2, null));
+        assertEquals(-1, IdGenerator.compare(null, id3));
+
+        String idg1 = gen.generateId();
+        assertEquals(1, IdGenerator.compare(idg1, id3));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


[2/2] git commit: Create test case for the PropertyUtil class that covers most bits, fix all the little issues that were found during testing.

Posted by ta...@apache.org.
Create test case for the PropertyUtil class that covers most bits, fix
all the little issues that were found during testing.

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/601d8f50
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/601d8f50
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/601d8f50

Branch: refs/heads/master
Commit: 601d8f50565a2a49afcb16a5303c3896c75ef2d9
Parents: 7a7bec7
Author: Timothy Bish <ta...@gmail.com>
Authored: Mon Oct 6 15:57:17 2014 -0400
Committer: Timothy Bish <ta...@gmail.com>
Committed: Mon Oct 6 16:37:13 2014 -0400

----------------------------------------------------------------------
 .../org/apache/qpid/jms/util/PropertyUtil.java  |  87 ++-
 .../apache/qpid/jms/util/PropertyUtilTest.java  | 539 +++++++++++++++++++
 2 files changed, 600 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/601d8f50/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 0c6b54b..aebb845 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
@@ -143,15 +143,23 @@ public class PropertyUtil {
     }
 
     /**
-     * Get properties from a URI
+     * Get properties from a URI and return them in a new 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
+     *
+     * @throws Exception if an error occurs while parsing the query options.
      */
-    @SuppressWarnings("unchecked")
     public static Map<String, String> parseParameters(URI uri) throws Exception {
-        return uri.getQuery() == null ? Collections.EMPTY_MAP : parseQuery(stripPrefix(uri.getQuery(), "?"));
+        if (uri == null || uri.getQuery() == null) {
+            return Collections.emptyMap();
+        }
+
+        return parseQuery(stripPrefix(uri.getQuery(), "?"));
     }
 
     /**
@@ -159,26 +167,34 @@ public class PropertyUtil {
      * foo?name="fred"&size=2
      *
      * @param uri
+     *        the URI whose parameters are to be parsed.
+     *
      * @return <Code>Map</Code> of properties
-     * @throws Exception
+     *
+     * @throws Exception if an error occurs while parsing the query options.
      */
-    @SuppressWarnings("unchecked")
     public static Map<String, String> parseParameters(String uri) throws Exception {
-        return uri == null ? Collections.EMPTY_MAP : parseQuery(stripUpto(uri, '?'));
+        if (uri == null) {
+            return Collections.emptyMap();
+        }
+
+        return parseQuery(stripUpto(uri, '?'));
     }
 
     /**
-     * Get properties from a uri
+     * Get properties from a URI query string.
      *
-     * @param uri
-     * @return <Code>Map</Code> of properties
+     * @param queryString
+     *        the string value returned from a call to the URI class getQuery method.
      *
-     * @throws Exception
+     * @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> parseQuery(String uri) throws Exception {
-        if (uri != null) {
+    public static Map<String, String> parseQuery(String queryString) throws Exception {
+        if (queryString != null && !queryString.isEmpty()) {
             Map<String, String> rc = new HashMap<String, String>();
-            String[] parameters = uri.split("&");
+            String[] parameters = queryString.split("&");
             for (int i = 0; i < parameters.length; i++) {
                 int p = parameters[i].indexOf("=");
                 if (p >= 0) {
@@ -191,6 +207,7 @@ public class PropertyUtil {
             }
             return rc;
         }
+
         return Collections.emptyMap();
     }
 
@@ -264,6 +281,8 @@ public class PropertyUtil {
             Map<String, String> map = parseParameters(uri);
             if (!map.isEmpty()) {
                 map.putAll(props);
+            } else {
+                map = props;
             }
             if (!map.isEmpty()) {
                 base.append('?');
@@ -312,13 +331,21 @@ public class PropertyUtil {
     }
 
     /**
-     * Get properties from an object
+     * Get properties from an object using reflection.  If the passed object is null an
+     * empty <code>Map</code> is returned.
      *
      * @param object
-     * @return <Code>Map</Code> of properties
-     * @throws Exception
+     *        the Object whose properties are to be extracted.
+     *
+     * @return <Code>Map</Code> of properties extracted from the given object.
+     *
+     * @throws Exception if an error occurs while examining the object's properties.
      */
     public static Map<String, String> getProperties(Object object) throws Exception {
+        if (object == null) {
+            return Collections.emptyMap();
+        }
+
         Map<String, String> props = new LinkedHashMap<String, String>();
         BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
         Object[] NULL_ARG = {};
@@ -410,14 +437,18 @@ public class PropertyUtil {
     }
 
     /**
-     * Return a String past a prefix
+     * Return a String minus the given prefix.  If the string does not start
+     * with the given prefix the original string value is returned.
      *
      * @param value
+     *        The String whose prefix is to be removed.
      * @param prefix
-     * @return stripped
+     *        The prefix string to remove from the target string.
+     *
+     * @return stripped version of the original input string.
      */
     public static String stripPrefix(String value, String prefix) {
-        if (value.startsWith(prefix)) {
+        if (value != null && prefix != null && value.startsWith(prefix)) {
             return value.substring(prefix.length());
         }
         return value;
@@ -432,9 +463,11 @@ public class PropertyUtil {
      */
     public static String stripUpto(String value, char c) {
         String result = null;
-        int index = value.indexOf(c);
-        if (index > 0) {
-            result = value.substring(index + 1);
+        if (value != null) {
+            int index = value.indexOf(c);
+            if (index > 0) {
+                result = value.substring(index + 1);
+            }
         }
         return result;
     }
@@ -448,9 +481,11 @@ public class PropertyUtil {
      */
     public static String stripBefore(String value, char c) {
         String result = value;
-        int index = value.indexOf(c);
-        if (index > 0) {
-            result = value.substring(0, index);
+        if (value != null) {
+            int index = value.indexOf(c);
+            if (index > 0) {
+                result = value.substring(0, index);
+            }
         }
         return result;
     }

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/601d8f50/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
new file mode 100644
index 0000000..feb14a7
--- /dev/null
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/util/PropertyUtilTest.java
@@ -0,0 +1,539 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.qpid.jms.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.net.ssl.SSLContext;
+
+import org.junit.Test;
+
+/**
+ * Tests for URI and Class level property Utilities class.
+ */
+public class PropertyUtilTest {
+
+    @SuppressWarnings("unused")
+    private static final class Embedded {
+        private String option;
+
+        public void setOption(String option) {
+            this.option = option;
+        }
+
+        public String getOption() {
+            return option;
+        }
+    }
+
+    @SuppressWarnings("unused")
+    private static final class Options {
+        private String firstOption;
+        private String secondOption;
+        private String throwsWhenSet;
+        private String requiresTwo;
+        private String notReadable;
+
+        private int numberValue;
+        private boolean booleanValue;
+        private URL urlValue;
+        private URI uriValue;
+        private SSLContext context;
+
+        private final Embedded embedded = new Embedded();
+
+        public Options() {
+        }
+
+        public Options(String first, String last) {
+            this.firstOption = first;
+            this.secondOption = last;
+        }
+
+        public String getFirstName() {
+            return this.firstOption;
+        }
+
+        public void setFirstName(String name) {
+            this.firstOption = name;
+        }
+
+        public String getLastName() {
+            return this.secondOption;
+        }
+
+        public void setLastName(String name) {
+            this.secondOption = name;
+        }
+
+        public String getThrowsWhenSet() {
+            return throwsWhenSet;
+        }
+
+        public void setThrowsWhenSet(String value) {
+            throw new RuntimeException("Can't set this");
+        }
+
+        public String getRequiresTwo() {
+            return requiresTwo;
+        }
+
+        public void setRequiresTwo(String value, boolean trigger) {
+            if (trigger) this.requiresTwo = value;
+        }
+
+        public Number getNumberProperty() {
+            return numberValue;
+        }
+
+        public Boolean getBooleanProperty() {
+            return booleanValue;
+        }
+
+        public URI getUriValue() {
+            return uriValue;
+        }
+
+        public URL getUrlValue() {
+            return urlValue;
+        }
+
+        public SSLContext getSSLContext() {
+            return context;
+        }
+
+        public Embedded getEmbedded() {
+            return embedded;
+        }
+
+        public void setNotReadable(String value) {
+            this.notReadable = value;
+        }
+    }
+
+    @Test
+    public void testReplaceQueryUsingMap() throws URISyntaxException {
+        URI original = new URI("http://www.example.com?option=true");
+
+        Map<String, String> newQuery = new HashMap<String, String>();
+        newQuery.put("param", "replaced");
+
+        URI updated = PropertyUtil.replaceQuery(original, newQuery);
+
+        assertEquals("param=replaced", updated.getQuery());
+    }
+
+    @Test
+    public void testReplaceQueryUsingString() throws URISyntaxException {
+        URI original = new URI("http://www.example.com?option=true");
+        URI updated = PropertyUtil.replaceQuery(original, "param=replaced");
+
+        assertEquals("param=replaced", updated.getQuery());
+    }
+
+    @Test
+    public void testEraseQuery() throws URISyntaxException {
+        URI original = new URI("http://www.example.com?option=true");
+        URI updated = PropertyUtil.eraseQuery(original);
+
+        assertNull(updated.getQuery());
+    }
+
+    @Test
+    public void testCreateQuery() throws URISyntaxException {
+        Map<String, String> newQuery = new HashMap<String, String>();
+        newQuery.put("param1", "value");
+        newQuery.put("param2", "value");
+
+        String query = PropertyUtil.createQueryString(newQuery);
+
+        assertNotNull(query);
+        assertEquals("param1=value&param2=value", query);
+    }
+
+    @Test
+    public void testParseParametersFromURI() throws Exception {
+        URI original = new URI("http://www.example.com?option=true&another=false");
+        Map<String, String> result = PropertyUtil.parseParameters(original);
+
+        assertEquals(2, result.size());
+        assertTrue(result.containsKey("option"));
+        assertTrue(result.containsKey("another"));
+        assertEquals("true", result.get("option"));
+        assertEquals("false", result.get("another"));
+    }
+
+    @Test
+    public void testParseParametersFromURIWithNoQuery() throws Exception {
+        URI original = new URI("http://www.example.com");
+        Map<String, String> result = PropertyUtil.parseParameters(original);
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testParseParametersFromNullURI() throws Exception {
+        Map<String, String> result = PropertyUtil.parseParameters((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");
+
+        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 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);
+        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&notAssigned");
+
+        assertTrue(result.size() == 3);
+        assertTrue(result.containsKey("option"));
+        assertTrue(result.containsKey("another"));
+        assertTrue(result.containsKey("notAssigned"));
+        assertEquals("true", result.get("option"));
+        assertEquals("false", result.get("another"));
+        assertEquals(null, result.get("notAssigned"));
+    }
+
+    @Test
+    public void testParseQueryEmptryString() throws Exception {
+        Map<String, String> result = PropertyUtil.parseQuery("");
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testParseQueryNullString() throws Exception {
+        Map<String, String> result = PropertyUtil.parseQuery((String) null);
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testFilterPropertiesNullProperties() throws Exception {
+        PropertyUtil.filterProperties(null, "option.");
+    }
+
+    @Test
+    public void testFilterPropertiesNoFilter() throws Exception {
+        Map<String, String> properties = new HashMap<String, String>();
+        properties.put("unprefixied", "true");
+        properties.put("option.filtered1", "true");
+        properties.put("option.filtered2", "false");
+
+        Map<String, String> result = PropertyUtil.filterProperties(properties, "option.");
+
+        assertEquals(2, result.size());
+        assertTrue(result.containsKey("filtered1"));
+        assertTrue(result.containsKey("filtered2"));
+        assertFalse(result.containsKey("unprefixed"));
+
+        assertEquals("true", result.get("filtered1"));
+        assertEquals("false", result.get("filtered2"));
+    }
+
+    @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();
+
+        Map<String, String> properties = new HashMap<String, String>();
+        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();
+
+        Map<String, String> properties = new HashMap<String, String>();
+        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");
+
+        Map<String, String> properties = PropertyUtil.getProperties(configObject);
+
+        assertFalse(properties.isEmpty());
+        assertEquals("foo", properties.get("firstName"));
+        assertEquals("bar", properties.get("lastName"));
+    }
+
+    @Test
+    public void testGetProperty() throws Exception {
+        Options configObject = new Options("foo", "bar");
+        Object result = PropertyUtil.getProperty(configObject, "firstName");
+        assertNotNull(result);
+        assertTrue(result instanceof String);
+        assertEquals("foo", result);
+    }
+
+    @Test
+    public void testGetPropertyInvliadFieldName() throws Exception {
+        Options configObject = new Options("foo", "bar");
+        Object result = PropertyUtil.getProperty(configObject, "first");
+        assertNull(result);
+    }
+
+    @Test
+    public void testGetPropertyWithNoReadMethod() throws Exception {
+        Options configObject = new Options("foo", "bar");
+        Object result = PropertyUtil.getProperty(configObject, "notReadable");
+        assertNull(result);
+    }
+
+    @Test
+    public void testSetProperty() throws Exception {
+        Options configObject = new Options();
+        assertTrue(PropertyUtil.setProperty(configObject, "firstName", "foo"));
+        assertEquals("foo", configObject.getFirstName());
+    }
+
+    @Test
+    public void testSetPropertyToNull() throws Exception {
+        Options configObject = new Options();
+        configObject.setFirstName("foo");
+        assertTrue(PropertyUtil.setProperty(configObject, "firstName", null));
+        assertNull(configObject.getFirstName());
+    }
+
+    @Test
+    public void testSetPropertyOnlyWorksOnSingleArgProperties() throws Exception {
+        Options configObject = new Options();
+        configObject.setRequiresTwo("unchanged", true);
+        assertFalse(PropertyUtil.setProperty(configObject, "requiresTwo", "foo"));
+        assertEquals("unchanged", configObject.getRequiresTwo());
+    }
+
+    @Test
+    public void testSetPropertyWithWrongPropertyName() throws Exception {
+        Options configObject = new Options("goo", "bar");
+        assertFalse(PropertyUtil.setProperty(configObject, "first", "foo"));
+        assertEquals("goo", configObject.getFirstName());
+    }
+
+    @Test
+    public void testSetPropertyWithThrowOnSet() throws Exception {
+        Options configObject = new Options("goo", "bar");
+        assertFalse(PropertyUtil.setProperty(configObject, "throwsWhenSet", "foo"));
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testSetPropertiesWithNullObject() {
+        PropertyUtil.setProperties(null, new HashMap<String, String>());
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testSetPropertiesWithNullMap() {
+        PropertyUtil.setProperties(new Options(), null);
+    }
+
+    @Test
+    public void testStripPrefix() {
+        String value = "prefixed.option";
+        String result = PropertyUtil.stripPrefix(value, "prefixed.");
+        assertEquals("option", result);
+    }
+
+    @Test
+    public void testStripPrefixNullPrefix() {
+        String value = "prefixed.option";
+        String result = PropertyUtil.stripPrefix(value, null);
+        assertSame(value, result);
+    }
+
+    @Test
+    public void testStripPrefixNullValue() {
+        assertNull(PropertyUtil.stripPrefix((String) null, "prefixed."));
+    }
+
+    @Test
+    public void testStripBefore() {
+        String value = "prefixed.option";
+        String result = PropertyUtil.stripBefore(value, '.');
+        assertEquals("prefixed", result);
+    }
+
+    @Test
+    public void testStripBeforeNullString() {
+        assertNull(PropertyUtil.stripBefore((String) null, '.'));
+    }
+
+    @Test
+    public void testStripUpTo() {
+        String value = "prefixed.option";
+        String result = PropertyUtil.stripUpto(value, '.');
+        assertEquals("option", result);
+    }
+
+    @Test
+    public void testStripUpToNullString() {
+        assertNull(PropertyUtil.stripUpto((String) null, '.'));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org