commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r829213 - in /commons/sandbox/validator2/branches/alternative/validation-framework: pom.xml src/main/java/org/apache/commons/validation/framework/PathImpl.java src/test/java/org/apache/commons/validation/framework/PathImplTest.java
Date Fri, 23 Oct 2009 20:35:07 GMT
Author: niallp
Date: Fri Oct 23 20:35:06 2009
New Revision: 829213

URL: http://svn.apache.org/viewvc?rev=829213&view=rev
Log:
Implement parsing of String to populate Path nodes

Added:
    commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
  (with props)
Modified:
    commons/sandbox/validator2/branches/alternative/validation-framework/pom.xml
    commons/sandbox/validator2/branches/alternative/validation-framework/src/main/java/org/apache/commons/validation/framework/PathImpl.java

Modified: commons/sandbox/validator2/branches/alternative/validation-framework/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-framework/pom.xml?rev=829213&r1=829212&r2=829213&view=diff
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-framework/pom.xml (original)
+++ commons/sandbox/validator2/branches/alternative/validation-framework/pom.xml Fri Oct 23
20:35:06 2009
@@ -46,7 +46,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>3.8.2</version>
+            <version>4.7</version>
             <scope>test</scope>
         </dependency>
     </dependencies>

Modified: commons/sandbox/validator2/branches/alternative/validation-framework/src/main/java/org/apache/commons/validation/framework/PathImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-framework/src/main/java/org/apache/commons/validation/framework/PathImpl.java?rev=829213&r1=829212&r2=829213&view=diff
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-framework/src/main/java/org/apache/commons/validation/framework/PathImpl.java
(original)
+++ commons/sandbox/validator2/branches/alternative/validation-framework/src/main/java/org/apache/commons/validation/framework/PathImpl.java
Fri Oct 23 20:35:06 2009
@@ -16,9 +16,12 @@
  */
 package org.apache.commons.validation.framework;
 
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 
 import javax.validation.Path;
