This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.jcr.repoinit-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git
commit 7af1acb9c89e2197abb7a146675663666945fc0c Author: Bertrand Delacretaz <[email protected]> AuthorDate: Tue May 17 15:35:15 2016 +0000 SLING-5449 - Implement CreatePath in oak-jcr module git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/repoinit/oak-jcr@1744286 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 6 ++ .../sling/repoinit/jcr/JcrRepoInitOpVisitor.java | 23 ++++++- .../apache/sling/repoinit/jcr/CreatePathsTest.java | 77 ++++++++++++++++++++++ .../org/apache/sling/repoinit/jcr/TestUtil.java | 23 +++++++ 4 files changed, 128 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e093443..90ce530 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java b/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java index 1b991af..e3680bb 100644 --- a/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java +++ b/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java @@ -22,6 +22,7 @@ import static org.apache.sling.repoinit.parser.operations.AclLine.PROP_PRIVILEGE 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.operations.CreatePath; 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 implements OperationVisitor { @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); + } } } diff --git a/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java b/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java new file mode 100644 index 0000000..faf5cf2 --- /dev/null +++ b/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java @@ -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"); + } +} diff --git a/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java b/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java index e1d2383..7d72df8 100644 --- a/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java +++ b/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java @@ -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.sling.junit.SlingContext; /** 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)) { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
