commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r747924 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/base/ test/java/org/apache/commons/configuration2/base/
Date Wed, 25 Feb 2009 21:05:40 GMT
Author: oheger
Date: Wed Feb 25 21:05:39 2009
New Revision: 747924

URL: http://svn.apache.org/viewvc?rev=747924&view=rev
Log:
Added implementation for getKeys(String prefix) to ConfigurationSourceUtils.

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/PrefixedKeysIterator.java
      - copied, changed from r746796, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java?rev=747924&r1=747923&r2=747924&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
Wed Feb 25 21:05:39 2009
@@ -134,6 +134,34 @@
     }
 
     /**
+     * Calls the {@code getKeys(String prefix)} method on the specified {@code
+     * ConfigurationSource}. If the source does not implement this method, a
+     * default algorithm is used to obtain an iteration over all keys starting
+     * with the specified prefix.
+     *
+     * @param source the {@code ConfigurationSource} (must not be <b>null</b>)
+     * @param prefix the prefix of the desired keys
+     * @return an {@code Iterator} over all keys contained in this {@code
+     *         ConfigurationSource} starting with the given prefix
+     * @throws IllegalArgumentException if the source is <b>null</b>
+     * @see ConfigurationSource#getKeys(String)
+     */
+    public static Iterator<String> getKeys(ConfigurationSource source,
+            String prefix)
+    {
+        checkNullSource(source);
+
+        try
+        {
+            return source.getKeys(prefix);
+        }
+        catch (UnsupportedOperationException uoex)
+        {
+            return new PrefixedKeysIterator(source.getKeys(), prefix);
+        }
+    }
+
+    /**
      * Helper method for checking for a <b>null</b> parameter. If the specified
      * source is <b>null</b>, an exception is thrown.
      *

Copied: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/PrefixedKeysIterator.java
(from r746796, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/PrefixedKeysIterator.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/PrefixedKeysIterator.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java&r1=746796&r2=747924&rev=747924&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/PrefixedKeysIterator.java
Wed Feb 25 21:05:39 2009
@@ -14,16 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.configuration2;
+package org.apache.commons.configuration2.base;
 
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
  * <p>
- * A specialized iterator implementation used by
- * <code>{@link AbstractConfiguration}</code> to return an iteration over all
- * keys starting with a specified prefix.
+ * A specialized iterator implementation used by the {@code getKeys(String
+ * prefix)} method to return an iteration over all keys starting with a
+ * specified prefix.
  * </p>
  * <p>
  * This class is basically a stripped-down version of the

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java?rev=747924&r1=747923&r2=747924&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
Wed Feb 25 21:05:39 2009
@@ -231,4 +231,69 @@
             // ok
         }
     }
+
+    /**
+     * Tests the getKeys() implementation if this method is implemented by the
+     * source.
+     */
+    public void testGetKeysImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        @SuppressWarnings("unchecked")
+        Iterator<String> it = EasyMock.createMock(Iterator.class);
+        EasyMock.expect(source.getKeys(KEY)).andReturn(it);
+        EasyMock.replay(it, source);
+        assertEquals("Wrong iterator", it, ConfigurationSourceUtils.getKeys(
+                source, KEY));
+        EasyMock.verify(it, source);
+    }
+
+    /**
+     * Tests the getKeys() implementation if this method is not implemented by
+     * the source.
+     */
+    public void testGetKeysNotImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        EasyMock.expect(source.getKeys(KEY)).andThrow(
+                new UnsupportedOperationException());
+        Collection<String> keys = new ArrayList<String>();
+        keys.add(KEY);
+        final int count = 12;
+        final String key2 = "anotherKey";
+        for (int i = 0; i < count; i++)
+        {
+            keys.add(KEY + "." + i);
+            keys.add(key2 + i);
+        }
+        EasyMock.expect(source.getKeys()).andReturn(keys.iterator());
+        EasyMock.replay(source);
+        Iterator<String> it = ConfigurationSourceUtils.getKeys(source, KEY);
+        assertEquals("Wrong prefix", KEY, it.next());
+        for (int i = 0; i < count; i++)
+        {
+            assertEquals("Wrong key at " + i, KEY + "." + i, it.next());
+        }
+        assertFalse("Too many keys", it.hasNext());
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the getKeys() implementation if a null source is passed in. This
+     * should cause an exception.
+     */
+    public void testGetKeysNull()
+    {
+        try
+        {
+            ConfigurationSourceUtils.getKeys(null, KEY);
+            fail("No exception for null source!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
 }



Mime
View raw message