Skip to content
Permalink
Browse files
Merge pull request #609 from apache/OAK-9819
OAK-9819 : Improve Node.hasProperty/getProperty and Node.hasNode/getNode combinations
  • Loading branch information
joerghoh committed Jul 1, 2022
2 parents 72fd3d0 + e40907d commit 0adc1626811d1b7c590eedb58d5b7391bfef08ed
Showing 5 changed files with 172 additions and 3 deletions.
@@ -16,6 +16,11 @@
*/
package org.apache.jackrabbit.api;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
@@ -26,7 +31,7 @@
* The Jackrabbit Node interface. This interface contains the
* Jackrabbit-specific extensions to the JCR {@link javax.jcr.Node} interface.
*/
public interface JackrabbitNode {
public interface JackrabbitNode extends Node {

/**
*
@@ -47,4 +52,37 @@ public interface JackrabbitNode {
void setMixins(String[] mixinNames)
throws NoSuchNodeTypeException, VersionException,
ConstraintViolationException, LockException, RepositoryException;

/**
* Returns the node at {@code relPath} relative to {@code this} node or {@code null} if no such node exists.
* The same reacquisition semantics apply as with {@link #getNode(String)}.
*
* @param relPath The relative path of the node to retrieve.
* @return The node at {@code relPath} or {@code null}
* @throws RepositoryException If an error occurs.
*/
default @Nullable JackrabbitNode getNodeOrNull(@NotNull String relPath) throws RepositoryException {
if (hasNode(relPath)) {
Node n = getNode(relPath);
return (n instanceof JackrabbitNode) ? (JackrabbitNode) n : null;
} else {
return null;
}
}

/**
* Returns the property at {@code relPath} relative to {@code this} node or {@code null} if no such property exists.
* The same reacquisition semantics apply as with {@link #getNode(String)}.
*
* @param relPath The relative path of the property to retrieve.
* @return The property at {@code relPath} or {@code null}
* @throws RepositoryException If an error occurs.
*/
default @Nullable Property getPropertyOrNull(@NotNull String relPath) throws RepositoryException {
if (hasProperty(relPath)) {
return getProperty(relPath);
} else {
return null;
}
}
}
@@ -18,5 +18,5 @@
/**
* Jackrabbit extensions for JCR core interfaces
*/
@org.osgi.annotation.versioning.Version("2.7.0")
@org.osgi.annotation.versioning.Version("2.8.0")
package org.apache.jackrabbit.api;
@@ -0,0 +1,69 @@
/*
* 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.api;

import org.junit.Test;
import org.mockito.Answers;

import javax.jcr.Property;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

public class JackrabbitNodeTest {

@Test
public void testGetNodeOrNull() throws Exception {
JackrabbitNode n = mock(JackrabbitNode.class, withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));
JackrabbitNode child = mock(JackrabbitNode.class);

when(n.hasNode("child")).thenReturn(true);
when(n.getNode("child")).thenReturn(child);

assertSame(child, n.getNodeOrNull("child"));
assertNull(n.getNodeOrNull("not/existing"));

verify(n, times(2)).hasNode(anyString());
verify(n, times(1)).getNode(anyString());
verify(n, times(2)).getNodeOrNull(anyString());
verifyNoMoreInteractions(n);
}

@Test
public void testGetPropertyOrNull() throws Exception {
JackrabbitNode n = mock(JackrabbitNode.class, withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));
Property prop = mock(Property.class);

when(n.hasProperty("prop")).thenReturn(true);
when(n.getProperty("prop")).thenReturn(prop);

assertSame(prop, n.getPropertyOrNull("prop"));
assertNull(n.getPropertyOrNull("not/existing"));

verify(n, times(2)).hasProperty(anyString());
verify(n, times(1)).getProperty(anyString());
verify(n, times(2)).getPropertyOrNull(anyString());
verifyNoMoreInteractions(n);
}
}
@@ -107,7 +107,7 @@
*
* @param <T> the delegate type
*/
public class NodeImpl<T extends NodeDelegate> extends ItemImpl<T> implements Node, JackrabbitNode {
public class NodeImpl<T extends NodeDelegate> extends ItemImpl<T> implements JackrabbitNode {

/**
* Use an zero length MVP to check read permission on jcr:mixinTypes (OAK-7652)
@@ -1631,6 +1631,40 @@ public void performVoid() throws RepositoryException {
});
}

@Override
public @Nullable JackrabbitNode getNodeOrNull(@NotNull String relPath) throws RepositoryException {
final String oakPath = getOakPathOrThrowNotFound(relPath);
return sessionDelegate.performNullable(new NodeOperation<JackrabbitNode>(dlg, "getNodeOrNull") {
@Nullable
@Override
public JackrabbitNode performNullable() throws RepositoryException {
NodeDelegate nd = node.getChild(oakPath);
if (nd == null) {
return null;
} else {
return createNode(nd, sessionContext);
}
}
});
}

@Override
public @Nullable Property getPropertyOrNull(@NotNull String relPath) throws RepositoryException {
final String oakPath = getOakPathOrThrowNotFound(relPath);
return sessionDelegate.performNullable(new NodeOperation<PropertyImpl>(dlg, "getPropertyOrNull") {
@Nullable
@Override
public PropertyImpl performNullable() throws RepositoryException {
PropertyDelegate pd = node.getPropertyOrNull(oakPath);
if (pd == null) {
return null;
} else {
return new PropertyImpl(pd, sessionContext);
}
}
});
}

/**
* Provide current node path. Should be invoked from within
* the SessionDelegate#perform and preferred instead of getPath
@@ -36,6 +36,7 @@
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.api.JackrabbitNode;
import org.apache.jackrabbit.commons.cnd.CndImporter;
import org.apache.jackrabbit.oak.spi.nodetype.NodeTypeConstants;
import org.apache.jackrabbit.test.AbstractJCRTest;

/**
@@ -308,4 +309,31 @@ public void testSetMixins() throws RepositoryException {
n.remove();
superuser.save();
}

public void testGetNodeOrNull() throws Exception {
JackrabbitNode jn = (JackrabbitNode) testRootNode;
Node aa = jn.addNode("a/aa", NodeTypeConstants.NT_OAK_UNSTRUCTURED);
aa.setProperty("p", true);

assertNull(jn.getNodeOrNull("notExisting"));
assertNull(jn.getNodeOrNull("notExisting/rel/path"));
assertNull(jn.getNodeOrNull(JcrConstants.JCR_PRIMARYTYPE));
assertNull(jn.getNodeOrNull("a/aa/p"));

assertNotNull(jn.getNodeOrNull("a"));
assertNotNull(jn.getNodeOrNull("a/aa"));
}

public void testGetPropertyOrNull() throws Exception {
JackrabbitNode jn = (JackrabbitNode) testRootNode;
Node aa = jn.addNode("a/aa", NodeTypeConstants.NT_OAK_UNSTRUCTURED);
aa.setProperty("p", "value");

assertNull(jn.getPropertyOrNull("notExisting"));
assertNull(jn.getPropertyOrNull("notExisting/rel/path"));
assertNull(jn.getPropertyOrNull("a/aa"));

assertNotNull(jn.getPropertyOrNull(JcrConstants.JCR_PRIMARYTYPE));
assertNotNull(jn.getPropertyOrNull("a/aa/p"));
}
}

0 comments on commit 0adc162

Please sign in to comment.