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/02/29 21:55:37 UTC

svn commit: r632437 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/expr/ test/java/org/apache/commons/configuration2/expr/

Author: oheger
Date: Fri Feb 29 12:55:33 2008
New Revision: 632437

URL: http://svn.apache.org/viewvc?rev=632437&view=rev
Log:
Initial implementation of a node handler for configuration nodes including test class

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java   (with props)
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java   (with props)

Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java?rev=632437&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java Fri Feb 29 12:55:33 2008
@@ -0,0 +1,223 @@
+/*
+ * 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.expr;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.configuration2.tree.ConfigurationNode;
+import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
+
+/**
+ * <p>
+ * An implementation of the <code>{@link NodeHandler}</code> interface that
+ * operates on <code>ConfigurationNode</code> objects.
+ * </p>
+ *
+ * @author Oliver Heger
+ * @version $Id$
+ */
+public class ConfigurationNodeHandler implements NodeHandler<ConfigurationNode>
+{
+    /**
+     * Creates a new child node with the given name and adds it to the specified
+     * node.
+     *
+     * @param node the node
+     * @param name the name of the new child
+     * @return the new child node
+     */
+    public ConfigurationNode addChild(ConfigurationNode node, String name)
+    {
+        ConfigurationNode child = createNode(node, name);
+        node.addChild(child);
+        return child;
+    }
+
+    /**
+     * Returns the value of the attribute with the given name. This
+     * implementation supports only a single attribute value. If the attribute
+     * is present multiple times, only the value of the first occurrence is
+     * returned. If the attribute cannot be found, result is <b>null</b>.
+     *
+     * @param node the node
+     * @param name the name of the desired attribute
+     * @return the value of this attribute
+     */
+    public Object getAttributeValue(ConfigurationNode node, String name)
+    {
+        List<ConfigurationNode> attrs = node.getAttributes(name);
+        return (attrs.isEmpty()) ? null : attrs.get(0).getValue();
+    }
+
+    /**
+     * Returns a list with the names of the attributes of the given node.
+     *
+     * @param node the node
+     * @return a list with the names of the existing attributes
+     */
+    public List<String> getAttributes(ConfigurationNode node)
+    {
+        List<ConfigurationNode> attrs = node.getAttributes();
+        assert attrs != null : "Attribute list is null";
+        List<String> names = new ArrayList<String>(attrs.size());
+        for (ConfigurationNode n : attrs)
+        {
+            names.add(n.getName());
+        }
+        return names;
+    }
+
+    /**
+     * Returns the child with the given index from the specified node.
+     *
+     * @param node the node
+     * @param index the index
+     * @return the child with this index
+     */
+    public ConfigurationNode getChild(ConfigurationNode node, int index)
+    {
+        return node.getChild(index);
+    }
+
+    /**
+     * Returns a list with all children of the specified node.
+     *
+     * @param node the node
+     * @return a list with the children of this node
+     */
+    public List<ConfigurationNode> getChildren(ConfigurationNode node)
+    {
+        return node.getChildren();
+    }
+
+    /**
+     * Returns a list with all children of the specified node with the given
+     * name.
+     *
+     * @param node the node
+     * @param name the name of the children
+     * @return a list with all children with this name
+     */
+    public List<ConfigurationNode> getChildren(ConfigurationNode node,
+            String name)
+    {
+        return node.getChildren(name);
+    }
+
+    /**
+     * Returns the parent of the specified node.
+     *
+     * @param node the node
+     * @return the parent node
+     */
+    public ConfigurationNode getParent(ConfigurationNode node)
+    {
+        return node.getParentNode();
+    }
+
+    /**
+     * Returns the value of the given node.
+     *
+     * @param node the node
+     * @return the value of the node
+     */
+    public Object getValue(ConfigurationNode node)
+    {
+        return node.getValue();
+    }
+
+    /**
+     * Returns the name of the specified node.
+     *
+     * @param node the node
+     * @return the name of this node
+     */
+    public String nodeName(ConfigurationNode node)
+    {
+        return node.getName();
+    }
+
+    /**
+     * Sets the value of the specified attribute. This implementation only
+     * supports a single value per attribute. So any existing attributes with
+     * the given name are removed first.
+     *
+     * @param node the node
+     * @param name the name of the attribute to set
+     * @param value the new value
+     */
+    public void setAttributeValue(ConfigurationNode node, String name,
+            Object value)
+    {
+        node.removeAttribute(name);
+        ConfigurationNode attr = createNode(node, name);
+        attr.setValue(value);
+        node.addAttribute(attr);
+    }
+
+    /**
+     * Sets the value of the specified node.
+     *
+     * @param node the node
+     * @param value the new value
+     */
+    public void setValue(ConfigurationNode node, Object value)
+    {
+        node.setValue(value);
+    }
+
+    /**
+     * Creates a new configuration node. This method is called by
+     * <code>addChild()</code> for creating the new child node. This
+     * implementation returns an instance of
+     * <code>DefaultConfigurationNode</code>. Derived classes may override
+     * this method to create a different node implementation.
+     *
+     * @param parent the parent node
+     * @param name the name of the new node
+     * @return the newly created node
+     */
+    protected ConfigurationNode createNode(ConfigurationNode parent, String name)
+    {
+        ConfigurationNode node = new DefaultConfigurationNode(name);
+        node.setParentNode(parent);
+        return node;
+    }
+
+    /**
+     * Removes an attribute from the given node.
+     *
+     * @param node the node
+     * @param name the name of the attribute to be removed
+     */
+    public void removeAttribute(ConfigurationNode node, String name)
+    {
+        node.removeAttribute(name);
+    }
+
+    /**
+     * Removes a child from the given node.
+     *
+     * @param node the node
+     * @param child the child to be removed
+     */
+    public void removeChild(ConfigurationNode node, ConfigurationNode child)
+    {
+        node.removeChild(child);
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java?rev=632437&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java Fri Feb 29 12:55:33 2008
@@ -0,0 +1,277 @@
+/*
+ * 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.expr;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.configuration2.tree.ConfigurationNode;
+import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
+import org.easymock.EasyMock;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for ConfigurationNodeHandler.
+ * 
+ * @author hacker
+ * @version $Id$
+ */
+public class TestConfigurationNodeHandler extends TestCase
+{
+    /** Constant for a node value. */
+    private static final Object VALUE = "TEST";
+
+    /** Constant for the name of a node. */
+    private static final String NAME = "nodeName";
+
+    /** The handler to be tested. */
+    private ConfigurationNodeHandler handler;
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        handler = new ConfigurationNodeHandler();
+    }
+
+    /**
+     * Creates a mock for a configuration node.
+     * 
+     * @return the mock node
+     */
+    private static ConfigurationNode mockNode()
+    {
+        return EasyMock.createMock(ConfigurationNode.class);
+    }
+
+    /**
+     * Tests querying the parent node.
+     */
+    public void testGetParent()
+    {
+        ConfigurationNode node = mockNode();
+        ConfigurationNode ndParent = mockNode();
+        EasyMock.expect(node.getParentNode()).andReturn(ndParent);
+        EasyMock.replay(node, ndParent);
+        assertEquals("Wrong parent node", ndParent, handler.getParent(node));
+        EasyMock.verify(node, ndParent);
+    }
+
+    /**
+     * Tests querying the value of a node.
+     */
+    public void testGetValue()
+    {
+        ConfigurationNode node = mockNode();
+        EasyMock.expect(node.getValue()).andReturn(VALUE);
+        EasyMock.replay(node);
+        assertEquals("Wrong value", VALUE, handler.getValue(node));
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests setting a node's value.
+     */
+    public void testSetValue()
+    {
+        ConfigurationNode node = mockNode();
+        node.setValue(VALUE);
+        EasyMock.replay(node);
+        handler.setValue(node, VALUE);
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests querying the name of a node.
+     */
+    public void testNodeName()
+    {
+        ConfigurationNode node = mockNode();
+        EasyMock.expect(node.getName()).andReturn(NAME);
+        EasyMock.replay(node);
+        assertEquals("Wrong name", NAME, handler.nodeName(node));
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests adding a child node.
+     */
+    public void testAddChild()
+    {
+        ConfigurationNode node = mockNode();
+        node.addChild((ConfigurationNode) EasyMock.anyObject());
+        EasyMock.replay(node);
+        handler.addChild(node, NAME);
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests creating a new node.
+     */
+    public void testCreateNode()
+    {
+        ConfigurationNode parent = mockNode();
+        EasyMock.replay(parent);
+        ConfigurationNode node = handler.createNode(parent, NAME);
+        assertEquals("Wrong parent", parent, node.getParentNode());
+        assertEquals("Wrong name", NAME, node.getName());
+        assertNull("Node has a value", node.getValue());
+    }
+
+    /**
+     * Tests querying a child by its index.
+     */
+    public void testGetChild()
+    {
+        ConfigurationNode node = mockNode();
+        ConfigurationNode child = mockNode();
+        final int index = 2;
+        EasyMock.expect(node.getChild(index)).andReturn(child);
+        EasyMock.replay(node, child);
+        assertEquals("Wrong child node", child, handler.getChild(node, index));
+        EasyMock.verify(node, child);
+    }
+
+    /**
+     * Tests querying all children.
+     */
+    public void testGetChildren()
+    {
+        ConfigurationNode node = mockNode();
+        List<ConfigurationNode> children = new ArrayList<ConfigurationNode>();
+        EasyMock.expect(node.getChildren()).andReturn(children);
+        EasyMock.replay(node);
+        assertSame("Wrong children", children, handler.getChildren(node));
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests querying all children with a given name.
+     */
+    public void testGetChildrenName()
+    {
+        ConfigurationNode node = mockNode();
+        List<ConfigurationNode> children = new ArrayList<ConfigurationNode>();
+        EasyMock.expect(node.getChildren(NAME)).andReturn(children);
+        EasyMock.replay(node);
+        assertSame("Wrong children", children, handler.getChildren(node, NAME));
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests querying the attribute names.
+     */
+    public void testGetAttributes()
+    {
+        ConfigurationNode node = mockNode();
+        final String[] attrNames = {
+                "attr1", "testAttr", "anotherAttr"
+        };
+        List<ConfigurationNode> attrNodes = new ArrayList<ConfigurationNode>();
+        for (String an : attrNames)
+        {
+            ConfigurationNode attr = mockNode();
+            EasyMock.expect(attr.getName()).andStubReturn(an);
+            EasyMock.replay(attr);
+            attrNodes.add(attr);
+        }
+        EasyMock.expect(node.getAttributes()).andReturn(attrNodes);
+        EasyMock.replay(node);
+
+        List<String> attrs = handler.getAttributes(node);
+        assertEquals("Wrong number of attribute names", attrNames.length, attrs
+                .size());
+        for (int i = 0; i < attrNames.length; i++)
+        {
+            assertEquals("Wrong attribute name at " + i, attrNames[i], attrs
+                    .get(i));
+        }
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests querying the value of an attribute.
+     */
+    public void testGetAttributeValue()
+    {
+        ConfigurationNode node = mockNode();
+        ConfigurationNode attr = mockNode();
+        EasyMock.expect(attr.getValue()).andReturn(VALUE);
+        List<ConfigurationNode> attrs = new ArrayList<ConfigurationNode>(1);
+        attrs.add(attr);
+        EasyMock.expect(node.getAttributes(NAME)).andReturn(attrs);
+        EasyMock.replay(node, attr);
+        assertEquals("Wrong value for attribute", VALUE, handler
+                .getAttributeValue(node, NAME));
+        EasyMock.verify(node, attr);
+    }
+
+    /**
+     * Tests querying the value of a non-existing attribute. Result should be
+     * null.
+     */
+    public void testGetAttributeValueUnknown()
+    {
+        ConfigurationNode node = mockNode();
+        EasyMock.expect(node.getAttributes(NAME)).andReturn(
+                new ArrayList<ConfigurationNode>(0));
+        EasyMock.replay(node);
+        assertNull("Wrong value for non-existing attribute", handler
+                .getAttributeValue(node, NAME));
+        EasyMock.verify(node);
+    }
+
+    /**
+     * Tests setting the value of an attribute.
+     */
+    public void testSetAttributeValue()
+    {
+        ConfigurationNode node = new DefaultConfigurationNode();
+        ConfigurationNode attr = new DefaultConfigurationNode(NAME, "oldValue");
+        node.addAttribute(attr);
+        handler.setAttributeValue(node, NAME, VALUE);
+        List<ConfigurationNode> attrs = node.getAttributes(NAME);
+        assertEquals("Wrong size of attribute list", 1, attrs.size());
+        assertEquals("Wrong attribute value", VALUE, attrs.get(0).getValue());
+    }
+
+    /**
+     * Tests removing a child from a node.
+     */
+    public void testRemoveChild()
+    {
+        ConfigurationNode node = mockNode();
+        ConfigurationNode child = mockNode();
+        EasyMock.expect(node.removeChild(child)).andReturn(Boolean.TRUE);
+        EasyMock.replay(node, child);
+        handler.removeChild(node, child);
+        EasyMock.verify(node, child);
+    }
+
+    /**
+     * Tests removing an attribute from a node.
+     */
+    public void testRemoveAttribute()
+    {
+        ConfigurationNode node = mockNode();
+        EasyMock.expect(node.removeAttribute(NAME)).andReturn(Boolean.TRUE);
+        EasyMock.replay(node);
+        handler.removeAttribute(node, NAME);
+        EasyMock.verify(node);
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain