You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2016/09/08 11:34:20 UTC
svn commit: r1759782 - in /jackrabbit/trunk:
jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java
jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
Author: reschke
Date: Thu Sep 8 11:34:20 2016
New Revision: 1759782
URL: http://svn.apache.org/viewvc?rev=1759782&view=rev
Log:
JCR-4015: jackrabbit-jcr-commons JcrUtils.getOrCreateByPath fails if session is not allowed to read root
Modified:
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java
jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java?rev=1759782&r1=1759781&r2=1759782&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java Thu Sep 8 11:34:20 2016
@@ -35,14 +35,14 @@ public class UtilsGetPathTest extends Ab
@Test
public void testGetOrCreateByPath1() throws RepositoryException {
- String path ="/foo";
+ String path = testRoot + "/foo";
Node node = JcrUtils.getOrCreateByPath(path, "nt:unstructured", superuser);
superuser.save();
assertEquals(path, node.getPath());
assertTrue(superuser.nodeExists(path));
// existing top-level node, two new descendant nodes
- String path2 ="/foo/a/b";
+ String path2 = testRoot + "/foo/a/b";
Node node2 = JcrUtils.getOrCreateByPath(path2, "nt:unstructured", superuser);
superuser.save();
assertEquals(path2, node2.getPath());
@@ -51,18 +51,38 @@ public class UtilsGetPathTest extends Ab
@Test
public void testGetOrCreateByPathNoRoot() throws RepositoryException {
- Node inter = JcrUtils.getOrCreateByPath("/foo", "nt:unstructured", superuser);
- assertEquals("/foo", inter.getPath());
+ String base = testRoot + "/foo";
+ Node inter = JcrUtils.getOrCreateByPath(base, "nt:unstructured", superuser);
+ assertEquals(base, inter.getPath());
superuser.save();
// test what happens if getRootNode() throws
- final Session mockedSession = Mockito.spy(superuser);
+ Session mockedSession = Mockito.spy(superuser);
Mockito.when(mockedSession.getRootNode()).thenThrow(new AccessDeniedException("access denied"));
Mockito.when(mockedSession.getNode("/")).thenThrow(new AccessDeniedException("access denied"));
Mockito.when(mockedSession.getItem("/")).thenThrow(new AccessDeniedException("access denied"));
+ Mockito.when(mockedSession.nodeExists("/")).thenReturn(false);
- Node result = JcrUtils.getOrCreateByPath("/foo/bar", false, null, null, mockedSession, false);
- superuser.save();
- assertEquals("/foo/bar", result.getPath());
+ Node result = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
+ mockedSession.save();
+ assertEquals(base + "/bar", result.getPath());
+
+ // already exists -> nop
+ Node result2 = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
+ mockedSession.save();
+ assertEquals(base + "/bar", result2.getPath());
+
+ // create unique
+ Node result3 = JcrUtils.getOrCreateByPath(base + "/bar", true, null, null, mockedSession, false);
+ mockedSession.save();
+ assertEquals(base + "/bar0", result3.getPath());
+
+ // already exists with createUnique == false should pass even when parent isn't readable
+ Mockito.when(mockedSession.getNode(base)).thenThrow(new AccessDeniedException("access denied"));
+ Mockito.when(mockedSession.getItem(base)).thenThrow(new AccessDeniedException("access denied"));
+ Mockito.when(mockedSession.nodeExists(base)).thenReturn(false);
+ Node result4 = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
+ mockedSession.save();
+ assertEquals(base + "/bar", result4.getPath());
}
}
Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java?rev=1759782&r1=1759781&r2=1759782&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java Thu Sep 8 11:34:20 2016
@@ -1450,6 +1450,8 @@ public class JcrUtils {
return session.getRootNode();
} else if (!absolutePath.startsWith("/")) {
throw new IllegalArgumentException("not an absolute path: " + absolutePath);
+ } else if (session.nodeExists(absolutePath) && !createUniqueLeaf) {
+ return session.getNode(absolutePath);
} else {
// find deepest existing parent node
String path = absolutePath;