📄 nodetest.java
字号:
/* * 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;import org.apache.jackrabbit.test.AbstractJCRTest;import org.apache.jackrabbit.test.NotExecutableException;import javax.jcr.nodetype.ConstraintViolationException;import javax.jcr.Session;import javax.jcr.Node;import javax.jcr.RepositoryException;import javax.jcr.NoSuchWorkspaceException;import javax.jcr.ItemNotFoundException;import javax.jcr.InvalidItemStateException;import javax.jcr.ItemExistsException;import javax.jcr.PathNotFoundException;import javax.jcr.Repository;import javax.jcr.Value;import javax.jcr.PropertyType;import javax.jcr.lock.LockException;/** * <code>NodeTest</code> contains all test cases for the * <code>javax.jcr.Node</code> that are related to writing, modifing or deleting * nodes (level 2 of the specification). * * @test * @sources NodeTest.java * @executeClass org.apache.jackrabbit.test.api.NodeTest * @keywords level2 */public class NodeTest extends AbstractJCRTest { private Session superuserW2; /** * to be able to test the update(String) and getCorrespondingNodePath(String) * methods we need an addtional workspace */ public void setUp() throws Exception { super.setUp(); // login to second workspace superuserW2 = helper.getSuperuserSession(workspaceName); } /** * remove all nodes in second workspace and log out */ public void tearDown() throws Exception { try { cleanUpTestRoot(superuserW2); } catch (RepositoryException e) { log.println("Exception in tearDown: " + e.toString()); } finally { // log out superuserW2.logout(); } super.tearDown(); } /** * Calls {@link javax.jcr.Node#getCorrespondingNodePath(String )} with a non * existing workspace. <br/><br/> This should throw an {@link * javax.jcr.NoSuchWorkspaceException }. */ public void testGetCorrespondingNodePathNoSuchWorkspaceException() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create testNode in default workspace Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); // save changes superuser.save(); try { defaultTestNode.getCorrespondingNodePath(getNonExistingWorkspaceName(superuser)); fail("Calling Node.getCorrespondingNodePath(workspace) with invalid workspace should throw NoSuchWorkspaceException"); } catch (NoSuchWorkspaceException e) { // ok, works as expected } } /** * Calls {@link javax.jcr.Node#getCorrespondingNodePath(String)} on a node * that has no corresponding node in second workspace */ public void testGetCorrespondingNodePathItemNotFoundException() throws RepositoryException, NotExecutableException { // make sure the repository supports multiple workspaces super.ensureMultipleWorkspacesSupported(); // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create testNode in default workspace Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); // save changes superuser.save(); try { // call the update method on test node in default workspace defaultTestNode.getCorrespondingNodePath(workspaceName); fail("Calling Node.getCorrespondingNodePath() on node that has no correspondend node should throw ItemNotFoundException"); } catch (ItemNotFoundException e) { // ok, works as expected } } /** * Creates a node with same path in both workspaces to check if {@link * javax.jcr.Node#getCorrespondingNodePath(String)} works properly. */ public void testGetCorrespondingNodePath() throws RepositoryException, NotExecutableException { // make sure the repository supports multiple workspaces super.ensureMultipleWorkspacesSupported(); // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create test node in default workspace Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); // save changes superuser.save(); // get the root node in the second workspace Node rootNodeW2 = (Node) superuserW2.getItem(testRootNode.getPath()); // create test node in second workspace rootNodeW2.addNode(nodeName1, testNodeType); // save changes superuserW2.save(); // call the update method on test node in default workspace defaultTestNode.getCorrespondingNodePath(workspaceName); // ok, works as expected } /** * Tries calling {@link javax.jcr.Node#update(String)} after node has * changed in first workspace but not been saved yet. <br/><br/> This should * throw and {@link javax.jcr.InvalidItemStateException}. <br/><br/> * Prerequisites: <ul> <li><code>javax.jcr.tck.propertyname1</code> name of * a String property that can be modified in <code>javax.jcr.tck.nodetype</code> * for testing</li> </ul> */ public void testUpdateInvalidItemStateException() throws RepositoryException, NotExecutableException { // make sure the repository supports multiple workspaces super.ensureMultipleWorkspacesSupported(); // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a test node in default workspace Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); // save changes superuser.save(); // get the root node in the second workspace Node rootNodeW2 = (Node) superuserW2.getItem(testRootNode.getPath()); // create test node in second workspace rootNodeW2.addNode(nodeName1); // save changes superuserW2.save(); // modify the node testNode.setProperty(propertyName1, "test"); try { // try calling update testNode.update(workspaceName); fail("Calling Node.update() on modified node should throw InvalidItemStateException"); } catch (InvalidItemStateException e) { // ok , works as expected } } /** * Tries to use {@link javax.jcr.Node#update(String)} with an invalid * workspace. <br/><br/> This should throw an {@link * javax.jcr.NoSuchWorkspaceException}. */ public void testUpdateNoSuchWorkspaceException() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a test node in default workspace Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); // save changes superuser.save(); try { testNode.update(getNonExistingWorkspaceName(superuser)); fail("Calling Node.update() on a non existing workspace should throw NoSuchWorkspaceException"); } catch (NoSuchWorkspaceException e) { // ok, works as expected } } /** * Calls {@link javax.jcr.Node#update(String)} for a node that only exists * in current workspace. <br><br> In that case nothing should happen. * <br/><br/>Prerequisites: <ul> <li><code>javax.jcr.tck.propertyname1</code> * name of a String property that can be modified in * <code>javax.jcr.tck.nodetype</code> for testing</li> </ul> */ public void testUpdateNoClone() throws RepositoryException { // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create a test node in default workspace Node testNode = defaultRootNode.addNode(nodeName1, testNodeType); // modify the node testNode.setProperty(propertyName1, "test"); superuser.save(); // call the update method on test node in default workspace testNode.update(workspaceName); // check if property is still there assertTrue("Node got property removed after Node.update() eventhough node has no clone", testNode.hasProperty(propertyName1)); // check if node did not get childs suddenly assertFalse("Node has children assigned after Node.update() eventhough node has no clone", testNode.hasNodes()); } /** * Checks if {@link javax.jcr.Node#update(String)} works properly by * creating the same node in two workspaces one with a child node the other * with a property set. <br/><br/> Calling <code>update()</code> on the node * with properties, should remove the properties and add the child node. * <br/><br/>Prerequisites: <ul> <li><code>javax.jcr.tck.nodetype</code> * must allow children of same nodetype. <li><code>javax.jcr.tck.propertyname1</code> * name of a String property that can be modified in * <code>javax.jcr.tck.nodetype</code> for testing</li> </ul> */ public void testUpdate() throws RepositoryException, NotExecutableException { // make sure the repository supports multiple workspaces super.ensureMultipleWorkspacesSupported(); // get default workspace test root node using superuser session Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath()); // create test node in default workspace Node defaultTestNode = defaultRootNode.addNode(nodeName1, testNodeType); defaultTestNode.setProperty(propertyName1, "test"); // save changes superuser.save(); // get the root node in the second workspace Node rootNodeW2 = (Node) superuserW2.getItem(testRootNode.getPath());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -