Search in sources :

Example 66 with InvalidItemStateException

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

the class NodeTest method testSaveInvalidStateException.

/**
     * Tries to save a node using {@link javax.jcr.Node#save()} that was already
     * deleted by an other session.
     * <p>
     * Procedure: <ul> <li>Creates a new
     * node with session 1, saves it, adds a child node.</li> <li>Access new
     * node with session 2,deletes the node, saves it.</li> <li>Session 1 tries
     * to save modifications using <code>Node.save()</code> on root node .</li>
     * </ul> This should throw an {@link javax.jcr.InvalidItemStateException}.
     * <p>
     * Prerequisites: <ul> <li><code>javax.jcr.tck.nodetype</code>
     * must accept children of same nodetype</li> </ul>
     */
public void testSaveInvalidStateException() throws RepositoryException {
    // get default workspace test root node using superuser session
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // create a node
    Node nodeSession1 = defaultRootNode.addNode(nodeName1, testNodeType);
    // save new node
    superuser.save();
    // make a modification
    nodeSession1.addNode(nodeName2, testNodeType);
    // get the new node with a different session
    Session testSession = getHelper().getReadWriteSession();
    try {
        Node nodeSession2 = (Node) testSession.getItem(nodeSession1.getPath());
        // delete the node with the new session
        nodeSession2.remove();
        // make node removal persistent
        testSession.save();
        // save changes made wit superuser session
        try {
            defaultRootNode.save();
            fail("Saving a modified Node using Node.save() already deleted by an other session should throw InvalidItemStateException");
        } catch (InvalidItemStateException e) {
        // ok, works as expected
        }
    } finally {
        testSession.logout();
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 67 with InvalidItemStateException

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

the class CheckinTest method testCheckinWithPendingChangesJcr2.

/**
     * Test if VersionManager.checkin(P) throws InvalidItemStateException if
     * the path P resolves to a node that has unsaved changes pending.
     *
     * @throws RepositoryException
     */
public void testCheckinWithPendingChangesJcr2() throws RepositoryException {
    try {
        // modify node without calling save()
        versionableNode.setProperty(propertyName1, propertyValue);
        VersionManager versionManager = versionableNode.getSession().getWorkspace().getVersionManager();
        String path = versionableNode.getPath();
        versionManager.checkin(path);
        fail("InvalidItemStateException must be thrown on attempt to checkin a node having any unsaved changes pending.");
    } catch (InvalidItemStateException e) {
    // ok
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) VersionManager(javax.jcr.version.VersionManager)

Example 68 with InvalidItemStateException

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

the class WorkspaceRestoreTest method testWorkspaceRestoreWithPendingChangesJcr2.

/**
     * Test if InvalidItemStateException is thrown if the session affected by
     * VersionManager.restore(Version[], boolean) has pending changes.
     */
public void testWorkspaceRestoreWithPendingChangesJcr2() throws RepositoryException {
    versionableNode.getSession().getWorkspace().getVersionManager().checkout(versionableNode.getPath());
    try {
        // modify node without calling save()
        versionableNode.setProperty(propertyName1, propertyValue);
        // create version in second workspace
        Version v = wVersionableNode.getSession().getWorkspace().getVersionManager().checkin(wVersionableNode.getPath());
        // try to restore that version
        superuser.getWorkspace().getVersionManager().restore(new Version[] { v }, false);
        fail("InvalidItemStateException must be thrown on attempt to call Workspace.restore(Version[], boolean) in a session having any unsaved changes pending.");
    } catch (InvalidItemStateException e) {
    // success
    }
}
Also used : Version(javax.jcr.version.Version) InvalidItemStateException(javax.jcr.InvalidItemStateException)

Example 69 with InvalidItemStateException

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

the class HierarchyEntryImpl method transientRemove.

/**
     * {@inheritDoc}
     * @see HierarchyEntry#transientRemove()
     */
public void transientRemove() throws InvalidItemStateException, RepositoryException {
    ItemState state = internalGetItemState();
    if (state == null) {
        // nothing to do -> correct status must be set upon resolution.
        return;
    }
    // it in order to determine the current status.
    if (state.getStatus() == Status.INVALIDATED) {
        reload(false);
    }
    switch(state.getStatus()) {
        case Status.NEW:
            state.setStatus(Status.REMOVED);
            parent.internalRemoveChildEntry(this);
            break;
        case Status.EXISTING:
        case Status.EXISTING_MODIFIED:
            state.setStatus(Status.EXISTING_REMOVED);
            // if a conflict with a new entry occurs.
            break;
        case Status.REMOVED:
        case Status.STALE_DESTROYED:
            throw new InvalidItemStateException("Item has already been removed by someone else. Status = " + Status.getName(state.getStatus()));
        default:
            throw new RepositoryException("Cannot transiently remove an ItemState with status " + Status.getName(state.getStatus()));
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemState(org.apache.jackrabbit.jcr2spi.state.ItemState) RepositoryException(javax.jcr.RepositoryException)

Example 70 with InvalidItemStateException

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

the class ConcurrentWriteACLTest method runTest.

@Override
public void runTest() throws Exception {
    Session session = null;
    try {
        session = loginWriter();
        for (int i = 0; i < numItems; i++) {
            session.refresh(false);
            int a = random.nextInt(NODE_COUNT);
            int b = random.nextInt(NODE_COUNT);
            String path = "/" + ROOT_NODE_NAME + "/node" + a + "/node" + b;
            AccessControlManager acMgr = session.getAccessControlManager();
            JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(session, path);
            if (acl.isEmpty()) {
                Privilege[] privileges = new Privilege[] { acMgr.privilegeFromName(Privilege.JCR_READ), acMgr.privilegeFromName(Privilege.JCR_READ_ACCESS_CONTROL) };
                if (acl.addAccessControlEntry(EveryonePrincipal.getInstance(), privileges)) {
                    acMgr.setPolicy(path, acl);
                }
            } else {
                for (AccessControlEntry ace : acl.getAccessControlEntries()) {
                    acl.removeAccessControlEntry(ace);
                }
                acMgr.setPolicy(path, acl);
            }
            session.save();
        }
    } catch (InvalidItemStateException e) {
        System.out.printf("error: %s%n", e);
    // ignore
    } finally {
        if (session != null) {
            session.logout();
        }
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) InvalidItemStateException(javax.jcr.InvalidItemStateException) AccessControlEntry(javax.jcr.security.AccessControlEntry) Privilege(javax.jcr.security.Privilege) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) Session(javax.jcr.Session)

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