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/05/17 15:35:15 UTC

svn commit: r1744286 - in /sling/trunk/bundles/commons/repoinit: oak-jcr/ oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/ oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/ parser/src/main/java/org/apache/sling/repoinit/parser/operations/

Author: bdelacretaz
Date: Tue May 17 15:35:15 2016
New Revision: 1744286

URL: http://svn.apache.org/viewvc?rev=1744286&view=rev
Log:
SLING-5449 - Implement CreatePath in oak-jcr module

Added:
    sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
Modified:
    sling/trunk/bundles/commons/repoinit/oak-jcr/pom.xml
    sling/trunk/bundles/commons/repoinit/oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java
    sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java
    sling/trunk/bundles/commons/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java

Modified: sling/trunk/bundles/commons/repoinit/oak-jcr/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/repoinit/oak-jcr/pom.xml?rev=1744286&r1=1744285&r2=1744286&view=diff
==============================================================================
--- sling/trunk/bundles/commons/repoinit/oak-jcr/pom.xml (original)
+++ sling/trunk/bundles/commons/repoinit/oak-jcr/pom.xml Tue May 17 15:35:15 2016
@@ -132,5 +132,11 @@
       <version>1.0.1-SNAPSHOT</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.testing</artifactId>
+      <version>2.0.18</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>

Modified: sling/trunk/bundles/commons/repoinit/oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/repoinit/oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java?rev=1744286&r1=1744285&r2=1744286&view=diff
==============================================================================
--- sling/trunk/bundles/commons/repoinit/oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java (original)
+++ sling/trunk/bundles/commons/repoinit/oak-jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java Tue May 17 15:35:15 2016
@@ -22,6 +22,7 @@ import static org.apache.sling.repoinit.
 
 import java.util.List;
 
+import javax.jcr.Node;
 import javax.jcr.Session;
 
 import org.apache.sling.repoinit.jcr.impl.AclUtil;
@@ -31,6 +32,7 @@ import org.apache.sling.repoinit.parser.
 import org.apache.sling.repoinit.parser.operations.CreateServiceUser;
 import org.apache.sling.repoinit.parser.operations.DeleteServiceUser;
 import org.apache.sling.repoinit.parser.operations.OperationVisitor;
+import org.apache.sling.repoinit.parser.operations.PathSegmentDefinition;
 import org.apache.sling.repoinit.parser.operations.SetAclPaths;
 import org.apache.sling.repoinit.parser.operations.SetAclPrincipals;
 import org.slf4j.Logger;
@@ -117,6 +119,25 @@ public class JcrRepoInitOpVisitor implem
 
     @Override
     public void visitCreatePath(CreatePath cp) {
-        throw new UnsupportedOperationException("visitCreatePath is not implemented yet");
+        String parentPath = "";
+            for(PathSegmentDefinition psd : cp.getDefinitions()) {
+                final String fullPath = parentPath + "/" + psd.getSegment();
+                try {
+                    if(session.itemExists(fullPath)) {
+                        // TODO warn if primary type is not correct 
+                    } else {
+                        final Node n = parentPath.equals("") ? session.getRootNode() : session.getNode(parentPath);
+                        n.addNode(psd.getSegment(), psd.getPrimaryType());
+                    }
+                } catch(Exception e) {
+                    throw new RuntimeException("CreatePath execution failed at " + psd + ": " + e, e);
+                }
+                parentPath += "/" + psd.getSegment();
+            }
+        try {
+            session.save();
+        } catch(Exception e) {
+            throw new RuntimeException("Session.save failed: "+ e, e);
+        }
     }
 }

