You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2016/12/07 14:31:06 UTC

svn commit: r1773084 - /sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java

Author: bdelacretaz
Date: Wed Dec  7 14:31:06 2016
New Revision: 1773084

URL: http://svn.apache.org/viewvc?rev=1773084&view=rev
Log:
SLING-6350 - more readable tests

Modified:
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java

Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java?rev=1773084&r1=1773083&r2=1773084&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java (original)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java Wed Dec  7 14:31:06 2016
@@ -27,73 +27,116 @@ import org.junit.Test;
 
 public class PathTest {
 
+    private void assertMatchOrNot(boolean expected, Path p, String ... pathOrPattern) {
+        for(String s : pathOrPattern) {
+            final boolean actual = p.matches(s);
+            if(actual != expected) {
+                fail("Expected " 
+                        + (expected ? "match" : "no match")
+                        + " between " + p.getPath() + " and " + s
+                        + " but got match=" + actual
+                );
+            }
+        }
+    }
+    
+    private void assertMatch(Path p, String ... pathOrPattern) {
+        assertMatchOrNot(true, p, pathOrPattern);
+    }
+    
+    private void assertNoMatch(Path p, String ... pathOrPattern) {
+        assertMatchOrNot(false, p, pathOrPattern);
+    }
+    
     @Test public void testRootMatching() {
-        final Path path = new Path("/");
-
-        assertTrue(path.matches("/"));
-        assertTrue(path.matches("/foo"));
-        assertTrue(path.matches("/foo/bar"));
+        final Path p = new Path("/");
+        
+        assertMatch(p, 
+                "/", 
+                "/foo", 
+                "/foo/bar"
+        );
     }
 
     @Test public void testPathMatching() {
-        final Path path = new Path("/content");
+        final Path p  = new Path("/content");
+        assertNoMatch(p, 
+                "/", 
+                "/foo", 
+                "/foo/bar", 
+                "/content1"
+        );
+        
+        assertMatch(p, 
+                "/content", 
+                "/content/a", 
+                "/content/a/b"
+        );
+    }
 
-        assertFalse(path.matches("/"));
-        assertFalse(path.matches("/foo"));
-        assertFalse(path.matches("/foo/bar"));
-
-        assertTrue(path.matches("/content"));
-        assertTrue(path.matches("/content/a"));
-        assertTrue(path.matches("/content/a/b"));
-
-        assertFalse(path.matches("/content1"));
-    }
-
-    @Test public void testPatternMatching() {
-        final Path path_1 = new Path("glob:/apps/**/*.html");
-        assertTrue(path_1.matches("/apps/project/a.html"));
-        assertTrue(path_1.matches("/apps/project/1/a.html"));
-        assertTrue(path_1.matches("/apps/project/1/2/a.html"));
-        assertTrue(path_1.matches("/apps/project/1/2/3/4/5/6/7/8/9/a.html"));
-        assertFalse(path_1.matches("/apps/a.html"));
-        assertFalse(path_1.matches("/apps/project/a.html/b"));
-
-        final Path path_2 = new Path("glob:/apps/*.html");
-        assertTrue(path_2.matches("/apps/a.html"));
-        assertFalse(path_2.matches("/apps/a/a.html"));
-
-        final Path path_3 = new Path("glob:/a/m-p/$structure/**/[cmp]/*.html");
-        assertTrue(path_3.matches("/a/m-p/$structure/1/2/3/[cmp]/test.html"));
-        assertTrue(path_3.matches("/a/m-p/$structure/1/2/3/4/5/6/[cmp]/test.html"));
-        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/test.html"));
-        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/te-[s]t$.html"));
-        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/.html"));
-        assertFalse(path_3.matches("/a/m-p/$structure/1/[cmp]/html"));
-        assertFalse(path_3.matches("/a/m-p/$structure/[cmp]/test.html"));
+    @Test public void testPatternMatchingA() {
+        final Path p = new Path("glob:/apps/**/*.html");
+        
+        assertMatch(p, 
+                "/apps/project/a.html", 
+                "/apps/project/1/a.html",
+                "/apps/project/1/2/a.html",
+                "/apps/project/1/2/3/4/5/6/7/8/9/a.html"
+        );
+        
+        assertNoMatch(p,
+                "/apps/a.html",
+                "/apps/project/a.html/b"
+        );
+    }
+
+    @Test public void testPatternMatchingB() {
+        final Path p = new Path("glob:/apps/*.html");
+        assertMatch(p, "/apps/a.html");
+        assertNoMatch(p, "/apps/a/a.html");
+    }
+
+    @Test public void testPatternMatchingC() {
+        final Path p = new Path("glob:/a/m-p/$structure/**/[cmp]/*.html");
+        
+        assertMatch(p, 
+                "/a/m-p/$structure/1/2/3/[cmp]/test.html",
+                "/a/m-p/$structure/1/2/3/4/5/6/[cmp]/test.html",
+                "/a/m-p/$structure/1/[cmp]/test.html",
+                "/a/m-p/$structure/1/[cmp]/te-[s]t$.html",
+                "/a/m-p/$structure/1/[cmp]/.html");
+        
+        assertNoMatch(p,
+                "/a/m-p/$structure/1/[cmp]/html",
+                "/a/m-p/$structure/[cmp]/test.html"
+        );
     }
 
     @Test public void testPatternRootMatching() {
-        final Path path = new Path("/");
-        assertTrue(path.matches("glob:/apps/myproject/components/**/*.html"));
-        assertTrue(path.matches("glob:/apps/**/*.html"));
+        final Path p = new Path("/");
+        assertMatch(p,
+                "glob:/apps/myproject/components/**/*.html",
+                "glob:/apps/**/*.html"
+        );
     }
 
     @Test public void testMatchesWithGlobPattern() {
-        final Path path = new Path("/apps/myproject");
-        assertTrue(path.matches("glob:/apps/myproject/components/**/*.html"));
-        assertTrue(path.matches("glob:/apps/**/components/**/*.html"));
-        assertFalse(path.matches("glob:/*/foo"));
-        assertTrue(path.matches("glob:/*/myproject"));
-        assertTrue(path.matches("glob:/*/*project"));
-        assertTrue(path.matches("glob:/*/*project/**.html"));
-        assertTrue(path.matches("glob:/**/myproject"));
-        assertTrue(path.matches("glob:/**/myproject/*.jsp"));
-        
-        // TODO this should be assertTrue?? as per SLING-6350
-        assertFalse(path.matches("glob:/**"));
+        final Path p = new Path("/apps/myproject");
+        assertMatch(p,
+                "glob:/apps/myproject/components/**/*.html",
+                "glob:/apps/**/components/**/*.html",
+                "glob:/*/myproject",
+                "glob:/*/*project",
+                "glob:/*/*project/**.html",
+                "glob:/**/myproject",
+                "glob:/**/myproject/*.jsp"
+        );
         
-        // TODO this should be assertTrue?? as per SLING-6350
-        assertFalse(path.matches("glob:/**/*.jsp"));
+        assertNoMatch(p,
+                "glob:/*/foo",
+                "glob:/**",         // TODO this one should match?? as per SLING-6350
+                "glob:/**/*.jsp"    // TODO this one should match?? as per SLING-6350
+        );
     }
 
     @Test public void testIllegalArgumentException() {
@@ -121,15 +164,19 @@ public class PathTest {
     }
 
     @Test public void testPathMatchingTrailingSlash() {
-        final Path path = new Path("/libs/");
-        assertTrue(path.matches("/libs"));
-        assertTrue(path.matches("/libs/foo"));
-        assertFalse(path.matches("/lib"));
+        final Path p = new Path("/libs/");
+        
+        assertMatch(p,
+                "/libs",
+                "/libs/foo"
+        );
+        
+        assertNoMatch(p,"/lib");
     }
     @Test public void testGlobPatternsMatch() {
-        final Path glob = new Path("glob:/*/foo");
-        assertTrue(glob.matches("glob:/*/foo"));
-        assertFalse(glob.matches("glob:/*/**/foo"));
+        final Path p = new Path("glob:/*/foo");
+        assertMatch(p, "glob:/*/foo");
+        assertNoMatch(p, "glob:/*/**/foo");
     }
     
     @Test public void testIsPatternWithGlob() {
@@ -159,4 +206,4 @@ public class PathTest {
         final Path path = new Path("/" + UUID.randomUUID());
         assertTrue(path.toString().contains(path.getPath()));
     }
-}
+}
\ No newline at end of file