Author: oheger
Date: Sun Feb 23 18:34:56 2014
New Revision: 1571048

URL: http://svn.apache.org/r1571048
Log:
Added a clearProperty() method to InMemoryNodeModel.

The method is partly implemented for properties represented by nodes.

Modified:
    
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
    
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.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=1571048&r1=1571047&r2=1571048&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:34:56 2014
@@ -225,7 +225,7 @@ public class InMemoryNodeModel implement
             public boolean initTransaction(ModelTransaction tx) {
                 TreeData currentStructure = tx.getCurrentData();
                 for (QueryResult<ImmutableNode> result : resolver.resolveKey(
-                        tx.getCurrentData().getRoot(), key, 
InMemoryNodeModel.this)) {
+                        currentStructure.getRoot(), key, 
InMemoryNodeModel.this)) {
                     if (result.isAttributeResult()) {
                         tx.addRemoveAttributeOperation(result.getNode(),
                                 result.getAttributeName());
@@ -246,6 +246,34 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Clears the value of a property. This method is similar to
+     * {@link #clearTree(String, NodeKeyResolver)}: However, the nodes
+     * referenced by the passed in key are not removed completely, but only
+     * their value is set to <b>null</b>. (If this operation leaves the 
affected
+     * node in an undefined state, it is indeed removed.)
+     *
+     * @param key the key selecting the properties to be cleared
+     * @param resolver the {@code NodeKeyResolver}
+     */
+    public void clearProperty(final String key, final NodeKeyResolver resolver)
+    {
+        updateModel(new TransactionInitializer()
+        {
+            public boolean initTransaction(ModelTransaction tx)
+            {
+                for (QueryResult<ImmutableNode> result : resolver.resolveKey(tx
+                        .getCurrentData().getRoot(), key,
+                        InMemoryNodeModel.this))
+                {
+                    // TODO handle attribute results
+                    tx.addClearNodeValueOperation(result.getNode());
+                }
+                return true;
+            }
+        });
+    }
+
+    /**
      * Removes all data from this model. A new empty root node is created with
      * the same name as the current root node. Implementation note: Because 
this
      * is a hard reset the usual dance for dealing with concurrent updates is

Modified: 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java?rev=1571048&r1=1571047&r2=1571048&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
 Sun Feb 23 18:34:56 2014
@@ -191,6 +191,17 @@ class ModelTransaction
     }
 
     /**
+     * Adds an operation for clearing the value of a target node.
+     *
+     * @param target the target node
+     */
+    public void addClearNodeValueOperation(ImmutableNode target)
+    {
+        fetchOperations(target, LEVEL_UNKNOWN).addOperation(
+                new ClearNodeValueOperation());
+    }
+
+    /**
      * Executes this transaction resulting in a new {@code TreeData} object. 
The
      * object returned by this method serves as the definition of a new node
      * structure for the calling model.
@@ -717,6 +728,19 @@ class ModelTransaction
     }
 
     /**
+     * A specialized operation class which clears the value of a node.
+     */
+    private class ClearNodeValueOperation extends Operation
+    {
+        @Override
+        protected ImmutableNode apply(ImmutableNode target,
+                Operations operations)
+        {
+            return target.setValue(null);
+        }
+    }
+
+    /**
      * A helper class which collects multiple update operations to be executed
      * on a single node.
      */

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=1571048&r1=1571047&r2=1571048&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:34:56 2014
@@ -946,4 +946,26 @@ public class TestInMemoryNodeModel
         assertEquals("Not all authors were created", threadCount,
                 indices.size());
     }
+
+    /**
+     * Tests whether a property value can be cleared on a node.
+     */
+    @Test
+    public void testClearPropertyNode()
+    {
+        NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_PERSONAE_TREE);
+        final String nodeKey =
+                "Ariel/The Tempest/" + NodeStructureHelper.ELEM_ORG_VALUE;
+        EasyMock.expect(resolver.resolveKey(model.getRootNode(), KEY, model))
+                .andReturn(
+                        Collections.singletonList(QueryResult
+                                .createNodeResult(nodeForKey(model, 
nodeKey))));
+        EasyMock.replay(resolver);
+
+        model.clearProperty(KEY, resolver);
+        ImmutableNode node = nodeForKey(model, nodeKey);
+        assertNull("Value not cleared", node.getValue());
+    }
 }


Reply via email to