Author: oheger
Date: Sun Feb 23 18:38:03 2014
New Revision: 1571053

URL: http://svn.apache.org/r1571053
Log:
InMemoryModel.setProperty() now handles nodes to be removed.

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/TestInMemoryNodeModel.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=1571053&r1=1571052&r2=1571053&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
 Sun Feb 23 18:38:03 2014
@@ -233,7 +233,7 @@ public class InMemoryNodeModel implement
                     initializeAddTransaction(tx, key,
                             updateData.getNewValues(), resolver);
                 }
-                // TODO handle removed nodes
+                initializeClearTransaction(tx, updateData.getRemovedNodes());
                 // TODO handle updated nodes
                 return true;
             }
@@ -298,20 +298,10 @@ public class InMemoryNodeModel implement
         {
             public boolean initTransaction(ModelTransaction tx)
             {
-                for (QueryResult<ImmutableNode> result : resolver.resolveKey(tx
-                        .getCurrentData().getRoot(), key,
-                        InMemoryNodeModel.this))
-                {
-                    if (result.isAttributeResult())
-                    {
-                        tx.addRemoveAttributeOperation(result.getNode(),
-                                result.getAttributeName());
-                    }
-                    else
-                    {
-                        tx.addClearNodeValueOperation(result.getNode());
-                    }
-                }
+                List<QueryResult<ImmutableNode>> results =
+                        resolver.resolveKey(tx.getCurrentData().getRoot(), key,
+                                InMemoryNodeModel.this);
+                initializeClearTransaction(tx, results);
                 return true;
             }
         });
@@ -538,6 +528,31 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Initializes a transaction to clear the values of a property based on the
+     * passed in collection of affected results.
+     *
+     * @param tx the transaction to be initialized
+     * @param results a collection with results pointing to the nodes to be
+     *        cleared
+     */
+    private static void initializeClearTransaction(ModelTransaction tx,
+            Collection<QueryResult<ImmutableNode>> results)
+    {
+        for (QueryResult<ImmutableNode> result : results)
+        {
+            if (result.isAttributeResult())
+            {
+                tx.addRemoveAttributeOperation(result.getNode(),
+                        result.getAttributeName());
+            }
+            else
+            {
+                tx.addClearNodeValueOperation(result.getNode());
+            }
+        }
+    }
+
+    /**
      * Determines the initial root node of this model. If a root node has been
      * provided, it is used. Otherwise, an empty dummy root node is created.
      *

Modified: 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java?rev=1571053&r1=1571052&r2=1571053&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java
 Sun Feb 23 18:38:03 2014
@@ -1020,4 +1020,31 @@ public class TestInMemoryNodeModel
         assertNotNull("Could not find other nodes",
                 nodeForKey(model, "Homer/Ilias/Hektor"));
     }
+
+    /**
+     * Tests whether setProperty() can handle nodes to be cleared.
+     */
+    @Test
+    public void testSetPropertyClearValues()
+    {
+        NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_PERSONAE_TREE);
+        final String nodeKey =
+                "Ariel/The Tempest/" + NodeStructureHelper.ELEM_ORG_VALUE;
+        NodeUpdateData<ImmutableNode> updateData =
+                new NodeUpdateData<ImmutableNode>(null, null,
+                        Collections.singletonList(QueryResult
+                                .createNodeResult(nodeForKey(model, nodeKey))),
+                        null);
+        EasyMock.expect(
+                resolver.resolveUpdateKey(
+                        NodeStructureHelper.ROOT_PERSONAE_TREE, KEY, this,
+                        model)).andReturn(updateData);
+        EasyMock.replay(resolver);
+
+        model.setProperty(KEY, this, resolver);
+        ImmutableNode node = nodeForKey(model, nodeKey);
+        assertNull("Value not cleared", node.getValue());
+    }
 }


Reply via email to