Author: rombert
Date: Tue Sep 29 21:57:57 2015
New Revision: 1705933

URL: http://svn.apache.org/viewvc?rev=1705933&view=rev
Log:
SLING-4988 - Not all folders are displayed in Project Explorer 

Simplify assertions in JcrContentContentProviderTest

Modified:
    
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/JcrContentContentProviderTest.java

Modified: 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/JcrContentContentProviderTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/JcrContentContentProviderTest.java?rev=1705933&r1=1705932&r2=1705933&view=diff
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/JcrContentContentProviderTest.java
 (original)
+++ 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/JcrContentContentProviderTest.java
 Tue Sep 29 21:57:57 2015
@@ -16,10 +16,11 @@
  */
 package org.apache.sling.ide.test.impl;
 
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.sling.ide.eclipse.ui.nav.JcrContentContentProvider;
 import org.apache.sling.ide.eclipse.ui.nav.model.JcrNode;
@@ -27,10 +28,10 @@ import org.apache.sling.ide.eclipse.ui.n
 import org.apache.sling.ide.test.impl.helpers.DisableDebugStatusHandlers;
 import org.apache.sling.ide.test.impl.helpers.ProjectAdapter;
 import org.apache.sling.ide.test.impl.helpers.TemporaryProject;
+import org.apache.sling.ide.util.PathUtil;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.Path;
-import org.hamcrest.CoreMatchers;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -58,36 +59,13 @@ public class JcrContentContentProviderTe
         InputStream contentXml = 
getClass().getResourceAsStream("content-nested-structure.xml");
         
project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/test-root/en.xml"),
 contentXml);
 
-        // instantiate the content provider
-        JcrContentContentProvider contentProvider = new 
JcrContentContentProvider();
-
         // directly create the root node
         SyncDir syncDirNode = new SyncDir((IFolder) 
contentProject.findMember("jcr_root"));
 
-        // test children of '/'
-        Object[] children = contentProvider.getChildren(syncDirNode);
-        assertChildrenHavePaths(children, "/content");
-
-        // test children of '/content'
-        JcrNode contentNode = (JcrNode) children[0];
-        Object[] children2 = contentProvider.getChildren(contentNode);
-        assertChildrenHavePaths(children2, "/content/test-root");
-
-        // test children of '/content/test-root'
-        JcrNode testRootNode = (JcrNode) children2[0];
-        Object[] children3 = contentProvider.getChildren(testRootNode);
-        assertChildrenHavePaths(children3, "/content/test-root/en");
-
-        // test children of '/content/test-root/en'
-        JcrNode enNode = (JcrNode) children3[0];
-        Object[] children4 = contentProvider.getChildren(enNode);
-        assertChildrenHavePaths(children4, "/content/test-root/en/message", 
"/content/test-root/en/error",
-                "/content/test-root/en/warning");
-
-        // test children of '/content/test-root/en/message'
-        JcrNode messageNode = (JcrNode) children4[0];
-        Object[] children5 = contentProvider.getChildren(messageNode);
-        assertChildrenHavePaths(children5); // no children
+        // assertions
+        assertIsNavigableAndHasNoChildren(syncDirNode, 
"/content/test-root/en/message");
+        assertIsNavigableAndHasNoChildren(syncDirNode, 
"/content/test-root/en/error");
+        assertIsNavigableAndHasNoChildren(syncDirNode, 
"/content/test-root/en/warning");
     }
 
     @Test
@@ -109,26 +87,11 @@ public class JcrContentContentProviderTe
         
project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/_sling_stuff/.content.xml"),
 getClass()
                 .getResourceAsStream("nt-unstructured-nodetype.xml"));
 
-        // instantiate the content provider
-        JcrContentContentProvider contentProvider = new 
JcrContentContentProvider();
-
         // directly create the root node
         SyncDir syncDirNode = new SyncDir((IFolder) 
contentProject.findMember("jcr_root"));
 
-        // test children of '/'
-        Object[] children = contentProvider.getChildren(syncDirNode);
-        assertChildrenHavePaths(children, "/content");
-
-        // test children of '/content'
-        JcrNode contentNode = (JcrNode) children[0];
-        Object[] children2 = contentProvider.getChildren(contentNode);
-        assertChildrenHavePaths(children2, "/content/sling:stuff");
-
-        // test children of '/content/sling:stuff
-        JcrNode slingStuffNode = (JcrNode) children2[0];
-        Object[] children3 = contentProvider.getChildren(slingStuffNode);
-        assertChildrenHavePaths(children3); // no children
-
+        // assertion
+        assertIsNavigableAndHasNoChildren(syncDirNode, "/content/sling:stuff");
     }
 
     @Test
@@ -151,20 +114,12 @@ public class JcrContentContentProviderTe
         
project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/child2.txt"),
 new ByteArrayInputStream(
                 "hello, world".getBytes()));
 
-        // instantiate the content provider
-        JcrContentContentProvider contentProvider = new 
JcrContentContentProvider();
-
         // directly create the root node
         SyncDir syncDirNode = new SyncDir((IFolder) 
contentProject.findMember("jcr_root"));
-
-        // test children of '/'
-        Object[] children = contentProvider.getChildren(syncDirNode);
-        assertChildrenHavePaths(children, "/content");
-
-        // test children of '/content'
-        JcrNode contentNode = (JcrNode) children[0];
-        Object[] children2 = contentProvider.getChildren(contentNode);
-        assertChildrenHavePaths(children2, "/content/child1.txt", 
"/content/child2.txt");
+        
+        // assertions
+        assertIsNavigableAndHasNoChildren(syncDirNode, "/content/child1.txt");
+        assertIsNavigableAndHasNoChildren(syncDirNode, "/content/child2.txt");
     }
     
     @Test
@@ -185,37 +140,55 @@ public class JcrContentContentProviderTe
         
         
project.ensureDirectoryExists(Path.fromPortableString("jcr_root/content/_jcr_content/first-folder/second-folder"));
 
-        // instantiate the content provider
-        JcrContentContentProvider contentProvider = new 
JcrContentContentProvider();
-
         // directly create the root node
         SyncDir syncDirNode = new SyncDir((IFolder) 
contentProject.findMember("jcr_root"));
-
-        // test children of '/'
-        Object[] children = contentProvider.getChildren(syncDirNode);
-        assertChildrenHavePaths(children, "/content");
-
-        // test children of '/content'
-        JcrNode contentNode = (JcrNode) children[0];
-        Object[] children2 = contentProvider.getChildren(contentNode);
-        assertChildrenHavePaths(children2, "/content/jcr:content");
-
-        // test children of '/content/first-child'
-        JcrNode folderNode = (JcrNode) children2[0];
-        Object[] children3 = contentProvider.getChildren(folderNode);
-        assertChildrenHavePaths(children3, 
"/content/jcr:content/first-folder"); 
+        
+        // assertion
+        assertIsNavigableAndHasNoChildren(syncDirNode, 
"/content/jcr:content/first-folder/second-folder");
     }
-
-    private void assertChildrenHavePaths(Object[] children, String... paths) {
-        assertThat("Unexpected number of children found", children.length, 
CoreMatchers.equalTo(paths.length));
-        for (int i = 0; i < children.length; i++) {
-
-            Object child = children[i];
-            assertThat("Unexpected type of child", child, 
CoreMatchers.instanceOf(JcrNode.class));
-
-            JcrNode node = (JcrNode) child;
-            assertThat("Unexpected path for child at index " + i, 
node.getJcrPath(), CoreMatchers.equalTo(paths[i]));
+    
+    /**
+     * Asserts that the specified <tt>nodePath</tt> is reachable from the 
<tt>startNode</tt>
+     * 
+     * <p>It further asserts that there are no children beyond the 
<tt>nodePath</tt>, i.e. it
+     * is and endpoint</p>
+     * 
+     * @param startNode the node to start from
+     * @param nodePath the path that is reachable and an endpoint
+     */
+    private void assertIsNavigableAndHasNoChildren(SyncDir startNode, String 
nodePath) {
+        
+        JcrContentContentProvider contentProvider = new 
JcrContentContentProvider();
+        
+        if ( nodePath.charAt(0) == '/') {
+            nodePath = nodePath.substring(1);
+        }
+        
+        String[] pathElements = nodePath.split("/");
+        JcrNode current = startNode;
+        
+        segments: for ( int i = 0 ; i < pathElements.length ; i++ ) {
+            
+            String expectedChildName = pathElements[i];
+            Object[] children = contentProvider.getChildren(current);
+            
+            for ( Object child : children ) {
+                JcrNode childNode = (JcrNode) child;
+                // childNode.getName() does not seem to be usable here, so 
relying on the path
+                String childName = PathUtil.getName(childNode.getJcrPath());
+                if ( childName.equals(expectedChildName)) {
+                    current = childNode;
+                    continue segments;
+                }
+            }
+            
+            fail("Unable to navigate to '" + nodePath + "'. "
+                    + " No child named '"+ expectedChildName +"'found for node 
at " + current.getJcrPath() + ", children: " + Arrays.toString(children));
+        }
+        
+        Object[] children = contentProvider.getChildren(current);
+        if ( children.length != 0 ) {
+            fail("Unexpected children for node at '" + current.getJcrPath() + 
"' : " + Arrays.toString(children));
         }
     }
-
 }


Reply via email to