incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1069985 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/util/PathImpl.java test/java/org/apache/bval/jsr303/util/PathImplTest.java
Date Fri, 11 Feb 2011 23:34:43 GMT
Author: mbenson
Date: Fri Feb 11 23:34:43 2011
New Revision: 1069985

URL: http://svn.apache.org/viewvc?rev=1069985&view=rev
Log:
handle removeLeafNode against root paths to improve consistency that trimming a path fully
will reduce it back to our single-node representation of a root path

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java?rev=1069985&r1=1069984&r2=1069985&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathImpl.java
Fri Feb 11 23:34:43 2011
@@ -30,6 +30,7 @@ import java.util.regex.Pattern;
  * Description: object holding the property path as a list of nodes.
  * (Implementation based on reference implementation)
  * <br/>
+ * This class is not synchronized.
  */
 public class PathImpl implements Path, Serializable {
 
@@ -143,11 +144,16 @@ public class PathImpl implements Path, S
      * @throws IllegalStateException if no nodes are found
      */
     public Node removeLeafNode() {
-        //TODO what if isRootNode()?
-        if (nodeList.size() == 0) {
+        if (isRootPath() || nodeList.size() == 0) {
             throw new IllegalStateException("No nodes in path!");
         }
-        return nodeList.remove(nodeList.size() - 1);
+        try {
+            return nodeList.remove(nodeList.size() - 1);
+        } finally {
+            if (nodeList.isEmpty()) {
+                nodeList.add(new NodeImpl((String) null));
+            }
+        }
     }
 
     /**

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java?rev=1069985&r1=1069984&r2=1069985&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/PathImplTest.java
Fri Feb 11 23:34:43 2011
@@ -146,6 +146,27 @@ public class PathImplTest extends TestCa
         assertEquals("[2].firstName", path.toString());
     }
 
+    public void testAddRemoveNodes() {
+        PathImpl path = PathImpl.createPathFromString("");
+        assertTrue(path.isRootPath());
+        assertEquals(1, countNodes(path));
+        path.addNode(new NodeImpl("foo"));
+        assertFalse(path.isRootPath());
+        assertEquals(1, countNodes(path));
+        path.removeLeafNode();
+        assertTrue(path.isRootPath());
+        assertEquals(1, countNodes(path));
+    }
+
+    private int countNodes(Path path) {
+        int result = 0;
+        for (Iterator<Path.Node> iter = path.iterator(); iter.hasNext();) {
+            iter.next();
+            result++;
+        }
+        return result;
+    }
+
     public static Test suite() {
         return new TestSuite(PathImplTest.class);
     }



Mime
View raw message