📄 nodereadmethodstest.java
字号:
Node firstNode = (Node) allNodes.get(0); // test pattern "*" String pattern1 = "*"; String assertString1 = "node.getNodes(\"" + pattern1 + "\"): "; NodeIterator nodes1 = node.getNodes(pattern1); // test if the number of found nodes is correct assertEquals(assertString1 + "number of nodes found: ", allNodes.size(), getSize(nodes1)); // test pattern "nodeName" String pattern2 = firstNode.getName(); String assertString2 = "node.getNodes(\"" + pattern2 + "\"): "; // test if the names of the found nodes are matching the pattern NodeIterator nodes2 = node.getNodes(pattern2); while (nodes2.hasNext()) { Node n = nodes2.nextNode(); assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName()); } // test if the number of found nodes is correct int numExpected2 = 0; for (int i = 0; i < allNodes.size(); i++) { Node n = (Node) allNodes.get(i); if (n.getName().equals(firstNode.getName())) { numExpected2++; } } nodes2 = node.getNodes(pattern2); assertEquals(assertString2 + "number of nodes found: ", numExpected2, getSize(nodes2)); // test pattern "nodeName|nodeName" String pattern3 = firstNode.getName() + "|" + firstNode.getName(); String assertString3 = "node.getNodes(\"" + pattern3 + "\"): "; // test if the names of the found nodes are matching the pattern NodeIterator nodes3 = node.getNodes(pattern3); while (nodes3.hasNext()) { Node n = nodes3.nextNode(); assertEquals(assertString2 + "name comparison failed: ", firstNode.getName(), n.getName()); } // test if the number of found nodes is correct int numExpected3 = 0; for (int i = 0; i < allNodes.size(); i++) { Node n = (Node) allNodes.get(i); if (n.getName().equals(firstNode.getName())) { numExpected3++; } } nodes3 = node.getNodes(pattern3); assertEquals(assertString3 + "number of nodes found: ", numExpected3, getSize(nodes3)); // test pattern "*odeNam*" if (firstNode.getName().length() > 2) { String name = firstNode.getName(); String shortenName = name.substring(1, name.length() - 1); String pattern4 = "*" + shortenName + "*"; String assertString4 = "node.getNodes(\"" + pattern4 + "\"): "; // test if the names of the found nodes are matching the pattern NodeIterator nodes4 = node.getNodes(pattern4); while (nodes4.hasNext()) { Node n = nodes4.nextNode(); assertTrue(assertString4 + "name comparison failed: *" + shortenName + "* not found in " + n.getName(), n.getName().indexOf(shortenName) != -1); } // test if the number of found nodes is correct int numExpected4 = 0; for (int i = 0; i < allNodes.size(); i++) { Node n = (Node) allNodes.get(i); if (n.getName().indexOf(shortenName) != -1) { numExpected4++; } } nodes4 = node.getNodes(pattern4); assertEquals(assertString4 + "number of nodes found: ", numExpected4, getSize(nodes4)); } } /** * Test if getProperty(String relPath) returns the correct node and if a * PathNotFoundException is thrown when property at relPath does not exist */ public void testGetProperty() throws NotExecutableException, RepositoryException { StringBuffer notExistingPath = new StringBuffer("X"); PropertyIterator properties = testRootNode.getProperties(); while (properties.hasNext()) { // build a path that for sure is not existing // (":" of namespace prefix will be replaced later on) notExistingPath.append(properties.nextProperty().getName()); } try { testRootNode.getProperty(notExistingPath.toString().replaceAll(":", "")); fail("getProperty(String relPath) must throw a " + "PathNotFoundException if no node exists at relPath"); } catch (PathNotFoundException e) { // success } try { PropertyIterator properties2 = testRootNode.getProperties(); Property property = properties2.nextProperty(); assertTrue("Property returned by getProperties() is not the same as returned by getProperty(String).", testRootNode.getProperty(property.getName()).isSame(property)); } catch (NoSuchElementException e) { fail("Root node must always have at least one property: jcr:primaryType"); } } /** * Test if all returned items are of type node. */ public void testGetProperties() throws RepositoryException { PropertyIterator properties = testRootNode.getProperties(); while (properties.hasNext()) { Item item = (Item) properties.next(); assertFalse("Item is not a property", item.isNode()); } } /** * Test getProperties(String namePattern) with all possible patterns. Tested * node: root - a NotExecutableException is thrown when root node has no * properties. */ public void testGetPropertiesNamePattern() throws NotExecutableException, RepositoryException { // get root node and build an ArrayList of its sub nodes Node node = testRootNode; if (!node.hasProperties()) { fail("Root node must always have at least one property: jcr:primaryType"); } PropertyIterator allPropertiesIt = node.getProperties(); ArrayList allProperties = new ArrayList(); StringBuffer notExistingPropertyName = new StringBuffer(); while (allPropertiesIt.hasNext()) { Property p = allPropertiesIt.nextProperty(); allProperties.add(p); notExistingPropertyName.append(p.getName() + "X"); } // test that an empty NodeIterator is returned // when the pattern is not matching any child node String pattern0 = notExistingPropertyName.toString().replaceAll(":", ""); NodeIterator properties0 = node.getNodes(pattern0); try { properties0.nextNode(); fail("An empty NodeIterator must be returned if pattern does" + "not match any child node."); } catch (NoSuchElementException e) { // success } // all tests are running using root's first property Property firstProperty = (Property) allProperties.get(0); // test: getProperties("*") String pattern1 = "*"; String assertString1 = "node.getProperties(\"" + pattern1 + "\"): "; PropertyIterator properties1 = node.getProperties(pattern1); assertEquals(assertString1 + "number of properties found: ", allProperties.size(), getSize(properties1)); // test: getProperties("propertyName") String pattern2 = firstProperty.getName(); String assertString2 = "node.getProperties(\"" + pattern2 + "\"): "; // test if the names of the found properties are matching the pattern PropertyIterator properties2 = node.getProperties(pattern2); while (properties2.hasNext()) { Property p = properties2.nextProperty(); assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName()); } // test if the number of found properties is correct int numExpected2 = 0; for (int i = 0; i < allProperties.size(); i++) { Property p = (Property) allProperties.get(i); if (p.getName().equals(firstProperty.getName())) { numExpected2++; } } properties2 = node.getProperties(pattern2); assertEquals(assertString2 + "number of properties found: ", numExpected2, getSize(properties2)); // test: getProperties("propertyName|propertyName") String pattern3 = firstProperty.getName() + "|" + firstProperty.getName(); String assertString3 = "node.getProperties(\"" + pattern3 + "\"): "; // test if the names of the found properties are matching the pattern PropertyIterator properties3 = node.getProperties(pattern3); while (properties3.hasNext()) { Property p = properties3.nextProperty(); assertEquals(assertString2 + "name comparison failed: ", firstProperty.getName(), p.getName()); } // test if the number of found properties is correct int numExpected3 = 0; for (int i = 0; i < allProperties.size(); i++) { Property p = (Property) allProperties.get(i); if (p.getName().equals(firstProperty.getName())) { numExpected3++; } } properties3 = node.getProperties(pattern3); assertEquals(assertString3 + "number of properties found: ", numExpected3, getSize(properties3)); // test: getProperties("*opertyNam*") if (firstProperty.getName().length() > 2) { String name = firstProperty.getName(); String shortenName = name.substring(1, name.length() - 1); String pattern4 = "*" + shortenName + "*"; String assertString4 = "node.getProperties(\"" + pattern4 + "\"): "; // test if the names of the found properties are matching the pattern PropertyIterator properties4 = node.getProperties(pattern4); while (properties4.hasNext()) { Property p = properties4.nextProperty(); assertTrue(assertString4 + "name comparison failed: *" + shortenName + "* not found in " + p.getName(), p.getName().indexOf(shortenName) != -1); } // test if the number of found properties is correct int numExpected4 = 0; for (int i = 0; i < allProperties.size(); i++) { Property p = (Property) allProperties.get(i); if (p.getName().indexOf(shortenName) != -1) { numExpected4++; } } properties4 = node.getProperties(pattern4); assertEquals(assertString4 + "number of properties found: ", numExpected4, getSize(properties4)); } } /** * Test if getPrimaryItem returns the primary item as defined in the primary * node type. Therefor a node with a primary item is located recursively in * the entire repository. A NotExecutableException is thrown when no such * node is found. */ public void testGetPrimaryItem() throws NotExecutableException, RepositoryException { Node node = locateNodeWithPrimaryItem(testRootNode); if (node == null) { throw new NotExecutableException("Workspace does not contain a node with primary item defined"); } String primaryItemName = node.getPrimaryNodeType().getPrimaryItemName(); Item primaryItem = node.getPrimaryItem(); if (primaryItem.isNode()) { assertTrue("Node returned by getPrimaryItem() is not the same as " + "the one aquired by getNode(String)", node.getNode(primaryItemName).isSame(primaryItem)); } else { assertTrue("Property returned by getPrimaryItem() is not the same as " + "the one aquired by getProperty(String)", node.getProperty(primaryItemName).isSame(primaryItem)); } } /** * Test if getPrimaryItem does throw an ItemNotFoundException if the primary * node type does not define a primary item. Therefor a node without a * primary item is located recursively in the entire repository. A * NotExecutableException is thrown when no such node is found. */ public void testGetPrimaryItemItemNotFoundException() throws NotExecutableException, RepositoryException { Node node = locateNodeWithoutPrimaryItem(testRootNode); if (node == null) { throw new NotExecutableException("Workspace does not contain a node without primary item defined"); } try { node.getPrimaryItem(); fail("getPrimaryItem() must throw a ItemNotFoundException " + "if the primary node type does not define one"); } catch (ItemNotFoundException e) { // success } } /** * Test if getIndex() returns the correct index. Therefor a node with same * name sibling is located recursively in the entire repository. If no such * node is found, the test checks if the testRootNode returns 1 */ public void testGetIndex() throws RepositoryException { Node node = locateNodeWithSameNameSiblings(testRootNode); if (node == null) { assertEquals("getIndex() of a node without same name siblings " + "must return 1", testRootNode.getIndex(), 1); } else { NodeIterator nodes = node.getParent().getNodes(node.getName()); int i = 1; while (nodes.hasNext()) { assertEquals("getIndex() must return the correct index", nodes.nextNode().getIndex(), i); i++; } } } public void testGetReferences() throws NotExecutableException, RepositoryException { Node node = locateNodeWithReference(testRootNode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -