commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1580042 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java
Date Fri, 21 Mar 2014 20:36:23 GMT
Author: oheger
Date: Fri Mar 21 20:36:23 2014
New Revision: 1580042

URL: http://svn.apache.org/r1580042
Log:
Added trackChildNodes() method to InMemoryNodeModel.

This method allows tracking of all children of a selected node. It will be used
for the new implementation of childConfigurationsAt().

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java?rev=1580042&r1=1580041&r2=1580042&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
Fri Mar 21 20:36:23 2014
@@ -444,6 +444,51 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Tracks all nodes which are children of the node selected by the passed in
+     * key. If the key selects exactly one node, for all children of this node
+     * {@code NodeSelector} objects are created, and they become tracked nodes.
+     * The returned collection of {@code NodeSelector} objects can be used for
+     * interacting with the selected nodes.
+     *
+     * @param key the key for selecting the parent node whose children are to be
+     *        tracked
+     * @param resolver the {@code NodeKeyResolver}
+     * @return a collection with the {@code NodeSelector} objects for the new
+     *         tracked nodes
+     */
+    public Collection<NodeSelector> trackChildNodes(String key,
+            NodeKeyResolver<ImmutableNode> resolver)
+    {
+        Mutable<Collection<NodeSelector>> refSelectors =
+                new MutableObject<Collection<NodeSelector>>();
+        boolean done;
+        do
+        {
+            refSelectors.setValue(Collections.<NodeSelector> emptyList());
+            TreeData current = structure.get();
+            List<ImmutableNode> nodes =
+                    resolver.resolveNodeKey(current.getRootNode(), key, current);
+            if (nodes.size() == 1)
+            {
+                ImmutableNode node = nodes.get(0);
+                done =
+                        node.getChildren().isEmpty()
+                                || structure.compareAndSet(
+                                        current,
+                                        createSelectorsForTrackedNodes(
+                                                refSelectors,
+                                                node.getChildren(), current,
+                                                resolver));
+            }
+            else
+            {
+                done = true;
+            }
+        } while (!done);
+        return refSelectors.getValue();
+    }
+
+    /**
      * Returns the current {@code ImmutableNode} instance associated with the
      * given {@code NodeSelector}. The node must be a tracked node, i.e.
      * {@link #trackNode(NodeSelector, NodeKeyResolver)} must have been called

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java?rev=1580042&r1=1580041&r2=1580042&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModelTrackedNodes.java
Fri Mar 21 20:36:23 2014
@@ -804,7 +804,7 @@ public class TestInMemoryNodeModelTracke
         NodeKeyResolver<ImmutableNode> resolver = createResolver(false);
         EasyMock.expect(
                 resolver.resolveNodeKey(root, TEST_KEY, model.getNodeHandler()))
-                .andReturn(Collections.<ImmutableNode> emptyList());
+                .andReturn(Collections.<ImmutableNode>emptyList());
         EasyMock.replay(resolver);
 
         assertTrue("Got selectors",
@@ -835,4 +835,91 @@ public class TestInMemoryNodeModelTracke
         model.untrackNode(selector);
         assertSame("Node not tracked", node, model.getTrackedNode(selector));
     }
+
+    /**
+     * Tests whether all children of a node can be tracked at once.
+     */
+    @Test
+    public void testTrackChildNodes()
+    {
+        NodeKeyResolver<ImmutableNode> resolver = createResolver(false);
+        ImmutableNode node = NodeStructureHelper.nodeForKey(root, "tables");
+        String[] keys = new String[node.getChildren().size()];
+        for (int i = 0; i < keys.length; i++)
+        {
+            ImmutableNode child = node.getChildren().get(i);
+            keys[i] =
+                    String.format("%s.%s(%d)", node.getNodeName(),
+                            child.getNodeName(), i);
+            expectNodeKey(resolver, child, keys[i]);
+        }
+        EasyMock.expect(
+                resolver.resolveNodeKey(root, TEST_KEY, model.getNodeHandler()))
+                .andReturn(Collections.singletonList(node));
+        EasyMock.replay(resolver);
+
+        Collection<NodeSelector> selectors =
+                model.trackChildNodes(TEST_KEY, resolver);
+        assertEquals("Wrong number of selectors", node.getChildren().size(),
+                selectors.size());
+        int idx = 0;
+        for (NodeSelector sel : selectors)
+        {
+            assertEquals("Wrong selector", new NodeSelector(keys[idx]), sel);
+            assertEquals("Wrong tracked node for " + sel, node.getChildren()
+                    .get(idx), model.getTrackedNode(sel));
+            idx++;
+        }
+    }
+
+    /**
+     * Checks trackChildNodes() if the passed in key has a result set which
+     * causes the operation to be aborted.
+     *
+     * @param queryResult the result set of the key
+     */
+    private void checkTrackChildNodesNoResult(List<ImmutableNode> queryResult)
+    {
+        NodeKeyResolver<ImmutableNode> resolver = createResolver(false);
+        EasyMock.expect(
+                resolver.resolveNodeKey(root, TEST_KEY, model.getNodeHandler()))
+                .andReturn(queryResult);
+        EasyMock.replay(resolver);
+        TreeData oldData = model.getTreeData();
+
+        assertTrue("Got selectors", model.trackChildNodes(TEST_KEY, resolver)
+                .isEmpty());
+        assertSame("Model was changed", oldData, model.getTreeData());
+    }
+
+    /**
+     * Tests trackChildNodes() for a key that does not return any results.
+     */
+    @Test
+    public void testTrackChildNodesNoResults()
+    {
+        checkTrackChildNodesNoResult(Collections.<ImmutableNode> emptyList());
+    }
+
+    /**
+     * Tests trackChildNodes() for a key that returns more than a single result.
+     */
+    @Test
+    public void testTrackChildNodesMultipleResults()
+    {
+        checkTrackChildNodesNoResult(Arrays.asList(
+                NodeStructureHelper.nodeForKey(root, "tables/table(0)"),
+                NodeStructureHelper.nodeForKey(root, "tables/table(1)")));
+    }
+
+    /**
+     * Tests trackChildNodes() for a key pointing to a node with no children.
+     */
+    @Test
+    public void testTrackChildNodesNodeWithNoChildren()
+    {
+        checkTrackChildNodesNoResult(Collections
+                .singletonList(NodeStructureHelper.nodeForKey(root,
+                        "tables/table(0)/name")));
+    }
 }



Mime
View raw message