commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r746806 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/base/ test/java/org/apache/commons/configuration2/base/
Date Sun, 22 Feb 2009 20:05:17 GMT
Author: oheger
Date: Sun Feb 22 20:05:15 2009
New Revision: 746806

URL: http://svn.apache.org/viewvc?rev=746806&view=rev
Log:
(Experimental) implementation of ConfigurationSourceUtils.

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
  (with props)
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
  (with props)
Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java?rev=746806&r1=746805&r2=746806&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java
Sun Feb 22 20:05:15 2009
@@ -64,10 +64,13 @@
 public interface ConfigurationSource
 {
     /**
-     * Checks if the configuration is empty.
+     * Checks if the configuration is empty. This is an optional operation. It
+     * may be implemented by querying the keys contained in this configuration
+     * source and checking whether this iterator is empty.
      *
      * @return <code>true</code> if the configuration contains no property,
      *         <code>false</code> otherwise.
+     * @throws UnsupportedOperationException if this operation is not implemented
      */
     boolean isEmpty();
 
@@ -81,7 +84,7 @@
     boolean containsKey(String key);
 
     /**
-     * Adds a property to the configuration. If it already exists then the value
+     * Adds a property to the configuration. If it already exists, then the value
      * stated here will be added to the configuration entry. For example, if the
      * property:
      *

Added: 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=746806&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
Sun Feb 22 20:05:15 2009
@@ -0,0 +1,151 @@
+/*
+ * 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.commons.configuration2.base;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * <p>
+ * A class with utility methods for dealing with configuration sources.
+ * </p>
+ * <p>
+ * This class can be used for safely calling methods on a
+ * {@link ConfigurationSource} object that are optional. If the
+ * {@link ConfigurationSource} implements the method, it is invoked directly.
+ * Otherwise, a default implementation is provided that will be called.
+ * </p>
+ *
+ * @author Commons Configuration team
+ * @version $Id$
+ */
+public final class ConfigurationSourceUtils
+{
+    /**
+     * Private constructor. This class contains only static utility methods, so
+     * no instances need to be created.
+     */
+    private ConfigurationSourceUtils()
+    {
+    }
+
+    /**
+     * Calls {@code isEmpty()} on the specified {@code ConfigurationSource}. If
+     * the source does not implement this method, a default algorithm is used
+     * for finding out whether the source is empty.
+     *
+     * @param source the {@code ConfigurationSource} to be tested (must not be
+     *        <b>null</b>
+     * @return <b>true</b> if the configuration source is empty, <b>false</b>
+     *         otherwise
+     * @throws IllegalArgumentException if the source is <b>null</b>
+     * @see ConfigurationSource#isEmpty()
+     */
+    public static boolean isEmpty(ConfigurationSource source)
+    {
+        checkNullSource(source);
+        try
+        {
+            return source.isEmpty();
+        }
+        catch (UnsupportedOperationException uoex)
+        {
+            Iterator<String> it = source.getKeys();
+            return !it.hasNext();
+        }
+    }
+
+    /**
+     * Calls the {@code size()} method on the specified {@code
+     * ConfigurationSource}. If the source does not implement this method, a
+     * default algorithm is used for determining the size of the configuration
+     * source.
+     *
+     * @param source the {@code ConfigurationSource} (must not be <b>null</b>)
+     * @return the size of the {@code ConfigurationSource}
+     * @throws IllegalArgumentException if the source is <b>null</b>
+     * @see ConfigurationSource#size()
+     */
+    public static int size(ConfigurationSource source)
+    {
+        checkNullSource(source);
+
+        try
+        {
+            return source.size();
+        }
+        catch (UnsupportedOperationException uoex)
+        {
+            int count = 0;
+            for (Iterator<String> it = source.getKeys(); it.hasNext();)
+            {
+                count += valueCount(source, it.next());
+            }
+            return count;
+        }
+    }
+
+    /**
+     * Calls the {@code valueCount()} method on the specified {@code
+     * ConfigurationSource}. If the source does not implement this method, a
+     * default algorithm is used to determine the number of values stored for
+     * this property.
+     *
+     * @param source the {@code ConfigurationSource} (must not be <b>null</b>)
+     * @param key the key of the property to be tested
+     * @return the number of values stored for this property
+     * @throws IllegalArgumentException if the source is <b>null</b>
+     * @see ConfigurationSource#valueCount(String)
+     */
+    public static int valueCount(ConfigurationSource source, String key)
+    {
+        checkNullSource(source);
+
+        try
+        {
+            return source.valueCount(key);
+        }
+        catch (UnsupportedOperationException uoex)
+        {
+            Object value = source.getProperty(key);
+            if (value instanceof Collection)
+            {
+                return ((Collection<?>) value).size();
+            }
+            else
+            {
+                return (value == null) ? 0 : 1;
+            }
+        }
+    }
+
+    /**
+     * Helper method for checking for a <b>null</b> parameter. If the specified
+     * source is <b>null</b>, an exception is thrown.
+     *
+     * @param source the source in question
+     * @throws IllegalArgumentException if the source is <b>null</b>
+     */
+    private static void checkNullSource(ConfigurationSource source)
+    {
+        if (source == null)
+        {
+            throw new IllegalArgumentException(
+                    "ConfigurationSource must not be null!");
+        }
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 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=746806&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
Sun Feb 22 20:05:15 2009
@@ -0,0 +1,234 @@
+/*
+ * 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.commons.configuration2.base;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+
+/**
+ * Test class for {@link ConfigurationSourceUtils}.
+ *
+ * @author Commons Configuration team
+ * @version $Id$
+ */
+public class TestConfigurationSourceUtils extends TestCase
+{
+    /** Constant for a property key. */
+    private static final String KEY = "propertyKey";
+
+    /**
+     * Tests the isEmpty() method if it is implemented by the source.
+     */
+    public void testIsEmptyImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        EasyMock.expect(source.isEmpty()).andReturn(Boolean.FALSE);
+        EasyMock.replay(source);
+        assertFalse("Wrong result of isEmpty()", ConfigurationSourceUtils
+                .isEmpty(source));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the isEmpty() method if it is not implemented by the source.
+     */
+    public void testIsEmptyNotImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        Iterator<?> it = EasyMock.createMock(Iterator.class);
+        EasyMock.expect(source.isEmpty()).andThrow(
+                new UnsupportedOperationException());
+        source.getKeys();
+        EasyMock.expectLastCall().andReturn(it);
+        EasyMock.expect(it.hasNext()).andReturn(Boolean.TRUE);
+        EasyMock.replay(it, source);
+        assertFalse("Wrong result of isEmpty()", ConfigurationSourceUtils
+                .isEmpty(source));
+        EasyMock.verify(it, source);
+    }
+
+    /**
+     * Tests isEmpty() for a null source. This should throw an exception.
+     */
+    public void testIsEmptyNull()
+    {
+        try
+        {
+            ConfigurationSourceUtils.isEmpty(null);
+            fail("No exception for null source!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests the valueCount() implementation if this method is implemented by
+     * the source.
+     */
+    public void testValueCountImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        final int count = 4;
+        EasyMock.expect(source.valueCount(KEY)).andReturn(count);
+        EasyMock.replay(source);
+        assertEquals("Wrong value count", count, ConfigurationSourceUtils
+                .valueCount(source, KEY));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the valueCount() implementation if this method is not implemented
+     * by the source and the property cannot be found.
+     */
+    public void testValueCountNotImplementedNonExisting()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        EasyMock.expect(source.valueCount(KEY)).andThrow(
+                new UnsupportedOperationException());
+        EasyMock.expect(source.getProperty(KEY)).andReturn(null);
+        EasyMock.replay(source);
+        assertEquals("Wrong value count", 0, ConfigurationSourceUtils
+                .valueCount(source, KEY));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the valueCount() implementation if this method is not implemented
+     * by the source and the property has a single value.
+     */
+    public void testValueCountNotImplementedSingleValue()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        EasyMock.expect(source.valueCount(KEY)).andThrow(
+                new UnsupportedOperationException());
+        EasyMock.expect(source.getProperty(KEY)).andReturn(this);
+        EasyMock.replay(source);
+        assertEquals("Wrong value count", 1, ConfigurationSourceUtils
+                .valueCount(source, KEY));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the valueCount() implementation if this method is not implemented
+     * by the source and the property is a collection.
+     */
+    public void testValueCountNotImplementedCollection()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        EasyMock.expect(source.valueCount(KEY)).andThrow(
+                new UnsupportedOperationException());
+        final int count = 5;
+        Collection<Object> values = new ArrayList<Object>(count);
+        for (int i = 0; i < count; i++)
+        {
+            values.add(KEY + i);
+        }
+        EasyMock.expect(source.getProperty(KEY)).andReturn(values);
+        EasyMock.replay(source);
+        assertEquals("Wrong value count", count, ConfigurationSourceUtils
+                .valueCount(source, KEY));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the valueCount() implementation if a null source is passed in. This
+     * should cause an exception.
+     */
+    public void testValueCountNull()
+    {
+        try
+        {
+            ConfigurationSourceUtils.valueCount(null, KEY);
+            fail("No exception for null source!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests the size() implementation if this method is implemented by the
+     * source.
+     */
+    public void testSizeImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        final int size = 256;
+        EasyMock.expect(source.size()).andReturn(size);
+        EasyMock.replay(source);
+        assertEquals("Wrong size", size, ConfigurationSourceUtils.size(source));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the size() implementation if this method is not implemented by the
+     * source.
+     */
+    public void testSizeNotImplemented()
+    {
+        ConfigurationSource source = EasyMock
+                .createMock(ConfigurationSource.class);
+        final int size = 128;
+        EasyMock.expect(source.size()).andThrow(
+                new UnsupportedOperationException());
+        Collection<String> keys = new ArrayList<String>(size);
+        for (int i = 0; i < size; i++)
+        {
+            keys.add(KEY + i);
+        }
+        EasyMock.expect(source.getKeys()).andReturn(keys.iterator());
+        for (String k : keys)
+        {
+            EasyMock.expect(source.valueCount(k)).andReturn(1);
+        }
+        EasyMock.replay(source);
+        assertEquals("Wrong size", size, ConfigurationSourceUtils.size(source));
+        EasyMock.verify(source);
+    }
+
+    /**
+     * Tests the size() implementation if a null source is passed in. This
+     * should cause an exception.
+     */
+    public void testSizeNull()
+    {
+        try
+        {
+            ConfigurationSourceUtils.size(null);
+            fail("No exception for null source!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message