📄 nodereadmethodstest.java
字号:
if (node == null) { throw new NotExecutableException("Workspace does not contain a node with a reference property set"); } PropertyIterator properties = node.getProperties(); while (properties.hasNext()) { Property p = properties.nextProperty(); if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) { Node referencedNode = p.getNode(); PropertyIterator refs = referencedNode.getReferences(); boolean referenceFound = false; while (refs.hasNext()) { Property ref = refs.nextProperty(); if (ref.isSame(p)) { referenceFound = true; } } assertTrue("Correct reference not found", referenceFound); } } } /** * Test if getUUID() returns the string value of the property "jcr:uuid". * Therefor a node of type "mix:referenceable" is located recursively in the * entire repository. A NotExecutableException is thrown when no node of * this type is found. * * @throws NotExecutableException * @throws RepositoryException */ public void testGetUUID() throws NotExecutableException, RepositoryException { // find a node of type mix:referenceable Node node = locateReferenceableNode(testRootNode); if (node == null) { throw new NotExecutableException("Workspace does not contain a referencable node"); } try { assertEquals("node.getUUID() does not match " + "node.getProperty(\"jcr:uuid\").getString()", node.getProperty(jcrUUID).getString(), node.getUUID()); } catch (PathNotFoundException e) { fail("Property UUID expected for " + "node of type \"" + mixReferenceable + "\""); } } /** * Test if getUUID() throws a UnsupportedRepositoryOperationException if * Node is not referenceable */ public void testGetUUIDOfNonReferenceableNode() throws NotExecutableException, RepositoryException { // find a node NOT of type mix:referenceable Node node = locateNonReferenceableNode(testRootNode); if (node == null) { throw new NotExecutableException("Workspace does not contain a non referenceable node"); } try { node.getUUID(); fail("UnsupportedRepositoryOperationException expected"); } catch (UnsupportedRepositoryOperationException e) { // success } } /** * Test if hasNode(String relPath) returns true if the required node exists * and false if it doesn't. Tested node: root */ public void testHasNode() throws NotExecutableException, RepositoryException { Node node = testRootNode; NodeIterator nodes = node.getNodes(); StringBuffer notExistingNodeName = new StringBuffer(); while (nodes.hasNext()) { Node n = nodes.nextNode(); assertTrue("hasNode(String relPath) returns false although " + "node at relPath is existing", node.hasNode(n.getName())); notExistingNodeName.append(n.getName() + "X"); } if (notExistingNodeName.toString().equals("")) { throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node."); } assertFalse("hasNode(String relPath) returns true although " + "node at relPath is not existing", node.hasNode(notExistingNodeName.toString().replaceAll(":", ""))); } /** * Test if hasNodes() returns true if any sub node exists or false if not. * Tested node: root */ public void testHasNodes() throws RepositoryException { Node node = testRootNode; NodeIterator nodes = node.getNodes(); int i = 0; while (nodes.hasNext()) { nodes.nextNode(); i++; } if (i == 0) { assertFalse("node.hasNodes() returns true although " + "no sub nodes existing", node.hasNodes()); } else { assertTrue("node.hasNodes() returns false althuogh " + "sub nodes are existing", node.hasNodes()); } } /** * Test if hasProperty(String relPath) returns true if a required property * exists and false if it doesn't. Tested node: root */ public void testHasProperty() throws NotExecutableException, RepositoryException { Node node = testRootNode; PropertyIterator properties = node.getProperties(); StringBuffer notExistingPropertyName = new StringBuffer(); while (properties.hasNext()) { Property p = properties.nextProperty(); assertTrue("node.hasProperty(\"relPath\") returns false " + "although property at relPath is existing", node.hasProperty(p.getName())); notExistingPropertyName.append(p.getName() + "X"); } if (notExistingPropertyName.toString().equals("")) { fail("Root node must at least have one property: jcr:primaryType"); } assertFalse("node.hasProperty(\"relPath\") returns true " + "although property at relPath is not existing", node.hasProperty(notExistingPropertyName.toString().replaceAll(":", ""))); } /** * Test if hasProperty() returns true if any property exists or false if * not. Tested node: root * * @throws RepositoryException */ public void testHasProperties() throws RepositoryException { Node node = testRootNode; PropertyIterator properties = node.getProperties(); int i = 0; while (properties.hasNext()) { Property p = properties.nextProperty(); log.println(p.getName()); i++; } if (i == 0) { assertFalse("Must return false when no properties exist", node.hasProperties()); } else { assertTrue("Must return true when one or more properties exist", node.hasProperties()); } } //-----------------------< internal >--------------------------------------- /** * Returns the first descendant of <code>node</code> which is of type * mix:referencable. * * @param node <code>Node</code> to start traversal. * @return first node of type mix:referenceable */ private Node locateReferenceableNode(Node node) throws RepositoryException { if (node.isNodeType(mixReferenceable)) { return node; } NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node returnedNode = locateReferenceableNode(nodes.nextNode()); if (returnedNode != null) { return returnedNode; } } return null; } /** * Returns the first descendant of <code>node</code> which is not of type * mix:referenceable. * * @param node <code>Node</code> to start traversal. * @return first node which is not of type mix:referenceable */ private Node locateNonReferenceableNode(Node node) throws RepositoryException { if (!node.isNodeType(mixReferenceable)) { return node; } NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node returnedNode = locateNonReferenceableNode(nodes.nextNode()); if (returnedNode != null) { return returnedNode; } } return null; } /** * Returns the first descendant of <code>node</code> which has a property of * type {@link javax.jcr.PropertyType#REFERENCE} set and is <b>not</b> * multivalued. * * @param node <code>Node</code> to start traversal. * @return first node with a property of PropertType.REFERENCE */ private Node locateNodeWithReference(Node node) throws RepositoryException { PropertyIterator properties = node.getProperties(); while (properties.hasNext()) { Property p = properties.nextProperty(); if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) { return node; } } NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node returnedNode = locateNodeWithReference(nodes.nextNode()); if (returnedNode != null) { return returnedNode; } } return null; } /** * Returns the first descendant of <code>node</code> which defines a primary * item. * * @param node <code>Node</code> to start traversal. * @return first node with a primary item */ private Node locateNodeWithPrimaryItem(Node node) throws RepositoryException { if (node.getPrimaryNodeType().getPrimaryItemName() != null) { return node; } NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node returnedNode = locateNodeWithPrimaryItem(nodes.nextNode()); if (returnedNode != null) { return returnedNode; } } return null; } /** * Returns the first descendant of <code>node</code> which does not define a * primary item. * * @param node <code>Node</code> to start traversal. * @return first node without a primary item */ private Node locateNodeWithoutPrimaryItem(Node node) throws RepositoryException { if (node.getPrimaryNodeType().getPrimaryItemName() == null) { return node; } NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node n = locateNodeWithoutPrimaryItem(nodes.nextNode()); if (n != null) { return n; } } return null; } /** * Returns the first descendant of <code>node</code> which has same name * siblings. * * @param node <code>Node</code> to start traversal. * @return first node with same name siblings */ private Node locateNodeWithSameNameSiblings(Node node) throws RepositoryException { NodeIterator nodes = node.getNodes(); while (nodes.hasNext()) { Node n = nodes.nextNode(); NodeIterator nodes2 = node.getNodes(n.getName()); int i = 0; while (nodes2.hasNext()) { nodes2.next(); i++; } if (i > 1) { // node has same name siblings return n; } else { Node returnedNode = locateNodeWithSameNameSiblings(n); if (returnedNode != null) { return returnedNode; } } } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -