📄 treecomparator.java
字号:
this.check = check; compare(sourceFolder + "/" + sc.rootNodeName, 0); } /** * Compares two nodes in the source and target tree * * @param sourcePath The path of the node in the source tree * @param level The level of depth in the tree */ public void compare(String sourcePath, int level) { Node source = null; Node target = null; // get the source path try { source = (Node) session.getItem(sourcePath); } catch (RepositoryException e) { fail("Could not read source node " + sourcePath + ": " + e.getMessage()); } // get the target path String targetPath = getTargetPath(sourcePath); try { session.getItem(targetFolder); } catch (RepositoryException e) { fail("Target folder not found: " + e); } // Check noRecurse: After top level, the target tree must be empty if (noRecurse && level == 1) { check = CHECK_EMPTY; } // compare source and target if (check == CHECK_SAME) { try { target = (Node) session.getItem(targetPath); } catch (RepositoryException e) { showTree(); fail("Could not read target node " + targetPath + ": " + e); } compareNodes(source, target); } else if (check == CHECK_EMPTY) { try { session.getItem(targetPath); fail("The item " + targetPath + " must not be available."); } catch (RepositoryException e) { } } // iterate through all child nodes of the source tree try { NodeIterator ni = source.getNodes(); while (ni.hasNext()) { Node n = (Node) ni.next(); compare(n.getPath(), level + 1); } } catch (RepositoryException e) { fail("Error while iterating through child nodes: " + e); } } /** * Compares two nodes, a and b * * @param a The node in the source tree * @param b The same node in the target tree */ public void compareNodes(Node a, Node b) { try { log.println("Comparing " + a.getPath() + " to " + b.getPath()); } catch (RepositoryException e) { fail("Nodes not available: " + e.getMessage()); } // check primary node type String primaryTypeA = null, primaryTypeB = null; try { primaryTypeA = a.getProperty(jcrPrimaryType).getName(); primaryTypeB = b.getProperty(jcrPrimaryType).getName(); } catch (RepositoryException e) { fail("Primary node type not available: " + e); } assertEquals("Primary node type has changed.", primaryTypeA, primaryTypeB); compareProperties(a, b); } /** * Compares all the properties of the two nodes a and b * * @param a The node in the source tree. * @param b The node in the target tree. */ public void compareProperties(Node a, Node b) { PropertyIterator ai = null; try { ai = a.getProperties(); } catch (RepositoryException e) { fail("Cannot access properties: " + e); } while (ai.hasNext()) { Property pa = (Property) ai.next(); String pName = null; // todo String pPath = null; try { pPath = pa.getPath(); } catch (RepositoryException e) { } int pType = 0; try { pName = pa.getName(); if (pa.getDefinition().isMultiple()) { pType = -9999; } else { pType = pa.getValue().getType(); } } catch (RepositoryException e) { fail("Cannot access property information: " + e); } if (propertyValueMayChange(pName)) { continue; } Property pb = null; // avoid skipped properties if (!propertySkipped(pName)) { try { pb = b.getProperty(pName); } catch (RepositoryException e) { //fail if the property is not there but should fail("Property '" + pPath + "' not available: " + e); } if (!(skipBinary && pType == PropertyType.BINARY)) { // todo // compare source and target value compareProperties(pa, pb); } } } } /** * Compares two properties a and b. * * @param a The property in the source tree. * @param b The property in the target tree. */ public void compareProperties(Property a, Property b) { String nodeName = null, propertyName = null; boolean isMultiple = false; try { nodeName = a.getParent().getName(); propertyName = a.getName(); isMultiple = a.getDefinition().isMultiple(); } catch (RepositoryException e) { fail("Cannot access property information: " + e); } if (!propertyValueMayChange(propertyName)) { if (isMultiple) { try { compareValues(nodeName, propertyName, a.getValues(), b.getValues()); } catch (RepositoryException e) { fail("Could not access property values: " + e); } } else { try { compareValue(nodeName, propertyName, a.getValue(), b.getValue()); } catch (RepositoryException e) { fail("Could not access property value: " + e); } } } } /** * Compares a set of multi-value properties. * * @param n Name of the node. * @param p Name of the property. * @param a Value (array) of the property in the source tree. * @param b Value (array) of the property in the target tree. */ public void compareValues(String n, String p, Value[] a, Value[] b) { assertEquals("Multi-value property '" + p + "' of node '" + n + "' has changed length: ", a.length, b.length); for (int t = 0; t < a.length; t++) { compareValue(n, p, a[t], b[t]); } } /** * Compares the value of two properties * * @param n Name of the node. * @param p Name of the property. * @param a Value of the property in the source tree. * @param b Value of the property in the target tree. */ public void compareValue(String n, String p, Value a, Value b) { if (!propertyValueMayChange(p)) { try { assertEquals("Properties '" + p + "' of node '" + n + "' have different values.", a.getString(), b.getString()); } catch (RepositoryException e) { fail("Cannot access the content of the property value: " + e); } } } /** * Returns whether the value of the property may change in serialization. * For example, the last changed date of a node may change when the node is * re-imported. The values that may change are declared in the config file. * * @param propertyName The property name you want to check * @return True or false to indicate whether the value may or may not * change. */ public boolean propertyValueMayChange(String propertyName) { if (sc.propertyValueMayChange.indexOf(" " + propertyName + " ") < 0) { return false; } else { return true; } } /** * Returns the path to the source root. * * @return The path to the source root. */ public String getSourceRootPath() { return sourceFolder + "/" + sc.rootNodeName; } /** * Returns the path to the target root. * * @return The path to the target root. */ private String getTargetPath(String sourcePath) { String targetPath = sourcePath.replaceAll(sourceFolder, targetFolder); return targetPath; } /** * Displays the current source and target tree for debug/information * purpose */ public void showTree() { Node n = null; try { n = (Node) session.getItem(sc.testroot); showTree(n, 0); } catch (RepositoryException e) { log.println("Cannot display tree diagnostics: " + e); } } /** * Recursive display of source and target tree */ public void showTree(Node n, int level) throws RepositoryException { for (int t = 0; t < level; t++) { log.print("-"); } log.print(n.getName() + " "); log.print(n.getPrimaryNodeType().getName() + " [ "); PropertyIterator pi = n.getProperties(); while (pi.hasNext()) { Property p = (Property) pi.next(); log.print(p.getName() + " "); } log.println("]"); NodeIterator ni = n.getNodes(); while (ni.hasNext()) { showTree((Node) ni.next(), level + 1); } } /** * Checks if a given property should be skipped during xml import. * * @param propertyName * @return */ public boolean propertySkipped(String propertyName) { if (sc.propertySkipped.indexOf(" " + propertyName + " ") < 0) { return false; } else { return true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -