incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r618270 - in /incubator/sling/trunk/jcr/resource/src: main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java
Date Mon, 04 Feb 2008 13:17:30 GMT
Author: fmeschbe
Date: Mon Feb  4 05:17:27 2008
New Revision: 618270

URL: http://svn.apache.org/viewvc?rev=618270&view=rev
Log:
* Prevent NullPointerException in getParent if path normalization returns null
* Do not return leading slash for getName
* Add unit tests for getParent and getName

Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
    incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=618270&r1=618269&r2=618270&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Mon Feb  4 05:17:27 2008
@@ -184,7 +184,7 @@
         path = normalize(path);
 
         // if normalized to root, there is no parent
-        if ("/".equals(path)) {
+        if (path == null || "/".equals(path)) {
             return null;
         }
 
@@ -193,6 +193,9 @@
         if (lastSlash < 0) {
             // no slash in the path
             return null;
+        } else if (lastSlash == 0) {
+            // parent is root
+            return "/";
         }
 
         return path.substring(0, lastSlash);
@@ -223,8 +226,7 @@
         }
 
         // find the last slash
-        int lastSlash = path.lastIndexOf('/');
-        return (lastSlash >= 0) ? path.substring(lastSlash) : path;
+        return path.substring(path.lastIndexOf('/')+1);
     }
 
 }

Modified: incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java?rev=618270&r1=618269&r2=618270&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrResourceUtilTest.java
Mon Feb  4 05:17:27 2008
@@ -117,4 +117,34 @@
         assertEquals("az/bz/cz", JcrResourceUtil.normalize("az/bz/cz"));
         assertEquals("", JcrResourceUtil.normalize(""));
     }
+    
+    public void testGetParent() {
+        assertNull(JcrResourceUtil.getParent("/"));
+        assertNull(JcrResourceUtil.getParent("/.."));
+        
+        assertEquals("/", JcrResourceUtil.getParent("/b"));
+        assertEquals("b/c", JcrResourceUtil.getParent("b/c/d"));
+        assertEquals("/b/c", JcrResourceUtil.getParent("/b/c/d"));
+
+        assertNull(JcrResourceUtil.getParent("b"));
+        assertNull(JcrResourceUtil.getParent("/b/.."));
+    }
+    
+    public void testGetName() {
+        assertEquals("", JcrResourceUtil.getName("/"));
+        assertEquals("", JcrResourceUtil.getName("/a/.."));
+        
+        assertEquals("c", JcrResourceUtil.getName("c"));
+        assertEquals("c", JcrResourceUtil.getName("/c"));
+        
+        assertEquals("c", JcrResourceUtil.getName("b/c"));
+        assertEquals("c", JcrResourceUtil.getName("/b/c"));
+        
+        assertEquals("c", JcrResourceUtil.getName("b/c/"));
+        assertEquals("c", JcrResourceUtil.getName("/b/c/"));
+
+        assertEquals("b", JcrResourceUtil.getName("b/c/.."));
+        assertEquals("b", JcrResourceUtil.getName("/b/c/.."));
+        assertEquals("", JcrResourceUtil.getName("/b/c/../.."));
+    }
 }



Mime
View raw message