Author: oheger
Date: Sun Feb 23 18:43:49 2014
New Revision: 1571062

URL: http://svn.apache.org/r1571062
Log:
InMemoryNodeModel.addNodes() now handles null and empty collections.

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=1571062&r1=1571061&r2=1571062&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:43:49 2014
@@ -211,7 +211,7 @@ public class InMemoryNodeModel implement
      * selects exactly one node) or to a newly created node.
      *
      * @param key the key
-     * @param nodes the collection of nodes to be added
+     * @param nodes the collection of nodes to be added (may be <b>null</b>)
      * @param resolver the {@code NodeKeyResolver}
      * @throws IllegalArgumentException if the key references an attribute (of
      *         course, it is not possible to add something to an attribute)
@@ -220,38 +220,41 @@ public class InMemoryNodeModel implement
             final Collection<ImmutableNode> nodes,
             final NodeKeyResolver resolver)
     {
-        updateModel(new TransactionInitializer()
+        if (nodes != null && !nodes.isEmpty())
         {
-            public boolean initTransaction(ModelTransaction tx)
+            updateModel(new TransactionInitializer()
             {
-                List<QueryResult<ImmutableNode>> results =
-                        resolver.resolveKey(tx.getCurrentData().getRoot(), key,
-                                InMemoryNodeModel.this);
-                if (results.size() == 1)
+                public boolean initTransaction(ModelTransaction tx)
                 {
-                    if (results.get(0).isAttributeResult())
+                    List<QueryResult<ImmutableNode>> results =
+                            resolver.resolveKey(tx.getCurrentData().getRoot(),
+                                    key, InMemoryNodeModel.this);
+                    if (results.size() == 1)
                     {
-                        throw new IllegalArgumentException(
-                                "New nodes cannot be added to an attribute 
key: "
-                                        + key);
+                        if (results.get(0).isAttributeResult())
+                        {
+                            throw new IllegalArgumentException(
+                                    "New nodes cannot be added to an attribute 
key: "
+                                            + key);
+                        }
+                        tx.addAddNodesOperation(results.get(0).getNode(), 
nodes);
                     }
-                    tx.addAddNodesOperation(results.get(0).getNode(), nodes);
-                }
-                else
-                {
-                    NodeAddData<ImmutableNode> addData =
-                            resolver.resolveAddKey(tx.getCurrentData()
-                                    .getRoot(), key, InMemoryNodeModel.this);
-                    ImmutableNode newNode =
-                            new ImmutableNode.Builder(nodes.size())
-                                    .name(addData.getNewNodeName())
-                                    .addChildren(nodes).create();
-                    addNodesByAddData(tx, addData,
-                            Collections.singleton(newNode));
+                    else
+                    {
+                        NodeAddData<ImmutableNode> addData =
+                                resolver.resolveAddKey(tx.getCurrentData()
+                                        .getRoot(), key, 
InMemoryNodeModel.this);
+                        ImmutableNode newNode =
+                                new ImmutableNode.Builder(nodes.size())
+                                        .name(addData.getNewNodeName())
+                                        .addChildren(nodes).create();
+                        addNodesByAddData(tx, addData,
+                                Collections.singleton(newNode));
+                    }
+                    return true;
                 }
-                return true;
-            }
-        });
+            });
+        }
     }
 
     /**

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=1571062&r1=1571061&r2=1571062&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:43:49 2014
@@ -31,6 +31,7 @@ import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -1187,4 +1188,40 @@ public class TestInMemoryNodeModel
                 new ImmutableNode.Builder().name("newNode").create();
         model.addNodes(KEY, Collections.singleton(newNode), resolver);
     }
+
+    /**
+     * Helper method for testing the behavior of addNodes() if no nodes to be
+     * added are provided.
+     *
+     * @param newNodes the collection with new nodes
+     */
+    private void checkAddNodesNoNodes(Collection<ImmutableNode> newNodes)
+    {
+        NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_AUTHORS_TREE);
+        EasyMock.replay(resolver);
+
+        model.addNodes(KEY, newNodes, resolver);
+        assertSame("Model was changed", NodeStructureHelper.ROOT_AUTHORS_TREE,
+                model.getRootNode());
+    }
+
+    /**
+     * Tests an add nodes operation if a null collection is passed in.
+     */
+    @Test
+    public void testAddNodesNullCollection()
+    {
+        checkAddNodesNoNodes(null);
+    }
+
+    /**
+     * Tests an add nodes operation if an empty collection is passed in.
+     */
+    @Test
+    public void testAddNodesEmptyCollection()
+    {
+        checkAddNodesNoNodes(Collections.<ImmutableNode> emptySet());
+    }
 }


Reply via email to