Search in sources :

Example 31 with InvalidItemStateException

use of javax.jcr.InvalidItemStateException in project jackrabbit by apache.

the class IsSameTest method testNewNodeIsSame.

public void testNewNodeIsSame() throws RepositoryException {
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    assertTrue(n.isSame(n));
    n.remove();
    Node n2 = testRootNode.addNode(nodeName1, testNodeType);
    try {
        assertFalse(n2.isSame(n));
    } catch (InvalidItemStateException e) {
    // ok as well
    }
    try {
        assertFalse(n.isSame(n2));
    } catch (InvalidItemStateException e) {
    // ok as well
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node)

Example 32 with InvalidItemStateException

use of javax.jcr.InvalidItemStateException in project jackrabbit by apache.

the class ExternalModificationTest method testExternalRemoval3.

public void testExternalRemoval3() throws RepositoryException, NotExecutableException {
    Node childN = refNode.addNode(nodeName3);
    Property p = childN.setProperty(propertyName1, "anyvalue");
    refNode.save();
    String uuid = refNode.getUUID();
    Node refNode2 = testSession.getNodeByUUID(uuid);
    Node c2 = (Node) testSession.getItem(childN.getPath());
    Property p2 = (Property) testSession.getItem(p.getPath());
    // transiently modify  -> test effect of external removal.
    p2.setValue("changedValue");
    String srcPath = refNode.getPath();
    String destPath = destParentNode.getPath() + "/" + nodeName2;
    superuser.move(srcPath, destPath);
    superuser.save();
    try {
        refNode2.refresh(true);
        Node parent = refNode2.getParent();
    } catch (InvalidItemStateException e) {
    }
    assertItemStatus(refNode2, Status.REMOVED);
    assertItemStatus(c2, Status.REMOVED);
    assertItemStatus(p2, Status.STALE_DESTROYED);
    assertEquals("changedValue", p2.getString());
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node) Property(javax.jcr.Property)

Example 33 with InvalidItemStateException

use of javax.jcr.InvalidItemStateException in project jackrabbit by apache.

the class OverlappingNodeAddTest method testWith2Folders.

/**
     * Performs add operations on a single node (no SNS) through 2 sessions
     */
public void testWith2Folders() throws Exception {
    boolean bWasSaved = false;
    String testpath = testfolder.getPath();
    Node f1 = s1.getNode(testpath).addNode("folder", "nt:folder");
    Node c1 = f1.addNode("a", "nt:file");
    Node r1 = c1.addNode("jcr:content", "nt:resource");
    r1.setProperty("jcr:data", "foo");
    Node f2 = s2.getNode(testpath).addNode("folder", "nt:folder");
    Node c2 = f2.addNode("b", "nt:file");
    Node r2 = c2.addNode("jcr:content", "nt:resource");
    r2.setProperty("jcr:data", "bar");
    s1.save();
    String s1FolderId = f1.getIdentifier();
    try {
        s2.save();
        bWasSaved = true;
    } catch (InvalidItemStateException ex) {
        // retry; adding refresh doesn't change anything here
        try {
            s2.save();
            bWasSaved = true;
        } catch (InvalidItemStateException ex2) {
        // we would be cool with this
        }
    }
    // we don't have changes in s1, so the keepChanges flag should be
    // irrelevant
    s1.refresh(false);
    // be nice and get a new Node instance
    Node newf1 = s1.getNode(testpath + "/folder");
    // if bWasSaved it should now be visible to Session 1
    assertEquals("'b' was saved, so session 1 should see it", bWasSaved, newf1.hasNode("b"));
    // 'a' was saved by Session 1 earlier on
    if (!newf1.hasNode("a")) {
        String message = "child node 'a' not present";
        if (bWasSaved && !s1FolderId.equals(newf1.getIdentifier())) {
            message += ", and also the folder's identifier changed from " + s1FolderId + " to " + newf1.getIdentifier();
        }
        Node oldf1 = null;
        try {
            oldf1 = s1.getNodeByIdentifier(s1FolderId);
        } catch (ItemNotFoundException ex) {
            message += "; node with id " + s1FolderId + " can't be retrieved using getNodeByIdentifier either";
        }
        if (oldf1 != null) {
            try {
                oldf1.getPath();
            } catch (Exception ex) {
                message += "; node with id " + s1FolderId + " can be retrieved using getNodeByIdentifier, but getPath() fails with: " + ex.getMessage();
            }
        }
        fail(message);
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemNotFoundException(javax.jcr.ItemNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 34 with InvalidItemStateException

use of javax.jcr.InvalidItemStateException in project jackrabbit by apache.

the class TransientItemStateManager method getChangeLog.

/**
     * Create the change log for the tree starting at <code>target</code>. This
     * includes a  check if the ChangeLog to be created is totally 'self-contained'
     * and independent; items within the scope of this update operation (i.e.
     * below the target) must not have dependencies outside of this tree (e.g.
     * moving a node requires that the target node including both old and new
     * parents are saved).
     *
     * @param target
     * @param throwOnStale Throws InvalidItemStateException if either the given
     * <code>ItemState</code> or any of its descendants is stale and the flag is true.
     * @return
     * @throws InvalidItemStateException if a stale <code>ItemState</code> is
     * encountered while traversing the state hierarchy. The <code>changeLog</code>
     * might have been populated with some transient item states. A client should
     * therefore not reuse the <code>changeLog</code> if such an exception is thrown.
     * @throws RepositoryException if <code>state</code> is a new item state.
     */
ChangeLog getChangeLog(ItemState target, boolean throwOnStale) throws InvalidItemStateException, ConstraintViolationException, RepositoryException {
    // fail-fast test: check status of this item's state
    if (target.getStatus() == Status.NEW) {
        String msg = "Cannot save/revert an item with status NEW (" + target + ").";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    if (throwOnStale && Status.isStale(target.getStatus())) {
        String msg = "Attempt to save/revert an item, that has been externally modified (" + target + ").";
        log.debug(msg);
        throw new InvalidItemStateException(msg);
    }
    Set<Operation> ops = new LinkedHashSet<Operation>();
    Set<ItemState> affectedStates = new LinkedHashSet<ItemState>();
    HierarchyEntry he = target.getHierarchyEntry();
    if (he.getParent() == null) {
        // simplicity. collecting ops, states can be omitted.
        if (throwOnStale && !staleStates.isEmpty()) {
            String msg = "Cannot save changes: States has been modified externally.";
            log.debug(msg);
            throw new InvalidItemStateException(msg);
        } else {
            affectedStates.addAll(staleStates);
        }
        ops.addAll(operations);
        affectedStates.addAll(addedStates);
        affectedStates.addAll(modifiedStates);
        affectedStates.addAll(removedStates);
    } else {
        // - check if there is a stale state in the scope (save only)
        if (throwOnStale) {
            for (ItemState state : staleStates) {
                if (containedInTree(target, state)) {
                    String msg = "Cannot save changes: States has been modified externally.";
                    log.debug(msg);
                    throw new InvalidItemStateException(msg);
                }
            }
        }
        // - collect all affected states within the scope of save/undo
        Iterator[] its = new Iterator[] { addedStates.iterator(), removedStates.iterator(), modifiedStates.iterator() };
        IteratorChain chain = new IteratorChain(its);
        if (!throwOnStale) {
            chain.addIterator(staleStates.iterator());
        }
        while (chain.hasNext()) {
            ItemState state = (ItemState) chain.next();
            if (containedInTree(target, state)) {
                affectedStates.add(state);
            }
        }
        //   changelog.
        for (Operation op : operations) {
            Collection<ItemState> opStates = op.getAffectedItemStates();
            for (ItemState state : opStates) {
                if (affectedStates.contains(state)) {
                    // operation needs to be included
                    if (!affectedStates.containsAll(opStates)) {
                        // incomplete changelog: need to save a parent as well
                        String msg = "ChangeLog is not self contained.";
                        throw new ConstraintViolationException(msg);
                    }
                    // no violation: add operation an stop iteration over
                    // all affected states present in the operation.
                    ops.add(op);
                    break;
                }
            }
        }
    }
    ChangeLog cl = new ChangeLog(target, ops, affectedStates);
    return cl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InvalidItemStateException(javax.jcr.InvalidItemStateException) RepositoryException(javax.jcr.RepositoryException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) HierarchyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyEntry) IteratorChain(org.apache.commons.collections.iterators.IteratorChain) Iterator(java.util.Iterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 35 with InvalidItemStateException

use of javax.jcr.InvalidItemStateException in project jackrabbit-oak by apache.

the class SimpleSearchTest method beforeSuite.

@Override
public void beforeSuite() throws RepositoryException {
    session = getRepository().login(getCredentials());
    try {
        ensurePropertyIndex();
    } catch (InvalidItemStateException e) {
        // some other oak instance probably created the same
        // index definition concurrently. refresh and try again
        // do not catch exception if it fails again.
        session.refresh(false);
        ensurePropertyIndex();
    }
    root = session.getRootNode().addNode("testroot" + TEST_ID, "nt:unstructured");
    for (int i = 0; i < NODE_COUNT; i++) {
        Node node = root.addNode("node" + i, "nt:unstructured");
        for (int j = 0; j < NODE_COUNT; j++) {
            Node child = node.addNode("node" + j, "nt:unstructured");
            child.setProperty("testcount", j);
        }
        session.save();
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node)

Aggregations

InvalidItemStateException (javax.jcr.InvalidItemStateException)100 Node (javax.jcr.Node)67 Session (javax.jcr.Session)35 Test (org.junit.Test)21 Property (javax.jcr.Property)15 RepositoryException (javax.jcr.RepositoryException)14 NodeIterator (javax.jcr.NodeIterator)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 QueryResult (javax.jcr.query.QueryResult)5 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)4 NodeType (javax.jcr.nodetype.NodeType)3 SessionImpl (org.apache.jackrabbit.core.SessionImpl)3 NodeId (org.apache.jackrabbit.core.id.NodeId)3 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)3 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)3 NodeState (org.apache.jackrabbit.core.state.NodeState)3 PathNotFoundException (javax.jcr.PathNotFoundException)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)2 Version (javax.jcr.version.Version)2