Added: sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java?rev=1744286&view=auto
==============================================================================
--- sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java (added)
+++ sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java Tue May 17 15:35:15 2016
@@ -0,0 +1,77 @@
+/*
+ * 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.sling.repoinit.jcr;
+
+import java.io.IOException;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.sling.commons.testing.jcr.RepositoryUtil;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+/** Test the creation of paths with specific node types */
+public class CreatePathsTest {
+    
+    @Rule
+    public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);
+    
+    private TestUtil U;
+    
+    @Before
+    public void setup() throws RepositoryException, IOException {
+        U = new TestUtil(context);
+        RepositoryUtil.registerSlingNodeTypes(U.adminSession);
+    }
+
+    @Test
+    public void createSimplePath() throws Exception {
+        final String path = "/one/two/three";
+        U.parseAndExecute("create path " + path);
+        U.assertNodeExists(path);
+    }
+    
+    @Test
+    public void createPathWithTypes() throws Exception {
+        final String path = "/four/five(sling:Folder)/six(nt:folder)";
+        U.parseAndExecute("create path " + path);
+        U.assertNodeExists("/four", "nt:unstructured");
+        U.assertNodeExists("/four/five", "sling:Folder");
+        U.assertNodeExists("/four/five/six", "nt:folder");
+    }
+    
+    @Test
+    public void createPathWithSpecificDefaultType() throws Exception {
+        final String path = "/seven/eight(nt:unstructured)/nine";
+        U.parseAndExecute("create path (sling:Folder) " + path);
+        U.assertNodeExists("/seven", "sling:Folder");
+        U.assertNodeExists("/seven/eight", "nt:unstructured");
+        U.assertNodeExists("/seven/eight/nine", "sling:Folder");
+    }
+    
+    @Test
+    public void createPathWithJcrDefaultType() throws Exception {
+        final String path = "/ten/eleven(sling:Folder)/twelve";
+        U.parseAndExecute("create path " + path);
+        U.assertNodeExists("/ten", "nt:unstructured");
+        U.assertNodeExists("/ten/eleven", "sling:Folder");
+        U.assertNodeExists("/ten/eleven/twelve", "sling:Folder");
+    }
+}

Modified: sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java?rev=1744286&r1=1744285&r2=1744286&view=diff
==============================================================================
--- sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java (original)
+++ sling/trunk/bundles/commons/repoinit/oak-jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java Tue May 17 15:35:15 2016
@@ -18,12 +18,14 @@ package org.apache.sling.repoinit.jcr;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
 
 import java.io.Reader;
 import java.io.StringReader;
 import java.util.List;
 import java.util.UUID;
 
+import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
@@ -40,6 +42,7 @@ import org.apache.sling.testing.mock.sli
 /** Test utilities */
 class TestUtil {
     
+    public static final String JCR_PRIMARY_TYPE = "jcr:primaryType";
     final Session adminSession;
     final String id;
     final String username;
@@ -70,6 +73,26 @@ class TestUtil {
         }
     }
     
+    void assertNodeExists(String path) throws RepositoryException {
+        assertNodeExists(path, null);
+    }
+    
+    void assertNodeExists(String path, String primaryType) throws RepositoryException {
+        if(!adminSession.nodeExists(path)) {
+            fail("Node does not exist:" + path);
+        }
+        if(primaryType != null) {
+            final Node n = adminSession.getNode(path);
+            if(!n.hasProperty(JCR_PRIMARY_TYPE)) {
+                fail("No " + JCR_PRIMARY_TYPE + " property at " + path);
+            }
+            final String actual = n.getProperty(JCR_PRIMARY_TYPE).getString();
+            if(!primaryType.equals(actual)) {
+                fail("Primary type mismatch for " + path + ", expected " + primaryType + " but got " + actual);
+            }
+        }
+    }
+    
     void parseAndExecute(String input) throws RepositoryException, RepoInitParsingException {
         final JcrRepoInitOpVisitor v = new JcrRepoInitOpVisitor(adminSession);
         for(Operation o : parse(input)) {

Modified: sling/trunk/bundles/commons/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java?rev=1744286&r1=1744285&r2=1744286&view=diff
==============================================================================
--- sling/trunk/bundles/commons/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java (original)
+++ sling/trunk/bundles/commons/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java Tue May 17 15:35:15 2016
@@ -18,7 +18,7 @@
 package org.apache.sling.repoinit.parser.operations;
 
 import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.Collections;
 import java.util.List;
 
 public class CreatePath extends Operation {
@@ -62,7 +62,7 @@ public class CreatePath extends Operatio
         }
     }
     
-    public Iterator<PathSegmentDefinition> getDefinitions() {
-        return pathDef.iterator();
+    public List<PathSegmentDefinition> getDefinitions() {
+        return Collections.unmodifiableList(pathDef);
     }
 }