📄 nodetest.java
字号:
Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); // save the nodes defaultRootNode.save(); // remove them defaultTestNode.remove(); defaultRootNode.save(); // check if the node has been properly removed try { defaultRootNode.getNode(nodeName1); fail("Permanently removed node should no longer be adressable using Parent Node's getNode() method"); } catch (PathNotFoundException e) { // ok , works as expected } } /** * Removes a node using {@link javax.jcr.Node#remove()}, then saves using * {@link javax.jcr.Session#save()} method. */ public void testRemoveNodeSessionSave() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create the node Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); // save the nodes superuser.save(); // remove them defaultTestNode.remove(); superuser.save(); // check if the node has been properly removed try { superuser.getItem(defaultRootNode.getPath() + "/" + nodeName1); fail("Permanently removed node should no longer be adressable using Session.getItem()"); } catch (PathNotFoundException e) { // ok, works as expected } } /** * Tests if <code>Node.remove()</code> does not throw a * <code>LockException</code> if <code>Node</code> is locked. * <p/> * The test creates a node <code>nodeName1</code> of type * <code>testNodeType</code> under <code>testRoot</code> and locks the node * with the superuser session. Then the test removes * <code>nodeName1</code>. */ public void testRemoveNodeLockedItself() throws LockException, NotExecutableException, RepositoryException { Session session = testRootNode.getSession(); if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) { throw new NotExecutableException("Locking is not supported."); } // create a node that is lockable Node node = testRootNode.addNode(nodeName1, testNodeType); // or try to make it lockable if it is not if (!node.isNodeType(mixLockable)) { if (node.canAddMixin(mixLockable)) { node.addMixin(mixLockable); } else { throw new NotExecutableException("Node " + nodeName1 + " is not lockable and does not " + "allow to add mix:lockable"); } } testRootNode.save(); // remove first slash of path to get rel path to root String pathRelToRoot = node.getPath().substring(1); // access node through another session to lock it Session session2 = helper.getSuperuserSession(); try { Node node2 = session2.getRootNode().getNode(pathRelToRoot); node2.lock(true, true); // test fails if a LockException is thrown when removing the node // (remove must be possible since the parent is not locked) node.remove(); } finally { session2.logout(); } } /** * Tests if <code>Node.remove()</code> throws a <code>LockException</code> * if the parent node of <code>Node</code> is locked. * <p/> * The test creates a node <code>nodeName1</code> of type * <code>testNodeType</code> under <code>testRoot</code>, adds a child node * <code>nodeName2</code> and locks it with the superuser session. Then the * test tries to remove the <code>nodeName2</code>. */ public void testRemoveNodeParentLocked() throws LockException, NotExecutableException, RepositoryException { Session session = testRootNode.getSession(); if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) { throw new NotExecutableException("Locking is not supported."); } // create a node that is lockable Node node = testRootNode.addNode(nodeName1, testNodeType); // or try to make it lockable if it is not if (!node.isNodeType(mixLockable)) { if (node.canAddMixin(mixLockable)) { node.addMixin(mixLockable); } else { throw new NotExecutableException("Node " + nodeName1 + " is not lockable and does not " + "allow to add mix:lockable"); } } // create a child node Node subNode = node.addNode(nodeName2, testNodeType); testRootNode.save(); // lock the node // remove first slash of path to get rel path to root String pathRelToRoot = node.getPath().substring(1); // access node through another session to lock it Session session2 = helper.getSuperuserSession(); try { Node node2 = session2.getRootNode().getNode(pathRelToRoot); node2.lock(true, true); try { subNode.remove(); session.save(); fail("Removal of a Node must throw a LockException upon remove() " + "or upon save() if the parent of the node is locked"); } catch (LockException e) { // success } // unlock to remove node at tearDown() node2.unlock(); } finally { session2.logout(); } } /** * Tests object identity, meaning two nodes objects accuired through the * same session must have the same properties and states. <br/><br/> * Prerequisites: <ul> <li><code>javax.jcr.tck.nodetype</code> must allow * children of same node type</li> <li><code>javax.jcr.tck.propertyname1</code> * name of a String property that can be set in <code>javax.jcr.tck.nodetype</code> * for testing</li> </ul> */ public void testNodeIdentity() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a node Node testNode1 = defaultRootNode.addNode(nodeName1, testNodeType); // add a child node testNode1.addNode(nodeName1, testNodeType); // add a property testNode1.setProperty(propertyName1, "test"); // save the new node defaultRootNode.save(); // acquire the same node with session 2 Node testNode2 = (Node) superuser.getItem(testNode1.getPath()); // check if they have the same property assertTrue("Two references of same node have different properties", testNode1.getProperty(propertyName1).isSame(testNode2.getProperty(propertyName1))); // check if they have the same child assertTrue("Two references of same node have different children", testNode1.getNode(nodeName1).isSame(testNode2.getNode(nodeName1))); // check state methods assertEquals("Two references of same node have different State for Node.isCheckedOut()", testNode1.isCheckedOut(), testNode2.isCheckedOut()); assertEquals("Two references of same node have different State for Node.isLocked()", testNode1.isLocked(), testNode2.isLocked()); assertEquals("Two references of same node have different State for Node.isModified()", testNode1.isModified(), testNode2.isModified()); assertEquals("Two references of same node have different State for Node.isNew()", testNode1.isNew(), testNode2.isNew()); assertEquals("Two references of same node have different State for Node.isNode()", testNode1.isNode(), testNode2.isNode()); assertEquals("Two references of same node have different State for Node.isNodeType()", testNode1.isNodeType(testNodeType), testNode2.isNodeType(testNodeType)); assertTrue("Two references of same node should return true for Node1.isSame(Node2)", testNode1.isSame(testNode2)); assertEquals("Two references of same node have different Definitions", testNode1.getDefinition().getName(), testNode2.getDefinition().getName()); } /** * Tests if <code>Item.isSame(Item otherItem)</code> will return true when * two <code>Node</code> objects representing the same actual repository * item have been retrieved through two different sessions and one has been * modified. */ public void testIsSameMustNotCompareStates() throws RepositoryException { // create a node and save it Node testNode1 = testRootNode.addNode(nodeName1, testNodeType); testRootNode.save(); // accuire the same node with a different session Session session = helper.getReadOnlySession(); try { Node testNode2 = (Node) session.getItem(testNode1.getPath()); // add a property and do not save it so property is different in testNode2 testNode1.setProperty(propertyName1, "value1"); assertTrue("Two references of same node should return true for Node1.isSame(Node2)", testNode1.isSame(testNode2)); } finally { session.logout(); } } /** * Checks if {@link Node#isModified()} works correcty for unmodified and * modified nodes. */ public void testIsModified() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a node Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); defaultRootNode.save(); assertFalse("Unmodified node should return false on Node.isModified()", testNode.isModified()); // check if modified properties are recognised testNode.setProperty(propertyName1, "test"); assertTrue("Modified node should return true on Node.isModified()", testNode.isModified()); defaultRootNode.save(); // check if modified child nodes are recognised testNode.addNode(nodeName2, testNodeType); assertTrue("Modified node should return true on Node.isModified()", testNode.isModified()); } /** * Checks if {@link Node#isNew()} works correctly for new and existing, * unmodified nodes. */ public void testIsNew() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a node Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); assertTrue("Newly created node should return true on newNode.isNew()", testNode.isNew()); defaultRootNode.save(); assertFalse("Unmodified, exisiting node should return false on newNode.isNew()", testNode.isNew()); } /** * Tries to call {@link Node#refresh(boolean)} on a deleted node. * <br/><br/> This should throw an {@link InvalidItemStateException}. */ public void testRefreshInvalidItemStateException() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a node Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); // save the new node defaultRootNode.save(); // remove the node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -