// 4. Check alphabetical order of categories and furniture in tree
assertTreeIsSorted(tree);
}
public void assertTreeIsSorted(JTree tree) {
TreeModel model = tree.getModel();
Object root = model.getRoot();
Collator comparator = Collator.getInstance();
// For each category
for (int i = 0, n = model.getChildCount(root); i < n; i++) {
Object rootChild = model.getChild(root, i);
if (i < n - 1) {
Object nextChild = model.getChild(root, i + 1);
// Check alphatical order of categories nodes in tree
assertTrue("Categories not sorted", comparator.compare(
getNodeText(tree, rootChild),
getNodeText(tree, nextChild)) <= 0);
}
// For each piece of furniture of a category
for (int j = 0, m = model.getChildCount(rootChild) - 1;
j < m; j++) {
Object child = model.getChild(rootChild, j);
if (j < m - 1) {
Object nextChild = model.getChild(rootChild, j + 1);
// Check alphatical order of furniture nodes in tree
assertTrue("Furniture not sorted", comparator.compare(
getNodeText(tree, child),
getNodeText(tree, nextChild)) <= 0);
}
assertTrue("Piece not a leaf", model.isLeaf(child));
}
}
}