📄 nodereadmethodstest.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.Session;import javax.jcr.Node;import javax.jcr.NodeIterator;import javax.jcr.RepositoryException;import javax.jcr.PathNotFoundException;import javax.jcr.ItemNotFoundException;import javax.jcr.Item;import javax.jcr.ItemVisitor;import javax.jcr.Property;import javax.jcr.PropertyIterator;import javax.jcr.PropertyType;import javax.jcr.UnsupportedRepositoryOperationException;import java.util.ArrayList;import java.util.NoSuchElementException;/** * Tests the 'read' methods specified in the {@link javax.jcr.Node} interface on * a level 1 repository. * <p/> * Most tests require at least one child node under the root node, otherwise a * {@link org.apache.jackrabbit.test.NotExecutableException} is thrown. * * @test * @sources NodeReadMethodsTest.java * @executeClass org.apache.jackrabbit.test.api.NodeReadMethodsTest * @keywords level1 */public class NodeReadMethodsTest extends AbstractJCRTest { /** * Session to access the workspace */ private Session session; /** * The first child node of the testRootNode */ private Node childNode; /** * Sets up the fixture for this test. */ protected void setUp() throws Exception { isReadOnly = true; super.setUp(); session = helper.getReadOnlySession(); testRootNode = session.getRootNode().getNode(testPath); NodeIterator nodes = testRootNode.getNodes(); try { childNode = nodes.nextNode(); } catch (NoSuchElementException e) { } } /** * Releases the session aquired in {@link #setUp()}. */ protected void tearDown() throws Exception { if (session != null) { session.logout(); } super.tearDown(); } // -----------< tests of methods inherited of Item >------------------------ /** * Tests if getPath() returns the correct path. */ public void testGetPath() throws NotExecutableException, RepositoryException { if (childNode == null) { throw new NotExecutableException("Workspace does not have sufficient content to run this test."); } String path = testRoot + "/" + childNode.getName(); if (getSize(testRootNode.getNodes(childNode.getName())) > 1) { // is a same-name-sibling, append index path += "[" + childNode.getIndex() + "]"; } log.println("path: " + childNode.getPath()); assertEquals("getPath returns wrong result", path, childNode.getPath()); } /** * Tests if getName() returns same as last name returned by getPath() */ public void testGetName() throws RepositoryException, NotExecutableException { if (childNode == null) { throw new NotExecutableException("Workspace does not have sufficient content to run this test."); } // build name from path String path = childNode.getPath(); String name = path.substring(path.lastIndexOf("/") + 1); if (name.indexOf("[") != -1) { name = name.substring(0, name.indexOf("[")); } assertEquals("getName() must be the same as the last item in the path", name, childNode.getName()); } /** * Test if the ancestor at depth = n, where n is the depth of this * <code>Item</code>, returns this <code>Node</code> itself. */ public void testGetAncestorOfNodeDepth() throws RepositoryException { Node nodeAtDepth = (Node) testRootNode.getAncestor(testRootNode.getDepth()); assertTrue("The ancestor of depth = n, where n is the depth of this " + "Node must be the item itself.", testRootNode.isSame(nodeAtDepth)); } /** * Test if getting the ancestor of depth = n, where n is greater than depth * of this <code>Node</code>, throws an <code>ItemNotFoundException</code>. */ public void testGetAncestorOfGreaterDepth() throws RepositoryException { try { int greaterDepth = testRootNode.getDepth() + 1; testRootNode.getAncestor(greaterDepth); fail("Getting ancestor of depth n, where n is greater than depth of" + "this Node must throw an ItemNotFoundException"); } catch (ItemNotFoundException e) { // success } } /** * Test if getting the ancestor of negative depth throws an * <code>ItemNotFoundException</code>. */ public void testGetAncestorOfNegativeDepth() throws RepositoryException { try { testRootNode.getAncestor(-1); fail("Getting ancestor of depth < 0 must throw an ItemNotFoundException."); } catch (ItemNotFoundException e) { // success } } /** * Tests if getParent() returns parent node */ public void testGetParent() throws NotExecutableException, RepositoryException { if (childNode == null) { throw new NotExecutableException("Workspace does not have sufficient content to run this test."); } assertTrue("getParent() of a child node return the parent node.", testRootNode.isSame(childNode.getParent())); } /** * Tests if getParent() of root throws an ItemNotFoundException */ public void testGetParentOfRoot() throws RepositoryException { try { session.getRootNode().getParent(); fail("getParent() of root must throw an ItemNotFoundException."); } catch (ItemNotFoundException e) { // success } } /** * Tests if depth of root is 0 and depth of a sub node of root is 1 */ public void testGetDepth() throws RepositoryException { assertEquals("getDepth() of root must be 0", 0, session.getRootNode().getDepth()); for (NodeIterator it = session.getRootNode().getNodes(); it.hasNext();) { assertEquals("getDepth() of child node of root must be 1", 1, it.nextNode().getDepth()); } } /** * Tests if getSession() is same as through which the Item was acquired */ public void testGetSession() throws RepositoryException { assertSame("getSession must return the Session through which " + "the Node was acquired.", testRootNode.getSession(), session); } /** * Tests if isNode() returns true */ public void testIsNode() { assertTrue("isNode() must return true.", testRootNode.isNode()); } /** * Tests if isSame() returns true when retrieving an item through different * sessions */ public void testIsSame() throws RepositoryException { // access same node through different session Session s = helper.getReadOnlySession(); try { Item otherTestNode = s.getRootNode().getNode(testPath); assertTrue("isSame(Item item) must return true for the same " + "item retrieved through different sessions.", testRootNode.isSame(otherTestNode)); } finally { s.logout(); } } /** * */ public void testAccept() throws RepositoryException { final Node n = testRootNode; ItemVisitor itemVisitor = new ItemVisitor() { public void visit(Property property) { fail("Wrong accept method executed."); } public void visit(Node node) throws RepositoryException { assertTrue("Visited node is not the same as the one passed in method visit(Node)", n.isSame(node)); } }; n.accept(itemVisitor); } // -----------< tests of Node specific methods >---------------------------- /** * Test if getNode(String relPath) returns the correct node and if a * PathNotFoundException is thrown when Node at relPath does not exist */ public void testGetNode() throws NotExecutableException, RepositoryException { StringBuffer notExistingPath = new StringBuffer("X"); NodeIterator nodes = testRootNode.getNodes(); while (nodes.hasNext()) { // build a path that for sure is not existing // (":" of namespace prefix will be replaced later on) notExistingPath.append(nodes.nextNode().getName()); } try { testRootNode.getNode(notExistingPath.toString().replaceAll(":", "")); fail("getNode(String relPath) must throw a PathNotFoundException" + "if no node exists at relPath"); } catch (PathNotFoundException e) { // success } try { NodeIterator nodes2 = testRootNode.getNodes(); Node node = nodes2.nextNode(); assertTrue("Node from Iterator is not the same as the Node from getNode()", testRootNode.getNode(node.getName()).isSame(node)); } catch (NoSuchElementException e) { throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node."); } } /** * Test if all returned items are of type node. */ public void testGetNodes() throws RepositoryException { NodeIterator nodes = testRootNode.getNodes(); while (nodes.hasNext()) { Item item = (Item) nodes.next(); assertTrue("Item is not a node", item.isNode()); } } /** * Test getNodes(String namePattern) with all possible patterns. Tested * node: root - NotExecutableException is thrown when root node has no sub * nodes. */ public void testGetNodesNamePattern() throws NotExecutableException, RepositoryException { // get root node and build an ArrayList of its sub nodes Node node = testRootNode; if (!node.hasNodes()) { throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node."); } NodeIterator allNodesIt = node.getNodes(); ArrayList allNodes = new ArrayList(); while (allNodesIt.hasNext()) { Node n = allNodesIt.nextNode(); allNodes.add(n); } // test if an empty NodeIterator is returned // when the pattern is not matching any child node String pattern0 = ""; NodeIterator nodes0 = node.getNodes(pattern0); try { nodes0.nextNode(); fail("An empty NodeIterator must be returned if pattern does" + "not match any child node."); } catch (NoSuchElementException e) { // success } // all further tests are using root's first sub node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -