commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1573220 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java
Date Sat, 01 Mar 2014 20:15:31 GMT
Author: oheger
Date: Sat Mar  1 20:15:31 2014
New Revision: 1573220

URL: http://svn.apache.org/r1573220
Log:
Attribute keys are now taken into account by the getKeys() methods.

Added some more tests related to attribute keys. Fixed the failing tests in
this area.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java?rev=1573220&r1=1573219&r2=1573220&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
Sat Mar  1 20:15:31 2014
@@ -626,15 +626,18 @@ public abstract class AbstractHierarchic
             visitor.getKeyList().add(prefix);
         }
 
-        List<QueryResult<T>> nodes = fetchNodeList(prefix);
+        List<QueryResult<T>> results = fetchNodeList(prefix);
         NodeHandler<T> handler = getModel().getNodeHandler();
 
-        for (QueryResult<T> node : nodes)
+        for (QueryResult<T> result : results)
         {
-            //TODO handle attributes
-            for (T c : handler.getChildren(node.getNode()))
+            if (!result.isAttributeResult())
             {
-                NodeTreeWalker.INSTANCE.walkDFS(c, visitor, handler);
+                for (T c : handler.getChildren(result.getNode()))
+                {
+                    NodeTreeWalker.INSTANCE.walkDFS(c, visitor, handler);
+                }
+                visitor.handleAttributeKeys(prefix, result.getNode(), handler);
             }
         }
 
@@ -877,20 +880,35 @@ public abstract class AbstractHierarchic
         /**
          * {@inheritDoc} If this node has a value, its key is added
          * to the internal list.
-         *
-         * TODO handle attributes
          */
         @Override
         public void visitBeforeChildren(T node, NodeHandler<T> handler)
         {
             String parentKey = parentKeys.isEmpty() ? null
-                    : (String) parentKeys.peek();
+                    : parentKeys.peek();
             String key = getExpressionEngine().nodeKey(node, parentKey, handler);
             parentKeys.push(key);
             if (handler.getValue(node) != null)
             {
                 keyList.add(key);
             }
+            handleAttributeKeys(key, node, handler);
+        }
+
+        /**
+         * Appends all attribute keys of the current node.
+         *
+         * @param parentKey the parent key
+         * @param node the current node
+         * @param handler the {@code NodeHandler}
+         */
+        public void handleAttributeKeys(String parentKey, T node,
+                NodeHandler<T> handler)
+        {
+            for (String attr : handler.getAttributes(node))
+            {
+                keyList.add(getExpressionEngine().attributeKey(parentKey, attr));
+            }
         }
     }
 }

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java?rev=1573220&r1=1573219&r2=1573220&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java
Sat Mar  1 20:15:31 2014
@@ -326,8 +326,14 @@ public class TestAbstractHierarchicalCon
         assertEquals(2, keys.size());
         assertTrue(keys.contains("tables.table.name"));
         assertTrue(keys.contains("tables.table.fields.field.name"));
+    }
 
-        // test the order of the keys returned
+    /**
+     * Tests whether keys are returned in a defined order.
+     */
+    @Test
+    public void testGetKeysOrder()
+    {
         config.addProperty("order.key1", "value1");
         config.addProperty("order.key2", "value2");
         config.addProperty("order.key3", "value3");
@@ -338,6 +344,33 @@ public class TestAbstractHierarchicalCon
         assertEquals("3rd key", "order.key3", it.next());
     }
 
+    /**
+     * Tests whether attribute keys are contained in the iteration of keys.
+     */
+    @Test
+    public void testGetKeysAttribute()
+    {
+        config.addProperty("tables.table(0)[@type]", "system");
+        Set<String> keys = new HashSet<String>();
+        for (Iterator<String> it = config.getKeys(); it.hasNext();)
+        {
+            keys.add(it.next());
+        }
+        assertTrue("Attribute key not found: " + keys, keys.contains("tables.table[@type]"));
+    }
+
+    /**
+     * Tests whether a prefix that points to an attribute is correctly handled.
+     */
+    @Test
+    public void testGetKeysAttributePrefix()
+    {
+        config.addProperty("tables.table(0)[@type]", "system");
+        Iterator<String> itKeys = config.getKeys("tables.table[@type]");
+        assertEquals("Wrong key", "tables.table[@type]", itKeys.next());
+        assertFalse("Too many keys", itKeys.hasNext());
+    }
+
     @Test
     public void testGetKeysString()
     {



Mime
View raw message