From 009d7e22334df2a3723467bbfc37dc5529f65ad8 Mon Sep 17 00:00:00 2001 From: Joel Richard Date: Thu, 30 Jul 2015 11:55:47 +0200 Subject: [PATCH] JCR-3870 - Export SessionImpl#getItemOrNull in JackrabbitSession * Add getItemOrNull, getNodeOrNull and getPropertyOrNull to JackrabbitSession interface * Implement new methods in SessionImpl and DummySession * Test cases --- .../apache/jackrabbit/api/JackrabbitSession.java | 37 ++++++++ .../org/apache/jackrabbit/core/SessionImpl.java | 30 ++++++ .../jackrabbit/core/integration/GetOrNullTest.java | 101 +++++++++++++++++++++ .../core/security/user/UserImporterTest.java | 12 +++ 4 files changed, 180 insertions(+) create mode 100644 jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/GetOrNullTest.java diff --git a/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/JackrabbitSession.java b/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/JackrabbitSession.java index 2b2e878..9f34c76 100644 --- a/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/JackrabbitSession.java +++ b/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/JackrabbitSession.java @@ -20,6 +20,9 @@ import org.apache.jackrabbit.api.security.user.UserManager; import org.apache.jackrabbit.api.security.principal.PrincipalManager; import javax.annotation.Nonnull; +import javax.jcr.Item; +import javax.jcr.Node; +import javax.jcr.Property; import javax.jcr.Session; import javax.jcr.AccessDeniedException; import javax.jcr.RepositoryException; @@ -214,4 +217,38 @@ public interface JackrabbitSession extends Session { */ UserManager getUserManager() throws AccessDeniedException, UnsupportedRepositoryOperationException, RepositoryException; + /** + * Returns the node at the specified absolute path in the workspace. If no + * such node exists, then it returns the property at the specified path. + * If no such property exists, then it return {@code null}. + * + * @param absPath An absolute path. + * @return the specified {@code Item} or {@code null}. + * @throws RepositoryException if another error occurs. + * @since 2.10.2 + */ + Item getItemOrNull(final String absPath) throws RepositoryException; + + /** + * Returns the property at the specified absolute path in the workspace or + * {@code null} if no such node exists. + * + * @param absPath An absolute path. + * @return the specified {@code Property} or {@code null}. + * @throws RepositoryException if another error occurs. + * @since 2.10.2 + */ + Property getPropertyOrNull(final String absPath) throws RepositoryException; + + /** + * Returns the node at the specified absolute path in the workspace or + * {@code null} if no such node exists. + * + * @param absPath An absolute path. + * @return the specified {@code Node} or {@code null}. + * @throws RepositoryException If another error occurs. + * @since 2.10.2 + */ + Node getNodeOrNull(final String absPath) throws RepositoryException; + } \ No newline at end of file diff --git a/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java b/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java index 4917d76..fea4b42 100644 --- a/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java +++ b/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java @@ -706,6 +706,36 @@ public class SessionImpl extends AbstractSession return userManager; } + @Override + public Item getItemOrNull(String absPath) throws RepositoryException { + // TODO optimise, reduce to a single read operation + if (itemExists(absPath)) { + return getItem(absPath); + } else { + return null; + } + } + + @Override + public Property getPropertyOrNull(String absPath) throws RepositoryException { + // TODO optimise, reduce to a single read operation + if (propertyExists(absPath)) { + return getProperty(absPath); + } else { + return null; + } + } + + @Override + public Node getNodeOrNull(String absPath) throws RepositoryException { + // TODO optimise, reduce to a single read operation + if (nodeExists(absPath)) { + return getNode(absPath); + } else { + return null; + } + } + //--------------------------------------------------------------< Session > /** * {@inheritDoc} diff --git a/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/GetOrNullTest.java b/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/GetOrNullTest.java new file mode 100644 index 0000000..b7fc5df --- /dev/null +++ b/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/GetOrNullTest.java @@ -0,0 +1,101 @@ +/* + * 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.jackrabbit.core.integration; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.apache.jackrabbit.commons.JcrUtils; +import org.apache.jackrabbit.test.AbstractJCRTest; + +import javax.jcr.Item; +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; + +/** + * @see JCR-3870 + */ +public class GetOrNullTest extends AbstractJCRTest { + + public static String NAME_EXISTING_PROPERTY = "property1"; + public static String PATH_EXISTING_NODE = "/node1"; + public static String PATH_NON_EXISTING_NODE = "/non-existing-node"; + public static String PATH_EXISTING_PROPERTY = PATH_EXISTING_NODE + "/" + NAME_EXISTING_PROPERTY; + public static String PATH_NON_EXISTING_PROPERTY = PATH_EXISTING_NODE + "/non-existing-property"; + + public void setUp() throws Exception { + super.setUp(); + + Node node = JcrUtils.getOrCreateByPath(PATH_EXISTING_NODE, "nt:unstructured", superuser); + node.setProperty(NAME_EXISTING_PROPERTY, "value"); + superuser.save(); + } + + public void testGetItemOrNullExistingNode() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Item item = js.getItemOrNull(PATH_EXISTING_NODE); + assertNotNull(item); + assertTrue(item instanceof Node); + assertEquals(item.getPath(), PATH_EXISTING_NODE); + } + + public void testGetItemOrNullNonExistingNode() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Item item = js.getItemOrNull(PATH_NON_EXISTING_NODE); + assertNull(item); + } + + public void testGetItemOrNullExistingProperty() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Item item = js.getItemOrNull(PATH_EXISTING_PROPERTY); + assertNotNull(item); + assertTrue(item instanceof Property); + assertEquals(item.getPath(), PATH_EXISTING_PROPERTY); + } + + public void testGetItemOrNullNonExistingProperty() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Item item = js.getItemOrNull(PATH_NON_EXISTING_PROPERTY); + assertNull(item); + } + + public void testGetNodeOrNullExisting() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Node node = js.getNodeOrNull(PATH_EXISTING_NODE); + assertNotNull(node); + assertEquals(node.getPath(), PATH_EXISTING_NODE); + } + + public void testGetNodeOrNullNonExisting() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Node node = js.getNodeOrNull(PATH_NON_EXISTING_NODE); + assertNull(node); + } + + public void testGetPropertyOrNullExisting() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Property property = js.getPropertyOrNull(PATH_EXISTING_PROPERTY); + assertNotNull(property); + assertEquals(property.getPath(), PATH_EXISTING_PROPERTY); + } + + public void testGetPropertyOrNullNonExisting() throws RepositoryException { + JackrabbitSession js = (JackrabbitSession) superuser; + Property property = js.getPropertyOrNull(PATH_NON_EXISTING_PROPERTY); + assertNull(property); + } + +} diff --git a/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java b/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java index 7714454..fc39550 100644 --- a/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java +++ b/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/user/UserImporterTest.java @@ -1659,6 +1659,18 @@ public class UserImporterTest extends AbstractJCRTest { }; } + public Item getItemOrNull(String absPath) throws RepositoryException { + return null; + } + + public Property getPropertyOrNull(String absPath) throws RepositoryException { + return null; + } + + public Node getNodeOrNull(String absPath) throws RepositoryException { + return null; + } + public Repository getRepository() { return null; } -- 2.3.6