locktest.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 881 行 · 第 1/2 页
JAVA
881 行
/* * 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.test.api.lock;import javax.jcr.ItemExistsException;import javax.jcr.Node;import javax.jcr.Repository;import javax.jcr.RepositoryException;import javax.jcr.Session;import javax.jcr.lock.Lock;import javax.jcr.lock.LockException;import javax.jcr.nodetype.ConstraintViolationException;import org.apache.jackrabbit.test.AbstractJCRTest;import org.apache.jackrabbit.test.NotExecutableException;/** * <code>LockTest</code> contains the test cases for the lock support in * the JCR specification. * * @test * @sources LockTest.java * @executeClass org.apache.jackrabbit.test.api.lock.LockTest * @keywords locking * * @tck.config testroot must allow child nodes of type <code>nodetype</code> * @tck.config nodetype nodetype which is lockable or allows to add mix:lockable. * The node must also allow child nodes with the same node type as itself. * @tck.config nodename1 name of a lockable child node of type <code>nodetype</code>. */public class LockTest extends AbstractJCRTest { /** * Test lock token functionality */ public void testAddRemoveLockToken() throws Exception { // create new node Node n = testRootNode.addNode(nodeName1, testNodeType); try { n.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node and get lock token Lock lock = n.lock(false, true); // assert: session must get a non-null lock token assertNotNull("session must get a non-null lock token", lock.getLockToken()); // assert: session must hold lock token assertTrue("session must hold lock token", containsLockToken(superuser, lock.getLockToken())); // remove lock token String lockToken = lock.getLockToken(); superuser.removeLockToken(lockToken); // assert: session must get a null lock token assertNull("session must get a null lock token", lock.getLockToken()); // assert: session must still hold lock token assertFalse("session must not hold lock token", containsLockToken(superuser, lockToken)); // assert: session unable to modify node try { n.addNode(nodeName2, testNodeType); fail("session unable to modify node"); } catch (LockException e) { // expected } // add lock token superuser.addLockToken(lockToken); // assert: session must get a non-null lock token assertNotNull("session must get a non-null lock token", lock.getLockToken()); // assert: session must hold lock token assertTrue("session must hold lock token", containsLockToken(superuser, lock.getLockToken())); // assert: session able to modify node n.addNode(nodeName2, testNodeType); } /** * Test session scope: other session may not access nodes that are * locked. */ public void testNodeLocked() throws Exception { // create new node and lock it Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node Lock lock = n1.lock(false, true); // assert: isLive must return true assertTrue("Lock must be live", lock.isLive()); // create new session Session otherSuperuser = helper.getSuperuserSession(); try { // get same node Node n2 = (Node) otherSuperuser.getItem(n1.getPath()); // assert: lock token must be null for other session assertNull("Lock token must be null for other session", n2.getLock().getLockToken()); // assert: modifying same node in other session must fail try { n2.addNode(nodeName2, testNodeType); fail("modifying same node in other session must fail"); } catch (LockException e) { // expected } } finally { otherSuperuser.logout(); } } /** * Test to get the lock holding node of a node */ public void testGetNode() throws Exception { // create new node with a sub node and lock it Node n1 = testRootNode.addNode(nodeName1, testNodeType); Node n1Sub = n1.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); n1Sub.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node n1.lock(true, true); assertEquals("getNode() must return the lock holder", n1.getPath(), n1.getLock().getNode().getPath()); assertEquals("getNode() must return the lock holder", n1.getPath(), n1Sub.getLock().getNode().getPath()); n1.unlock(); } /** * Test if getLockOwner() returns the same value as returned by * Session.getUserId at the time that the lock was placed */ public void testGetLockOwnerProperty() throws Exception { // create new node and lock it Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node Lock lock = n1.lock(false, true); if (n1.getSession().getUserID() == null) { assertFalse("jcr:lockOwner must not exist if Session.getUserId() returns null", n1.hasProperty(jcrLockOwner)); } else { assertEquals("getLockOwner() must return the same value as stored " + "in property " + jcrLockOwner + " of the lock holding " + "node", n1.getProperty(jcrLockOwner).getString(), lock.getLockOwner()); } n1.unlock(); } /** * Test if getLockOwner() returns the same value as returned by * Session.getUserId at the time that the lock was placed */ public void testGetLockOwner() throws Exception { // create new node and lock it Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node Lock lock = n1.lock(false, true); assertEquals("getLockOwner() must return the same value as returned " + "by Session.getUserId at the time that the lock was placed", testRootNode.getSession().getUserID(), lock.getLockOwner()); n1.unlock(); } /** * Test if a shallow lock does not lock the child nodes of the locked node. */ public void testShallowLock() throws Exception { // create new nodes Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node n2 = n1.addNode(nodeName2, testNodeType); testRootNode.save(); // lock parent node n1.lock(false, true); assertFalse("Shallow lock must not lock the child nodes of a node.", n2.isLocked()); } /** * Test if it is possible to lock and unlock a checked-in node. */ public void testCheckedIn() throws NotExecutableException, RepositoryException { if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) { throw new NotExecutableException("Versioning is not supported."); } // create a node that is lockable and versionable Node node = testRootNode.addNode(nodeName1, testNodeType); try { node.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } // try to make it versionable if it is not if (!node.isNodeType(mixVersionable)) { if (node.canAddMixin(mixVersionable)) { node.addMixin(mixVersionable); } else { throw new NotExecutableException("Node " + nodeName1 + " is " + "not versionable and does not allow to add " + "mix:versionable"); } } testRootNode.save(); node.checkin(); try { node.lock(false, false); } catch (RepositoryException ex) { // repository may not allow shallow locks on this resource // retry with a deep lock node.lock(true, false); } assertTrue("Locking of a checked-in node failed.", node.isLocked()); node.unlock(); assertFalse("Unlocking of a checked-in node failed.", node.isLocked()); } /** * Test parent/child lock */ public void testParentChildLock() throws Exception { // create new nodes Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node n2 = n1.addNode(nodeName2, testNodeType); try { n2.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock parent node n1.lock(false, true); // lock child node n2.lock(false, true); // unlock parent node n1.unlock(); // child node must still hold lock assertTrue("child node must still hold lock", n2.holdsLock()); } /** * Test parent/child lock */ public void testParentChildDeepLock() throws Exception { // create new nodes Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node n2 = n1.addNode(nodeName2, testNodeType); try { n2.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock child node n2.lock(false, true); // assert: unable to deep lock parent node try { n1.lock(true, true); fail("unable to deep lock parent node"); } catch (LockException e) { // expected } } /** * Test Lock.isDeep() */ public void testIsDeep() throws RepositoryException { // create two lockable nodes Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node n2 = testRootNode.addNode(nodeName2, testNodeType); try { n2.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node 1 "undeeply" Lock lock1 = n1.lock(false, true); assertFalse("Lock.isDeep() must be false if the lock has not been set " + "as not deep", lock1.isDeep()); // lock node 2 "deeply" Lock lock2 = n2.lock(true, true); assertTrue("Lock.isDeep() must be true if the lock has been set " + "as deep", lock2.isDeep()); } /** * Test Lock.isSessionScoped() */ public void testIsSessionScoped() throws RepositoryException { // create two lockable nodes Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node n2 = testRootNode.addNode(nodeName2, testNodeType); try { n2.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock node 1 session-scoped Lock lock1 = n1.lock(false, true); assertTrue("Lock.isSessionScoped() must be true if the lock " + "is session-scoped", lock1.isSessionScoped()); // lock node 2 open-scoped Lock lock2 = n2.lock(false, false); assertFalse("Lock.isSessionScoped() must be false if the lock " + "is open-scoped", lock2.isSessionScoped());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?