You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/03/01 21:17:20 UTC

svn commit: r1573221 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java

Author: oheger
Date: Sat Mar  1 20:17:20 2014
New Revision: 1573221

URL: http://svn.apache.org/r1573221
Log:
Improved key validation when adding nodes to a model.

It is no longer possible to add new nodes to a key pointing to an attribute.

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=1573221&r1=1573220&r2=1573221&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 Sat Mar  1 20:17:20 2014
@@ -218,9 +218,7 @@ public class InMemoryNodeModel implement
                     {
                         if (results.get(0).isAttributeResult())
                         {
-                            throw new IllegalArgumentException(
-                                    "New nodes cannot be added to an attribute key: "
-                                            + key);
+                            throw attributeKeyException(key);
                         }
                         tx.addAddNodesOperation(results.get(0).getNode(), nodes);
                     }
@@ -229,6 +227,10 @@ public class InMemoryNodeModel implement
                         NodeAddData<ImmutableNode> addData =
                                 resolver.resolveAddKey(tx.getCurrentData()
                                         .getRoot(), key, InMemoryNodeModel.this);
+                        if (addData.isAttribute())
+                        {
+                            throw attributeKeyException(key);
+                        }
                         ImmutableNode newNode =
                                 new ImmutableNode.Builder(nodes.size())
                                         .name(addData.getNewNodeName())
@@ -705,6 +707,20 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Creates an exception referring to an invalid key for adding properties.
+     * Such an exception is thrown when an operation tries to add something to
+     * an attribute.
+     *
+     * @param key the invalid key causing this exception
+     * @return the exception
+     */
+    private static RuntimeException attributeKeyException(String key)
+    {
+        return new IllegalArgumentException(
+                "New nodes cannot be added to an attribute key: " + key);
+    }
+
+    /**
      * An internally used helper class for storing information about the managed
      * node structure. An instance of this class represents the current tree. It
      * stores the current root node and additional information which is not part

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=1573221&r1=1573220&r2=1573221&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 Sat Mar  1 20:17:20 2014
@@ -1202,6 +1202,32 @@ public class TestInMemoryNodeModel
     }
 
     /**
+     * Tries to add new nodes to an non-existing key pointing to an attribute.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddNodesToNewAttributeKey()
+    {
+        NodeKeyResolver<ImmutableNode> resolver = createResolver();
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_AUTHORS_TREE);
+        EasyMock.expect(
+                resolver.resolveKey(NodeStructureHelper.ROOT_AUTHORS_TREE, KEY,
+                        model)).andReturn(
+                Collections.<QueryResult<ImmutableNode>> emptyList());
+        EasyMock.expect(
+                resolver.resolveAddKey(NodeStructureHelper.ROOT_AUTHORS_TREE,
+                        KEY, model)).andReturn(
+                new NodeAddData<ImmutableNode>(
+                        NodeStructureHelper.ROOT_AUTHORS_TREE, "test", true,
+                        null));
+        EasyMock.replay(resolver);
+
+        ImmutableNode newNode =
+                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.
      *