locktest.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 881 行 · 第 1/2 页
JAVA
881 行
n2.unlock(); } /** * Test locks are released when session logs out */ public void testLogout() throws Exception { // add node Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // create new session Session otherSuperuser = helper.getSuperuserSession(); Lock lock; try { // get node created above Node n2 = (Node) otherSuperuser.getItem(n1.getPath()); // lock node lock = n2.lock(false, true); // assert: lock must be alive assertTrue("lock must be alive", lock.isLive()); // assert: node must be locked assertTrue("node must be locked", n1.isLocked()); } finally { // log out otherSuperuser.logout(); } // assert: lock must not be alive assertFalse("lock must not be alive", lock.isLive()); // assert: node must not be locked assertFalse("node must not be locked", n1.isLocked()); } /** * Test locks may be transferred to other session */ public void testLockTransfer() throws Exception { // add node Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // create new session Session otherSuperuser = helper.getSuperuserSession(); try { // get node created above Node n2 = (Node) otherSuperuser.getItem(n1.getPath()); // lock node Lock lock = n2.lock(false, true); // assert: user must get non-null token assertNotNull("user must get non-null token", lock.getLockToken()); // transfer to standard session String lockToken = lock.getLockToken(); otherSuperuser.removeLockToken(lockToken); superuser.addLockToken(lockToken); // assert: user must get null token assertNull("user must get null token", lock.getLockToken()); // assert: user must get non-null token assertNotNull("user must get non-null token", n1.getLock().getLockToken()); } finally { // log out otherSuperuser.logout(); } } /** * Test open-scoped locks */ public void testOpenScopedLocks() throws Exception { // add node Node n1 = testRootNode.addNode(nodeName1, testNodeType); try { n1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // create new session Session otherSuperuser = helper.getSuperuserSession(); try { // get node created above Node n2 = (Node) otherSuperuser.getItem(n1.getPath()); // lock node Lock lock = n2.lock(false, false); // transfer to standard session String lockToken = lock.getLockToken(); otherSuperuser.removeLockToken(lockToken); superuser.addLockToken(lockToken); } finally { // log out otherSuperuser.logout(); } // assert: node still locked assertTrue(n1.isLocked()); } /** * Test refresh */ public void testRefresh() 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: lock must be alive assertTrue("lock must be alive", lock.isLive()); // assert: refresh must succeed lock.refresh(); // unlock node n.unlock(); // assert: lock must not be alive assertFalse("lock must not be alive", lock.isLive()); } /** * Test refresh */ public void testRefreshNotLive() 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: lock must be alive assertTrue("lock must be alive", lock.isLive()); // unlock node n.unlock(); // assert: lock must not be alive assertFalse("lock must not be alive", lock.isLive()); // refresh try { lock.refresh(); fail("Refresh on a lock that is not alive must fail"); } catch (LockException e) { // success } } /** * Test getLock */ public void testGetLock() 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(); // deep lock parent node n1.lock(true, true); // get lock on child node Lock lock = n2.getLock(); // lock holding node must be parent assertTrue("lock holding node must be parent", lock.getNode().isSame(n1)); } /** * Tests if a locked, checked-in node can be unlocked */ public void testCheckedInUnlock() throws Exception { if (!isSupported(Repository.OPTION_VERSIONING_SUPPORTED)) { throw new NotExecutableException("Repository does not support versioning."); } // set up versionable and lockable node Node testNode = testRootNode.addNode(nodeName1, testNodeType); try { testNode.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testNode.addMixin(mixVersionable); testRootNode.save(); // lock and check-in try { testNode.lock(false, false); } catch (RepositoryException ex) { // repository may not allow shallow locks on this resource // retry with a deep lock testNode.lock(true, false); } testNode.save(); testNode.checkin(); // do the unlock testNode.unlock(); assertFalse("Could not unlock a locked, checked-in node", testNode.holdsLock()); } /** * Tests if locks are maintained when child nodes are reordered */ public void testReorder() throws Exception { Node testNode = setUpSameNameSiblings(); // lock last node (3) testNode.lock(false, true); // assert: last node locked assertTrue("Third child node locked", testRootNode.getNode(nodeName1 + "[3]").isLocked()); // move last node in front of first testRootNode.orderBefore(nodeName1 + "[3]", nodeName1 + "[1]"); testRootNode.save(); // assert: first node locked assertTrue("First child node locked", testRootNode.getNode(nodeName1 + "[1]").isLocked()); } /** * Tests if locks are maintained when child nodes are reordered */ public void testReorder2() throws Exception { setUpSameNameSiblings(); // lock first node (1) testRootNode.getNode(nodeName1 + "[1]").lock(false, true); // assert: first node locked assertTrue("First child node locked", testRootNode.getNode(nodeName1 + "[1]").isLocked()); // move first node to last testRootNode.orderBefore(nodeName1 + "[1]", null); testRootNode.save(); // assert: third node locked assertTrue("Third child node locked", testRootNode.getNode(nodeName1 + "[3]").isLocked()); } /** * Tests if move preserves lock state (JIRA issue JCR-207). A node that has * been locked must still appear locked when it has been moved or renamed, * regardless whether the changes have already been made persistent. */ public void testMoveLocked() throws Exception { Session session = testRootNode.getSession(); // create two nodes, parent and child Node testNode1 = testRootNode.addNode(nodeName1, testNodeType); try { testNode1.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } Node testNode2 = testNode1.addNode(nodeName2, testNodeType); try { testNode2.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); // lock child node testNode2.lock(false, true); // assert: child node locked assertTrue("Child node locked", testNode2.isLocked()); // move child node up String newPath = testRootNode.getPath() + "/" + testNode2.getName(); session.move(testNode2.getPath(), newPath); // assert: child node locked, before save assertTrue("Child node locked before save", testNode2.isLocked()); session.save(); // assert: child node locked, after save assertTrue("Child node locked after save", testNode2.isLocked()); } /** * Tests if unlocking the first of two locked same-name sibling nodes does * not unlock the second (JIRA issue JCR-284). */ public void testUnlockSameNameSibling() throws RepositoryException, NotExecutableException { Session session = testRootNode.getSession(); Node n1, n2; try { // create two same-name sibling nodes n1 = testRootNode.addNode(nodeName1, testNodeType); n2 = testRootNode.addNode(nodeName1, testNodeType); session.save(); } catch (ItemExistsException ex) { throw new NotExecutableException("Node does not seem to allow same name siblings"); } n1.addMixin("mix:lockable"); n2.addMixin("mix:lockable"); session.save(); // lock both nodes n1.lock(true, true); n2.lock(true, true); // assert: both nodes are locked assertTrue("First node locked: ", n1.isLocked()); assertTrue("Second node locked: ", n2.isLocked()); // unlock first sibling n1.unlock(); // assert: first node unlocked, second node still locked assertFalse("First node unlocked: ", n1.isLocked()); assertTrue("Second node locked: ", n2.isLocked()); } /** * Return a flag indicating whether the indicated session contains * a specific lock token */ private boolean containsLockToken(Session session, String lockToken) { String[] lt = session.getLockTokens(); for (int i = 0; i < lt.length; i++) { if (lt[i].equals(lockToken)) { return true; } } return false; } /** * Create three child nodes with identical names */ private Node setUpSameNameSiblings() throws RepositoryException, NotExecutableException { // create three lockable nodes with same name Node testNode = testRootNode.addNode(nodeName1, testNodeType); try { testNode.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } try { testNode = testRootNode.addNode(nodeName1, testNodeType); } catch (ItemExistsException ex) { // repository does not seem to support same name siblings on this node type throw new NotExecutableException("Node type " + testNodeType + " does not support same-name-siblings"); } try { testNode.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testNode = testRootNode.addNode(nodeName1, testNodeType); try { testNode.addMixin(mixLockable); } catch (ConstraintViolationException ex) { // may already be lockable, just proceed } testRootNode.save(); return testNode; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?