Author: oheger
Date: Sun Feb 23 18:39:50 2014
New Revision: 1571055

URL: http://svn.apache.org/r1571055
Log:
InMemoryNodeModel.setProperty() now handles value changes.

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=1571055&r1=1571054&r2=1571055&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:39:50 2014
@@ -234,7 +234,7 @@ public class InMemoryNodeModel implement
                             updateData.getNewValues(), resolver);
                 }
                 initializeClearTransaction(tx, updateData.getRemovedNodes());
-                // TODO handle updated nodes
+                initializeUpdateTransaction(tx, updateData.getChangedValues());
                 return true;
             }
         });
@@ -553,6 +553,32 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Initializes a transaction to change the values of some query results
+     * based on the passed in map.
+     *
+     * @param tx the transaction to be initialized
+     * @param changedValues the map defining the elements to be changed
+     */
+    private static void initializeUpdateTransaction(ModelTransaction tx,
+            Map<QueryResult<ImmutableNode>, Object> changedValues)
+    {
+        for (Map.Entry<QueryResult<ImmutableNode>, Object> e : changedValues
+                .entrySet())
+        {
+            if (e.getKey().isAttributeResult())
+            {
+                tx.addAttributeOperation(e.getKey().getNode(), e.getKey()
+                        .getAttributeName(), e.getValue());
+            }
+            else
+            {
+                tx.addChangeNodeValueOperation(e.getKey().getNode(),
+                        e.getValue());
+            }
+        }
+    }
+
+    /**
      * 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/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=1571055&r1=1571054&r2=1571055&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:39:50 2014
@@ -197,8 +197,20 @@ class ModelTransaction
      */
     public void addClearNodeValueOperation(ImmutableNode target)
     {
+        addChangeNodeValueOperation(target, null);
+    }
+
+    /**
+     * Adds an operation for changing the value of a target node.
+     *
+     * @param target the target node
+     * @param newValue the new value for this node
+     */
+    public void addChangeNodeValueOperation(ImmutableNode target,
+            Object newValue)
+    {
         fetchOperations(target, LEVEL_UNKNOWN).addOperation(
-                new ClearNodeValueOperation());
+                new ChangeNodeValueOperation(newValue));
     }
 
     /**
@@ -728,15 +740,29 @@ class ModelTransaction
     }
 
     /**
-     * A specialized operation class which clears the value of a node.
+     * A specialized operation class which changes the value of a node.
      */
-    private class ClearNodeValueOperation extends Operation
+    private class ChangeNodeValueOperation extends Operation
     {
+        /** The new value for the affected node. */
+        private final Object newValue;
+
+        /**
+         * Creates a new instance of {@code ChangeNodeValueOperation} and
+         * initializes it with the new value to set for the node.
+         *
+         * @param value the new node value
+         */
+        public ChangeNodeValueOperation(Object value)
+        {
+            newValue = value;
+        }
+
         @Override
         protected ImmutableNode apply(ImmutableNode target,
                 Operations operations)
         {
-            return target.setValue(null);
+            return target.setValue(newValue);
         }
     }
 

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=1571055&r1=1571054&r2=1571055&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:39:50 2014
@@ -32,6 +32,7 @@ import static org.junit.Assert.fail;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -1047,4 +1048,38 @@ public class TestInMemoryNodeModel
         ImmutableNode node = nodeForKey(model, nodeKey);
         assertNull("Value not cleared", node.getValue());
     }
+
+    /**
+     * Tests whether setProperty() can handle changes in node values.
+     */
+    @Test
+    public void testSetPropertyChangedValues()
+    {
+        NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_PERSONAE_TREE);
+        final String nodeKey =
+                "Ariel/The Tempest/" + NodeStructureHelper.ELEM_ORG_VALUE;
+        Map<QueryResult<ImmutableNode>, Object> changedValues =
+                new HashMap<QueryResult<ImmutableNode>, Object>();
+        final String newValue = "of course";
+        ImmutableNode changedNode = nodeForKey(model, nodeKey);
+        changedValues.put(QueryResult.createAttributeResult(changedNode,
+                NodeStructureHelper.ATTR_TESTED), newValue);
+        changedValues.put(QueryResult.createNodeResult(changedNode), newValue);
+        NodeUpdateData<ImmutableNode> updateData =
+                new NodeUpdateData<ImmutableNode>(changedValues, null, null,
+                        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);
+        assertEquals("Attribute value not changed", newValue, node
+                .getAttributes().get(NodeStructureHelper.ATTR_TESTED));
+        assertEquals("Node value not changed", newValue, node.getValue());
+    }
 }


Reply via email to