-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OAK-11000: Test coverage for common JCR operations related to importing content #1622
Open
reschke
wants to merge
13
commits into
trunk
Choose a base branch
from
OAK-11000
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
50c1fd1
OAK-11000: Test coverage for common JCR operations related to importi…
reschke 4f57156
OAK-11000: Test coverage for common JCR operations related to importi…
reschke 737408f
OAK-11000:rename to ProtectedPropertyTest
reschke 0025cf4
OAK-11000: fix comment about adding mixin:created behavior (ack kwin)
reschke 0517fd4
OAK-11000: confirm that removeMixin(mix:referenceable) fails when inh…
reschke 9b7814a
OAK-11000: confirm that setting jcr:uuid as custom property will fail…
reschke 3173b5c
OAK-11000: check that jcr:uuid is gone after removing from nt:unstruc…
reschke 9401366
OAK-11000: add variant that uses a temporary node type with unprotect…
reschke a4cf43e
OAK-11000: use constants from java.jcr.Property
reschke 1399ce1
OAK-11000: test overlapping updates with same UUID using two sessions
reschke c802642
OAK-11000: add tests for getNodeByIdentifier
reschke f4fc2a2
OAK-11000: adjust expection for exceptions for DOCUMENT_NS
reschke fbc3bf1
OAK-11000: add tests for just setting conflicting jcr:uuids on nodes …
reschke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
376 changes: 376 additions & 0 deletions
376
oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ProtectedPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,376 @@ | ||
/* | ||
* 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.oak.jcr; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.PathNotFoundException; | ||
import javax.jcr.Property; | ||
import javax.jcr.PropertyIterator; | ||
import javax.jcr.PropertyType; | ||
import javax.jcr.ReferentialIntegrityException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.nodetype.ConstraintViolationException; | ||
import javax.jcr.nodetype.NoSuchNodeTypeException; | ||
import javax.jcr.nodetype.NodeType; | ||
import javax.jcr.nodetype.NodeTypeManager; | ||
import javax.jcr.nodetype.NodeTypeTemplate; | ||
import javax.jcr.nodetype.PropertyDefinitionTemplate; | ||
|
||
import org.apache.jackrabbit.oak.fixture.NodeStoreFixture; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test coverage for common JCR operations related to importing content. | ||
* <p> | ||
* Note that the purpose of these tests is not to check conformance with the JCR | ||
* specification, but to observe the actual behavior of the implementation | ||
* (which may be hard to change). | ||
*/ | ||
public class ProtectedPropertyTest extends AbstractRepositoryTest { | ||
|
||
private Session session; | ||
private Node testNode; | ||
private static String TEST_NODE_NAME = "ImportOperationsTest"; | ||
private static String TEST_NODE_NAME_REF = "ImportOperationsTest-Reference"; | ||
private static String TEST_NODE_NAME_TMP = "ImportOperationsTest-Temp"; | ||
|
||
public ProtectedPropertyTest(NodeStoreFixture fixture) { | ||
super(fixture); | ||
} | ||
|
||
@Before | ||
public void setup() throws RepositoryException { | ||
this.session = getAdminSession(); | ||
this.testNode = session.getRootNode().addNode("import-tests", NodeType.NT_UNSTRUCTURED); | ||
session.save(); | ||
} | ||
|
||
@Test | ||
public void jcrMixinCreatedOnNtUnstructured() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
try { | ||
test.setProperty("jcr:created", false); | ||
session.save(); | ||
test.addMixin(NodeType.MIX_CREATED); | ||
session.save(); | ||
assertEquals(false, test.getProperty("jcr:created").getBoolean()); | ||
// in Oak, existing properties are left as-is (even the property | ||
// type), which means that after adding the mixin:created type, the | ||
// state of the node might be inconsistent with the mixin:created | ||
// type. This may come as a surprise, but is allowed as | ||
// implementation specific behavior, see | ||
// https://developer.adobe.com/experience-manager/reference-materials/spec/jcr/2.0/3_Repository_Model.html#3.7.11.7%20mix:created | ||
} finally { | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void jcrMixinReferenceableOnNtUnstructuredBeforeSettingMixin() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
try { | ||
String testUuid = UUID.randomUUID().toString(); | ||
test.setProperty("jcr:uuid", testUuid); | ||
session.save(); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
// JCR spec | ||
// (https://developer.adobe.com/experience-manager/reference-materials/spec/jcr/2.0/3_Repository_Model.html#3.8%20Referenceable%20Nodes) | ||
// requests an "auto-created" property, so it might be a surprise | ||
// that Oak actually keeps the application-assigned previous value. | ||
assertEquals(testUuid, test.getProperty("jcr:uuid").getString()); | ||
} finally { | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void jcrMixinReferenceableOnNtUnstructuredBeforeSettingMixinButWithConflict() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
Node ref = testNode.addNode(TEST_NODE_NAME_REF, NodeType.NT_UNSTRUCTURED); | ||
ref.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
|
||
try { | ||
String testUuid = ref.getProperty("jcr:uuid").getString(); | ||
test.setProperty("jcr:uuid", testUuid); | ||
// note this fails even though test hasn't be set to mix:referenceable | ||
session.save(); | ||
fail("Attempt so set a UUID already in use should fail"); | ||
} catch (ConstraintViolationException ex) { | ||
// expected | ||
} finally { | ||
test.remove(); | ||
ref.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void jcrMixinReferenceableOnNtUnstructuredAfterSettingMixin() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
try { | ||
String testUuid = UUID.randomUUID().toString(); | ||
test.setProperty("jcr:uuid", testUuid); | ||
session.save(); | ||
fail("Setting jcr:uuid after adding mixin:referenceable should fail"); | ||
} catch (ConstraintViolationException ex) { | ||
// expected | ||
} finally { | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void jcrMixinReferenceableOnNtUnstructuredAfterRemovingMixin() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
try { | ||
// check jcr:uuid is there | ||
String prevUuid = test.getProperty("jcr:uuid").getString(); | ||
test.removeMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
// ist jcr:uuid gone now? | ||
try { | ||
String newUuid = test.getProperty("jcr:uuid").getString(); | ||
fail("jcr:uuid should be gone after removing the mixin type, was " + prevUuid + ", now is " + newUuid); | ||
} catch (PathNotFoundException ex) { | ||
// expected | ||
} | ||
String testUuid = UUID.randomUUID().toString(); | ||
test.setProperty("jcr:uuid", testUuid); | ||
session.save(); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
assertEquals(testUuid, test.getProperty("jcr:uuid").getString()); | ||
Node check = session.getNodeByIdentifier(testUuid); | ||
assertTrue(test.isSame(check)); | ||
} finally { | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void jcrMixinReferenceableOnNtUnstructuredAfterRemovingMixinButDanglingReference() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
Node ref = testNode.addNode(TEST_NODE_NAME_REF, NodeType.NT_UNSTRUCTURED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Earlier the name ref was used for the referenced node, not the referencing one. So we might want to change that to avoid confusion. |
||
ref.setProperty("reference", test.getIdentifier(), PropertyType.REFERENCE); | ||
session.save(); | ||
|
||
try { | ||
test.removeMixin(NodeType.MIX_REFERENCEABLE); | ||
String testUuid = UUID.randomUUID().toString(); | ||
test.setProperty("jcr:uuid", testUuid); | ||
session.save(); | ||
fail("Changing jcr:uuid causing a dangling refence should fail"); | ||
} catch (ReferentialIntegrityException ex) { | ||
// expected | ||
} finally { | ||
ref.remove(); | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void changeUuidOnReferencedNodeWithOnlyMixin() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_UNSTRUCTURED); | ||
test.addMixin(NodeType.MIX_REFERENCEABLE); | ||
session.save(); | ||
Node ref = testNode.addNode(TEST_NODE_NAME_REF, NodeType.NT_UNSTRUCTURED); | ||
ref.setProperty("reference", test.getIdentifier(), PropertyType.REFERENCE); | ||
session.save(); | ||
|
||
try { | ||
String newUuid = UUID.randomUUID().toString(); | ||
updateJcrUuidUsingRemoveMixin(test, newUuid); | ||
assertEquals(newUuid, test.getProperty("jcr:uuid").getString()); | ||
session.save(); | ||
assertEquals(newUuid, test.getProperty("jcr:uuid").getString()); | ||
} finally { | ||
ref.remove(); | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void changeUuidOnReferencedNodeWithInheritedMixin() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_RESOURCE); | ||
test.setProperty("jcr:data", session.getValueFactory().createBinary(new ByteArrayInputStream(new byte[0]))); | ||
session.save(); | ||
Node ref = testNode.addNode(TEST_NODE_NAME_REF, NodeType.NT_UNSTRUCTURED); | ||
ref.setProperty("reference", test.getIdentifier(), PropertyType.REFERENCE); | ||
session.save(); | ||
|
||
try { | ||
String newUuid = UUID.randomUUID().toString(); | ||
updateJcrUuidUsingRemoveMixin(test, newUuid); | ||
fail("removing mixin:referenceable should fail on nt:resource"); | ||
} catch (NoSuchNodeTypeException ex) { | ||
// expected | ||
} finally { | ||
ref.remove(); | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
@Test | ||
public void changeUuidOnReferencedNodeWithInheritedMixinByChangingNodeTypeTemporarily() throws RepositoryException { | ||
Node test = testNode.addNode(TEST_NODE_NAME, NodeType.NT_RESOURCE); | ||
test.setProperty("jcr:data", session.getValueFactory().createBinary(new ByteArrayInputStream(new byte[0]))); | ||
session.save(); | ||
Node ref = testNode.addNode(TEST_NODE_NAME_REF, NodeType.NT_UNSTRUCTURED); | ||
ref.setProperty("reference", test.getIdentifier(), PropertyType.REFERENCE); | ||
session.save(); | ||
|
||
try { | ||
String newUuid = UUID.randomUUID().toString(); | ||
updateJcrUuidUsingNodeTypeManager(test, newUuid); | ||
assertEquals(newUuid, test.getProperty("jcr:uuid").getString()); | ||
session.save(); | ||
session.refresh(false); | ||
assertEquals(newUuid, test.getProperty("jcr:uuid").getString()); | ||
} finally { | ||
ref.remove(); | ||
test.remove(); | ||
session.save(); | ||
} | ||
} | ||
|
||
private static void updateJcrUuidUsingRemoveMixin(Node target, String newUUID) throws RepositoryException { | ||
// temporary node for rewriting the references | ||
Node tmp = target.getParent().addNode(TEST_NODE_NAME_TMP, NodeType.NT_UNSTRUCTURED); | ||
tmp.addMixin(NodeType.MIX_REFERENCEABLE); | ||
|
||
try { | ||
// find all existing references to the node for which we want to rewrite the jcr:uuid | ||
Set<Property> referrers = getReferrers(target); | ||
|
||
// move existing references to TEST_MODE_NAME to TEST_NODE_NAME_TMP | ||
setReferrersTo(referrers, tmp.getIdentifier()); | ||
|
||
// rewrite jcr:uuid | ||
target.removeMixin(NodeType.MIX_REFERENCEABLE); | ||
target.setProperty("jcr:uuid", newUUID); | ||
target.addMixin(NodeType.MIX_REFERENCEABLE); | ||
|
||
// restore references | ||
setReferrersTo(referrers, newUUID); | ||
} finally { | ||
tmp.remove(); | ||
} | ||
} | ||
|
||
private static void updateJcrUuidUsingNodeTypeManager(Node target, String newUUID) throws RepositoryException { | ||
// temporary node for rewriting the references | ||
Node tmp = target.getParent().addNode(TEST_NODE_NAME_TMP, NodeType.NT_UNSTRUCTURED); | ||
tmp.addMixin(NodeType.MIX_REFERENCEABLE); | ||
|
||
try { | ||
String previousType = target.getPrimaryNodeType().getName(); | ||
|
||
// find all existing references to the node for which we want to rewrite the jcr:uuid | ||
Set<Property> referrers = getReferrers(target); | ||
|
||
// move existing references to TEST_MODE_NAME to TEST_NODE_NAME_TMP | ||
setReferrersTo(referrers, tmp.getIdentifier()); | ||
|
||
// rewrite jcr:uuid | ||
String temporaryType = registerPrimaryTypeExtendingAndUnprotectingJcrUUID(target); | ||
target.setPrimaryType(temporaryType); | ||
target.setProperty("jcr:uuid", newUUID); | ||
target.setPrimaryType(previousType); | ||
unregisterPrimaryTypeExtendingAndUnprotectingJcrUUID(target, temporaryType); | ||
|
||
// restore references | ||
setReferrersTo(referrers, newUUID); | ||
|
||
// assert temporary node type is gone | ||
try { | ||
target.getSession().getWorkspace().getNodeTypeManager().getNodeType(temporaryType); | ||
fail("temporary node type should be removed"); | ||
} catch (NoSuchNodeTypeException ex) { | ||
// expected | ||
} | ||
} finally { | ||
tmp.remove(); | ||
} | ||
} | ||
|
||
private static Set<Property> getReferrers(Node to) throws RepositoryException { | ||
Set<Property> referrers = new HashSet<>(); | ||
PropertyIterator pit = to.getReferences(); | ||
while (pit.hasNext()) { | ||
referrers.add(pit.nextProperty()); | ||
} | ||
return referrers; | ||
} | ||
|
||
private static void setReferrersTo(Set<Property> referrers, String identifier) throws RepositoryException { | ||
for (Property p : referrers) { | ||
// add case for multivalued | ||
p.getParent().setProperty(p.getName(), identifier); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static String registerPrimaryTypeExtendingAndUnprotectingJcrUUID(Node node) throws RepositoryException { | ||
String tmpNodeTypeName = "tmp-" + UUID.randomUUID().toString(); | ||
NodeTypeManager ntMgr = node.getSession().getWorkspace().getNodeTypeManager(); | ||
|
||
NodeTypeTemplate unprotectedNTT = ntMgr.createNodeTypeTemplate(); | ||
unprotectedNTT.setName(tmpNodeTypeName); | ||
unprotectedNTT.setDeclaredSuperTypeNames(new String[] {node.getPrimaryNodeType().getName()}); | ||
PropertyDefinitionTemplate pdt = ntMgr.createPropertyDefinitionTemplate(); | ||
pdt.setName("jcr:uuid"); | ||
pdt.setProtected(false); | ||
unprotectedNTT.getPropertyDefinitionTemplates().add(pdt); | ||
ntMgr.registerNodeType(unprotectedNTT, true); | ||
|
||
return tmpNodeTypeName; | ||
} | ||
|
||
private static void unregisterPrimaryTypeExtendingAndUnprotectingJcrUUID(Node node, String tmpType) throws RepositoryException { | ||
NodeTypeManager ntMgr = node.getSession().getWorkspace().getNodeTypeManager(); | ||
|
||
ntMgr.unregisterNodeType(tmpType); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The algorithm used to create the property value is free to use the existing one and I think that is also the most sane approach, so is it really that surprising?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the spec says "autocreated" :-)