commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1636037 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java
Date Sat, 01 Nov 2014 20:49:48 GMT
Author: oheger
Date: Sat Nov  1 20:49:47 2014
New Revision: 1636037

URL: http://svn.apache.org/r1636037
Log:
Added a default matcher implementation for node names.

The new implementation performs an exact match on a node name. It is defined as
a constant in an enumeration class because a single instance can be shared
between arbitrary components.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java?rev=1636037&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java
(added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeNameMatchers.java
Sat Nov  1 20:49:47 2014
@@ -0,0 +1,52 @@
+/*
+ * 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.tree;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * <p>
+ * An enumeration class with several pre-defined {@link NodeMatcher}
+ * implementations based on node names.
+ * </p>
+ * <p>
+ * Filtering nodes by their name is a typical use case. Therefore, some default
+ * implementations for typical filter algorithms are already provided. They are
+ * available as constants of this class. Because the algorithms are state-less
+ * these instances can be shared and accessed concurrently.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public enum NodeNameMatchers implements NodeMatcher<String>
+{
+    /**
+     * A matcher for exact node name matches. This matcher returns <b>true</b>
+     * if and only if the name of the passed in node equals exactly the given
+     * criterion string.
+     */
+    EQUALS
+    {
+        @Override
+        public <T> boolean matches(T node, NodeHandler<T> handler,
+                String criterion)
+        {
+            return StringUtils.equals(criterion, handler.nodeName(node));
+        }
+    }
+}

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java?rev=1636037&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java
(added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestNodeNameMatchers.java
Sat Nov  1 20:49:47 2014
@@ -0,0 +1,98 @@
+/*
+ * 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.tree;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Locale;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code NodeNameMatchers}.
+ *
+ * @version $Id$
+ */
+public class TestNodeNameMatchers
+{
+    /** Constant for a test node name. */
+    private static final String NODE_NAME = "TestNodeName";
+
+    /** A node handler. */
+    private NodeHandler<ImmutableNode> handler;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        InMemoryNodeModel model = new InMemoryNodeModel();
+        handler = model.getNodeHandler();
+    }
+
+    /**
+     * Creates a node with the given name.
+     *
+     * @param name the name
+     * @return the newly created node
+     */
+    private static ImmutableNode createNode(String name)
+    {
+        return new ImmutableNode.Builder().name(name).create();
+    }
+
+    /**
+     * Tests the equals matcher if the expected result is true.
+     */
+    @Test
+    public void testEqualsMatch()
+    {
+        ImmutableNode node = createNode(NODE_NAME);
+        assertTrue("No match",
+                NodeNameMatchers.EQUALS.matches(node, handler, NODE_NAME));
+    }
+
+    /**
+     * Tests the equals matcher for a non matching name.
+     */
+    @Test
+    public void testEqualsNoMatch()
+    {
+        ImmutableNode node = createNode(NODE_NAME);
+        assertFalse(
+                "Match (1)",
+                NodeNameMatchers.EQUALS.matches(node, handler, NODE_NAME
+                        + "_other"));
+        assertFalse(
+                "Match (2)",
+                NodeNameMatchers.EQUALS.matches(node, handler,
+                        NODE_NAME.toLowerCase(Locale.ENGLISH)));
+    }
+
+    /**
+     * Tests whether the equals matcher can handle a null criterion.
+     */
+    @Test
+    public void testEqualsNullCriterion()
+    {
+        ImmutableNode node = createNode(NODE_NAME);
+        assertFalse("Match (1)",
+                NodeNameMatchers.EQUALS.matches(node, handler, null));
+        assertFalse("Match (2)", NodeNameMatchers.EQUALS.matches(
+                createNode(null), handler, NODE_NAME));
+    }
+}



Mime
View raw message