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/02/23 19:36:28 UTC
svn commit: r1571050 - 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: Sun Feb 23 18:36:28 2014
New Revision: 1571050
URL: http://svn.apache.org/r1571050
Log:
Added a setProperty() method to InMemoryNodeModel.
This method was partially implemented for dealing with new values to be added.
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=1571050&r1=1571049&r2=1571050&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:36:28 2014
@@ -197,14 +197,7 @@ public class InMemoryNodeModel implement
{
updateModel(new TransactionInitializer() {
public boolean initTransaction(ModelTransaction tx) {
- NodeAddData<ImmutableNode> addData =
- resolver.resolveAddKey(tx.getCurrentData().getRoot(), key,
- InMemoryNodeModel.this);
- if (addData.isAttribute()) {
- addAttributeProperty(tx, addData, values);
- } else {
- addNodeProperty(tx, addData, values);
- }
+ initializeAddTransaction(tx, key, values, resolver);
return true;
}
});
@@ -212,6 +205,42 @@ public class InMemoryNodeModel implement
}
/**
+ * Changes the value of a property. This is a more complex operation as it
+ * might involve adding, updating, or deleting nodes and attributes from the
+ * model. The object representing the new value is passed to the
+ * {@code NodeKeyResolver} which will produce a corresponding
+ * {@link NodeUpdateData} object. Based on the content of this object,
+ * update operations are performed.
+ *
+ * @param key the key
+ * @param value the new value for this property (to be evaluated by the
+ * {@code NodeKeyResolver})
+ * @param resolver the {@code NodeKeyResolver}
+ */
+ public void setProperty(final String key, final Object value,
+ final NodeKeyResolver resolver)
+ {
+ updateModel(new TransactionInitializer()
+ {
+ public boolean initTransaction(ModelTransaction tx)
+ {
+ NodeUpdateData<ImmutableNode> updateData =
+ resolver.resolveUpdateKey(
+ tx.getCurrentData().getRoot(), key, value,
+ InMemoryNodeModel.this);
+ if (!updateData.getNewValues().isEmpty())
+ {
+ initializeAddTransaction(tx, key,
+ updateData.getNewValues(), resolver);
+ }
+ // TODO handle removed nodes
+ // TODO handle updated nodes
+ return true;
+ }
+ });
+ }
+
+ /**
* Removes the sub trees defined by the given key from this model. All nodes
* selected by this key are retrieved and removed from the model. If this
* causes other nodes to become undefined, they are removed, too.
@@ -221,16 +250,24 @@ public class InMemoryNodeModel implement
*/
public void clearTree(final String key, final NodeKeyResolver resolver)
{
- updateModel(new TransactionInitializer() {
- public boolean initTransaction(ModelTransaction tx) {
+ updateModel(new TransactionInitializer()
+ {
+ public boolean initTransaction(ModelTransaction tx)
+ {
TreeData currentStructure = tx.getCurrentData();
- for (QueryResult<ImmutableNode> result : resolver.resolveKey(
- currentStructure.getRoot(), key, InMemoryNodeModel.this)) {
- if (result.isAttributeResult()) {
+ for (QueryResult<ImmutableNode> result : resolver
+ .resolveKey(currentStructure.getRoot(), key,
+ InMemoryNodeModel.this))
+ {
+ if (result.isAttributeResult())
+ {
tx.addRemoveAttributeOperation(result.getNode(),
result.getAttributeName());
- } else {
- if (result.getNode() == currentStructure.getRoot()) {
+ }
+ else
+ {
+ if (result.getNode() == currentStructure.getRoot())
+ {
// the whole model is to be cleared
clear();
return false;
@@ -292,8 +329,8 @@ public class InMemoryNodeModel implement
new ImmutableNode.Builder().name(getRootNode().getNodeName())
.create();
structure.set(new TreeData(newRoot, Collections
- .<ImmutableNode, ImmutableNode> emptyMap(), Collections
- .<ImmutableNode, ImmutableNode> emptyMap()));
+ .<ImmutableNode, ImmutableNode>emptyMap(), Collections
+ .<ImmutableNode, ImmutableNode>emptyMap()));
}
/**
@@ -347,6 +384,29 @@ public class InMemoryNodeModel implement
}
/**
+ * Initializes a transaction for an add operation.
+ *
+ * @param tx the transaction to be initialized
+ * @param key the key
+ * @param values the collection with node values
+ * @param resolver the {@code NodeKeyResolver}
+ */
+ private void initializeAddTransaction(ModelTransaction tx, String key,
+ Iterable<?> values, NodeKeyResolver resolver)
+ {
+ NodeAddData<ImmutableNode> addData =
+ resolver.resolveAddKey(tx.getCurrentData().getRoot(), key, this);
+ if (addData.isAttribute())
+ {
+ addAttributeProperty(tx, addData, values);
+ }
+ else
+ {
+ addNodeProperty(tx, addData, values);
+ }
+ }
+
+ /**
* Creates a {@code TreeData} object for the specified root node.
*
* @param root the root node of the current tree
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=1571050&r1=1571049&r2=1571050&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:36:28 2014
@@ -992,4 +992,32 @@ public class TestInMemoryNodeModel
ImmutableNode node = nodeForKey(model, nodeKey);
assertTrue("Attribute not removed", node.getAttributes().isEmpty());
}
+
+ /**
+ * Tests whether setProperty() can handle newly added values.
+ */
+ @Test
+ public void testSetPropertyNewValues()
+ {
+ NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+ NodeAddData<ImmutableNode> addData =
+ new NodeAddData<ImmutableNode>(nodeForKey(ROOT_AUTHORS_TREE,
+ "Homer"), "work", false, null);
+ NodeUpdateData<ImmutableNode> updateData =
+ new NodeUpdateData<ImmutableNode>(null,
+ Collections.<Object> singleton("Odyssee"), null, KEY);
+ InMemoryNodeModel model = new InMemoryNodeModel(ROOT_AUTHORS_TREE);
+ EasyMock.expect(
+ resolver.resolveUpdateKey(ROOT_AUTHORS_TREE, KEY, this, model))
+ .andReturn(updateData);
+ EasyMock.expect(resolver.resolveAddKey(ROOT_AUTHORS_TREE, KEY, model))
+ .andReturn(addData);
+ EasyMock.replay(resolver);
+
+ model.setProperty(KEY, this, resolver);
+ ImmutableNode node = nodeForKey(model, "Homer/work");
+ assertEquals("Wrong node value", "Odyssee", node.getValue());
+ assertNotNull("Could not find other nodes",
+ nodeForKey(model, "Homer/Ilias/Hektor"));
+ }
}