+import javax.validation.ValidationException;
 
 /**
  * {@link Path} implementation.
@@ -28,13 +31,85 @@
  */
 public class PathImpl implements Path {
 
+    private static final char PROPERTY_SEPARATOR = '.';
+    private static final char INDEX_START = '[';
+    private static final char INDEX_END = ']';
+    private final List<Node> nodes;
+
+    /**
+     * Construct a new path instance.
+     *
+     * @param path The string path to be parsed
+     */
+    public PathImpl(String path) {
+        nodes = parse(path);
+    }
+
     /**
      * Get an iterator of the nodes of this path.
      *
      * @return an iterator of the nodes of this path
      */
     public Iterator<Node> iterator() {
-        return null;
+        return nodes.iterator();
+    }
+
+    /**
+     * Parse a string path into a set of nodes.
+     */
+    private List<Node> parse(String path) {
+        if (path == null) {
+            throw new ValidationException("path is missing");
+        }
+        List<Node> nodes = new ArrayList<Node>();
+        int start = 0;
+        String name = "";
+        String key = null;
+        Integer index = null;
+        for (int i = 0; i < path.length(); i++) {
+            char c = path.charAt(i);
+            if (c == INDEX_START) {
+                name = path.substring(start, i);
+                start = i + 1;
+                for (; i < path.length(); i++) {
+                    c = path.charAt(i);
+                    if (c == INDEX_END) {
+                        key = path.substring(start, i);
+                        if (key.length() > 0) {
+                            try {
+                                index = Integer.parseInt(key);
+                                key = null;
+                            } catch (NumberFormatException  e) {
+                                index = null;
+                            }
+                        }
+                        break;
+                    }
+                }
+                if (c != INDEX_END) {
+                    throw new ValidationException("Missing index/key end: " + path);
+                }
+                nodes.add(new NodeImpl(name, index, key));
+                name = "";
+                index = null;
+                key = null;
+                i++;
+                if (i < path.length() && path.charAt(i) == PROPERTY_SEPARATOR)
{
+                    i++;
+                }
+                start = i;
+            } else if (c == PROPERTY_SEPARATOR) {
+                name = path.substring(start, i);
+                nodes.add(new NodeImpl(name));
+                name = "";
+                start = i + 1;
+            }
+        }
+        if (start < path.length()) {
+            name = path.substring(start);
+            nodes.add(new NodeImpl(name));
+        }
+        return nodes;
     }
 
     /**
@@ -103,5 +178,45 @@
         public boolean isInIterable() {
             return (index != null || key != null);
         }
+
+        @Override
+        public boolean equals(Object obj) {
+            boolean equal = true;
+            if (obj instanceof Node) {
+                Node node = (Node)obj;
+                equal = equal && equal(this.name,  node.getName());
+                equal = equal && equal(this.key,   node.getKey());
+                equal = equal && equal(this.index, node.getIndex());
+            } else {
+                equal = false;
+            }
+            return equal;
+        }
+
+        @Override
+        public String toString() {
+            if (key == null && index == null) {
+                return name;
+            }
+            StringBuilder builder = new StringBuilder();
+            builder.append(name);
+            builder.append('[');
+            if (index != null) {
+                builder.append(index);
+            } else {
+                builder.append(key);
+            }
+            builder.append(']');
+            return builder.toString();
+        }
+
+        private boolean equal(Object obj1, Object obj2) {
+            if (obj1 == null || obj2 == null) {
+                return (obj1 == null && obj2 == null);
+            } else {
+                return obj1.equals(obj2);
+            }
+        }
+        
     }
 }

Added: commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java?rev=829213&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
Fri Oct 23 20:35:06 2009
@@ -0,0 +1,144 @@
+/*
+ * 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.validation.framework;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.validation.Path;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+
+/**
+ * PathImplTest.
+ *
+ * @version $Revision$ $Date$
+ * @since 1.0
+ */
+public class PathImplTest {
+
+    @Test
+    public void testFoo() {
+        String value = "foo";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo");
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    @Test
+    public void testFooIndexed() {
+        String value = "foo[3]";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo", 3);
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    @Test
+    public void testFooKey() {
+        String value = "foo[bar]";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo", "bar");
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    @Test
+    public void testFooBar() {
+        String value = "foo.bar";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo");
+        expected = addNode(expected, "bar");
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    @Test
+    public void testFooIndexedBar() {
+        String value = "foo[3].bar";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo", 3);
+        expected = addNode(expected, "bar");
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    @Test
+    public void testFooKeyBar() {
+        String value = "foo[doh].bar";
+        List<Path.Node> expected = null;
+        expected = addNode(expected, "foo", "doh");
+        expected = addNode(expected, "bar");
+        Path path = new PathImpl(value);
+        compareNodes(value, expected, path);
+    }
+
+    /**
+     * Check the results.
+     */
+    private void compareNodes(String value, List<Path.Node> expected, Path path) {
+        List<Path.Node> actual = new ArrayList<Path.Node>();
+        Iterator<Path.Node> iterator = path.iterator();
+        while (iterator.hasNext()) {
+            actual.add(iterator.next());
+        }
+        assertEquals("Size for '" + value + "'", expected.size(), actual.size());
+        for (int i = 0; i < expected.size(); i++) {
+            Path.Node expectedNode = expected.get(i);
+            Path.Node actualNode = actual.get(i);
+            assertTrue("Node[" + i + "]= expected=" + actualNode + ", actual=" + actualNode,
expectedNode.equals(actualNode));
+        }
+    }
+
+    /**
+     * Add a node to the list.
+     */
+    private static List<Path.Node> addNode(List<Path.Node> list, String name)
{
+        return addNode(list, name, null);
+    }
+
+    /**
+     * Add a node to the list.
+     */
+    private static List<Path.Node> addNode(List<Path.Node> list, String name,
int index) {
+        return addNode(list, name, index, null);
+    }
+
+    /**
+     * Add a node to the list.
+     */
+    private static List<Path.Node> addNode(List<Path.Node> list, String name,
String key) {
+        return addNode(list, name, null, key);
+    }
+
+    /**
+     * Add a node to the list.
+     */
+    private static List<Path.Node> addNode(List<Path.Node> list, String name,
Integer index, String key) {
+        Path.Node node = new PathImpl.NodeImpl(name, index, key);
+        if (list == null) {
+            list = new ArrayList<Path.Node>();
+        }
+        list.add(node);
+        return list;
+    }
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-framework/src/test/java/org/apache/commons/validation/framework/PathImplTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



Mime
View raw message