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 2008/03/26 12:03:06 UTC
svn commit: r641250 - in
/commons/proper/configuration/branches/configuration2_experimental/src:
main/java/org/apache/commons/configuration2/combined/
test/java/org/apache/commons/configuration2/combined/
Author: oheger
Date: Wed Mar 26 04:03:03 2008
New Revision: 641250
URL: http://svn.apache.org/viewvc?rev=641250&view=rev
Log:
NodeHandler implementation for CombinedNode
Added:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java (with props)
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java (with props)
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java?rev=641250&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java Wed Mar 26 04:03:03 2008
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.combined;
+
+import java.util.List;
+
+import org.apache.commons.configuration2.expr.NodeHandler;
+
+/**
+ * <p>
+ * A <code>NodeHandler</code> implementation for dealing with
+ * <code>CombinedNode</code> objects.
+ * </p>
+ * <p>
+ * This node handler is used by a <code>CombinedConfiguration</code> for
+ * manipulating <code>{@link CombinedNode}</code> instances that are part of
+ * their node structure.
+ * </p>
+ * <p>
+ * Note that the type parameter in the <code>NodeHandler</code> interface is
+ * set to object. This is because a <code>CombinedNodeHandler</code> typically has
+ * to deal with different types of nodes at the same time. The main purpose of a
+ * <code>CombinedNode</code> is to combine the node structures of arbitrary source
+ * configurations. So for instance the children of a <code>CombinedNode</code> can
+ * be of an arbitrary type. Nevertheless do most methods expect a
+ * <code>CombinedNode</code> object in their <code>node</code> parameter and
+ * will throw an <code>IllegalArgumentException</code> exception if an
+ * incompatible object is passed in.
+ * </p>
+ *
+ * @author <a href="http://commons.apache.org/configuration/team-list.html">Commons
+ * Configuration team</a>
+ * @version $Id$
+ * @since 2.0
+ */
+public class CombinedNodeHandler implements NodeHandler<Object>
+{
+ /**
+ * Adds another value to an attribute of the specified combined node.
+ *
+ * @param node the combined node
+ * @param name the name of the attribute
+ * @param value the value to add
+ */
+ public void addAttributeValue(Object node, String name, Object value)
+ {
+ node(node).addAttributeValue(name, value);
+ }
+
+ /**
+ * Adds a new child to the specified combined node. This implementation creates
+ * another <code>CombinedNode</code> instance with the given name and adds it
+ * as new child to the specified parent node.
+ *
+ * @param node the combined node
+ * @param name the name of the new child node
+ * @return the newly created child node
+ */
+ public Object addChild(Object node, String name)
+ {
+ CombinedNode vn = new CombinedNode(name);
+ vn.setParent(node(node));
+ node(node).addChild(name, vn);
+ return vn;
+ }
+
+ /**
+ * Returns the value of the specified attribute of the given combined node.
+ *
+ * @param node the combined node
+ * @param name the name of the attribute in question
+ * @return the value of this attribute
+ */
+ public Object getAttributeValue(Object node, String name)
+ {
+ return node(node).getAttribute(name);
+ }
+
+ /**
+ * Returns a list with the names of the attributes of the given combined node.
+ *
+ * @param node the combined node
+ * @return a list with the names of the defined attributes
+ */
+ public List<String> getAttributes(Object node)
+ {
+ return node(node).getAttributes();
+ }
+
+ /**
+ * Returns the child with the given index.
+ *
+ * @param node the combined node
+ * @param index the index
+ * @return the child with this index
+ */
+ public Object getChild(Object node, int index)
+ {
+ return node(node).getChild(index);
+ }
+
+ /**
+ * Returns a list with all children of the specified node.
+ *
+ * @param node the combined node
+ * @return a list with the child nodes of this combined node
+ */
+ public List<Object> getChildren(Object node)
+ {
+ return node(node).getChildren();
+ }
+
+ /**
+ * Returns a list with all children of the specified node with the given
+ * name.
+ *
+ * @param node the combined node
+ * @param name the name of the desired children
+ * @return a list with all children with this name
+ */
+ public List<Object> getChildren(Object node, String name)
+ {
+ return node(node).getChildren(name);
+ }
+
+ /**
+ * Returns the number of the selected children of the specified combined node.
+ *
+ * @param node the combined node
+ * @param name the name of the children in question
+ * @return the number of the selected children
+ */
+ public int getChildrenCount(Object node, String name)
+ {
+ return node(node).getChildrenCount(name);
+ }
+
+ /**
+ * Returns the parent of the specified node.
+ *
+ * @param node the combined node
+ * @return the parent node
+ */
+ public Object getParent(Object node)
+ {
+ return node(node).getParent();
+ }
+
+ /**
+ * Returns the value of the given node.
+ *
+ * @return the value
+ */
+ public Object getValue(Object node)
+ {
+ return node(node).getValue();
+ }
+
+ /**
+ * Tests whether the passed in node is defined. This implementation checks
+ * whether the node has a value or any attributes.
+ *
+ * @param node the node to test
+ * @return a flag whether this node is defined
+ */
+ public boolean isDefined(Object node)
+ {
+ CombinedNode vn = node(node);
+ return vn.getValue() != null || vn.hasAttributes();
+ }
+
+ /**
+ * Returns the name of the given node.
+ *
+ * @param node the combined node
+ * @return the name of the node
+ */
+ public String nodeName(Object node)
+ {
+ return node(node).getName();
+ }
+
+ /**
+ * Removes an attribute of the specified combined node.
+ *
+ * @param node the combined node
+ * @param name the name of the attribute to remove
+ */
+ public void removeAttribute(Object node, String name)
+ {
+ node(node).removeAttribute(name);
+ }
+
+ /**
+ * Removes the specified child from the given combined node.
+ *
+ * @param node the combined node
+ * @param child the child to remove
+ */
+ public void removeChild(Object node, Object child)
+ {
+ node(node).removeChild(child);
+ }
+
+ /**
+ * Sets the value of an attribute of the specified combined node.
+ *
+ * @param node the combined node
+ * @param name the name of the attribute
+ * @param value the new value
+ */
+ public void setAttributeValue(Object node, String name, Object value)
+ {
+ node(node).setAttribute(name, value);
+ }
+
+ /**
+ * Sets the value of the given node.
+ *
+ * @param node the node
+ * @param value the new value
+ */
+ public void setValue(Object node, Object value)
+ {
+ node(node).setValue(value);
+ }
+
+ /**
+ * Converts the specified node object to a <code>CombinedNode</code>. If this
+ * is not possible, an exception is thrown.
+ *
+ * @param node the object to convert
+ * @return the converted <code>CombinedNode</code>
+ * @throws IllegalArgumentException if the object is not a
+ * <code>CombinedNode</code>
+ */
+ private static CombinedNode node(Object node)
+ {
+ if (!(node instanceof CombinedNode))
+ {
+ throw new IllegalArgumentException("Node must be a ViewNode!");
+ }
+
+ return (CombinedNode) node;
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedNodeHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java?rev=641250&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java Wed Mar 26 04:03:03 2008
@@ -0,0 +1,300 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.combined;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for CombinedNodeHandler.
+ *
+ * @author <a href="http://commons.apache.org/configuration/team-list.html">Commons
+ * Configuration team</a>
+ * @version $Id$
+ */
+public class TestCombinedNodeHandler extends TestCase
+{
+ /** Constant for the prefix of child nodes. */
+ private static final String CHILD_NAME = "child";
+
+ /** Constant for the prefix of attributes. */
+ private static final String ATTR_NAME = "attr";
+
+ /** Constant for the number of children. */
+ private static final int CHILD_COUNT = 10;
+
+ /** Constant for the number of attributes. */
+ private static final int ATTR_COUNT = 8;
+
+ /** Constant for the name of the test view node. */
+ private static final String NAME = "MyTestViewNode";
+
+ /** A combined node that can be used for testing. */
+ private CombinedNode node;
+
+ /** The handler to be tested. */
+ private CombinedNodeHandler handler;
+
+ /**
+ * Initializes the fixture. Creates a combined node with a number children and
+ * attributes. The test handler is also created.
+ */
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ node = new CombinedNode(NAME);
+ for (int i = 0; i < CHILD_COUNT; i++)
+ {
+ String name = CHILD_NAME + i;
+ node.addChild(name, name);
+ }
+ for (int i = 0; i < ATTR_COUNT; i++)
+ {
+ String attr = ATTR_NAME + i;
+ node.setAttribute(attr, attr);
+ }
+ handler = new CombinedNodeHandler();
+ }
+
+ /**
+ * Tests querying the name of the node.
+ */
+ public void testNodeName()
+ {
+ assertEquals("Wrong node name", NAME, handler.nodeName(node));
+ }
+
+ /**
+ * Tests querying the value of the node.
+ */
+ public void testGetValue()
+ {
+ Object value = 42;
+ node.setValue(value);
+ assertEquals("Wrong value", value, handler.getValue(node));
+ }
+
+ /**
+ * Tests setting the value of a node.
+ */
+ public void testSetValue()
+ {
+ Object value = 42;
+ handler.setValue(node, value);
+ assertEquals("Value was not set", value, node.getValue());
+ }
+
+ /**
+ * Tests querying the parent node.
+ */
+ public void testGetParent()
+ {
+ CombinedNode parent = new CombinedNode();
+ node.setParent(parent);
+ assertEquals("Wrong parent", parent, handler.getParent(node));
+ }
+
+ /**
+ * Tests querying the children of a combined node.
+ */
+ public void testGetChildren()
+ {
+ List<?> children = handler.getChildren(node);
+ assertEquals("Wrong number of children", CHILD_COUNT, children.size());
+ for (int i = 0; i < CHILD_COUNT; i++)
+ {
+ assertEquals("Wrong child at " + i, CHILD_NAME + i, children.get(i));
+ }
+ }
+
+ /**
+ * Tests querying the children with the given name.
+ */
+ public void testGetChildrenName()
+ {
+ final String name = CHILD_NAME + "1";
+ List<Object> children = handler.getChildren(node, name);
+ assertEquals("Wrong number of children with name", 1, children.size());
+ assertEquals("Wrong child", name, children.get(0));
+ }
+
+ /**
+ * Tests querying the number of all children.
+ */
+ public void testGetChildrenCount()
+ {
+ assertEquals("Wrong number of children", CHILD_COUNT, handler
+ .getChildrenCount(node, null));
+ }
+
+ /**
+ * Tests querying the number of children with a given name.
+ */
+ public void testGetChildrenCountName()
+ {
+ assertEquals("Wrong number of named children", 1, handler
+ .getChildrenCount(node, CHILD_NAME + "2"));
+ assertEquals("Wrong number of non-existing children", 0, handler
+ .getChildrenCount(node, "unknownChild"));
+ }
+
+ /**
+ * Tests querying a child by its index.
+ */
+ public void testGetChild()
+ {
+ for (int i = 0; i < CHILD_COUNT; i++)
+ {
+ assertEquals("Wrong child at " + i, CHILD_NAME + i, handler
+ .getChild(node, i));
+ }
+ }
+
+ /**
+ * Tests removing a child.
+ */
+ public void testRemoveChild()
+ {
+ Object child = node.getChild(2);
+ handler.removeChild(node, child);
+ List<Object> children = node.getChildren();
+ assertEquals("Wrong number of children", CHILD_COUNT - 1, children
+ .size());
+ assertFalse("Child still present", children.contains(child));
+ }
+
+ /**
+ * Tests adding another child.
+ */
+ public void testAddChild()
+ {
+ final String childName = "newChild";
+ Object newChild = handler.addChild(node, childName);
+ assertEquals("No child added", CHILD_COUNT + 1, node
+ .getChildrenCount(null));
+ CombinedNode child = (CombinedNode) node.getChild(CHILD_COUNT);
+ assertSame("Wrong child", newChild, child);
+ assertEquals("Name was not set", childName, child.getName());
+ }
+
+ /**
+ * Tests querying the names of the existing attributes.
+ */
+ public void testGetAttributes()
+ {
+ List<String> attrs = handler.getAttributes(node);
+ assertEquals("Wrong number of attributes", ATTR_COUNT, attrs.size());
+ for (int i = 0; i < ATTR_COUNT; i++)
+ {
+ assertEquals("Wrong attribute at " + i, ATTR_NAME + i, attrs.get(i));
+ }
+ }
+
+ /**
+ * Tests adding a value to an attribute.
+ */
+ public void testAddAttributeValue()
+ {
+ final String attrName = ATTR_NAME + "3";
+ final Object newVal = "newAttributeValue";
+ handler.addAttributeValue(node, attrName, newVal);
+ List<?> vals = (List<?>) node.getAttribute(attrName);
+ assertEquals("Wrong number of attribute values", 2, vals.size());
+ assertEquals("Wrong value 1", attrName, vals.get(0));
+ assertEquals("Wrong value 2", newVal, vals.get(1));
+ }
+
+ /**
+ * Tests setting the value of an attribute.
+ */
+ public void testSetAttributeValue()
+ {
+ final String attrName = ATTR_NAME + "4";
+ final Object newVal = "anotherValue";
+ handler.setAttributeValue(node, attrName, newVal);
+ assertEquals("Wrong attribute value", newVal, node
+ .getAttribute(attrName));
+ }
+
+ /**
+ * Tests querying the value of an attribute.
+ */
+ public void testGetAttribute()
+ {
+ for (int i = 0; i < ATTR_COUNT; i++)
+ {
+ String attr = ATTR_NAME + i;
+ assertEquals("Wrong value for " + attr, attr, handler
+ .getAttributeValue(node, attr));
+ }
+ }
+
+ /**
+ * Tests removing an attribute.
+ */
+ public void testRemoveAttribute()
+ {
+ final String attr = ATTR_NAME + "1";
+ handler.removeAttribute(node, attr);
+ List<String> attrs = node.getAttributes();
+ assertEquals("No attribute removed", ATTR_COUNT - 1, attrs.size());
+ assertFalse("Attribute still found", attrs.contains(attr));
+ }
+
+ /**
+ * Tests isDefined() for a defined node.
+ */
+ public void testIsDefinedTrue()
+ {
+ assertTrue("Node not defined", handler.isDefined(node));
+ }
+
+ /**
+ * Tests the isDefined() method in multiple variants.
+ */
+ public void testIsDefined()
+ {
+ CombinedNode n = new CombinedNode();
+ assertFalse("Empty node defined", handler.isDefined(n));
+ n.addChild(CHILD_NAME, "test");
+ assertFalse("Node with child defined", handler.isDefined(n));
+ n.setValue("value");
+ assertTrue("Node with value not defined", handler.isDefined(n));
+ n.setValue(null);
+ n.addAttributeValue(ATTR_NAME, "test");
+ assertTrue("Node with attribute not defined", handler.isDefined(n));
+ }
+
+ /**
+ * Tests passing an illegal node to the handler. This should cause an
+ * exception.
+ */
+ public void testIllegalArgument()
+ {
+ try
+ {
+ handler.setValue(this, "new value");
+ fail("Illegal node argument was not detected!");
+ }
+ catch (IllegalArgumentException iex)
+ {
+ // ok
+ }
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedNodeHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain