@Test public void testGetChildren() throws Exception { Directory dir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir); int numCategories = atLeast(10); int numA = 0, numB = 0; Random random = random(); // add the two categories for which we'll also add children (so asserts are simpler) taxoWriter.addCategory(new FacetLabel("a")); taxoWriter.addCategory(new FacetLabel("b")); for (int i = 0; i < numCategories; i++) { if (random.nextBoolean()) { taxoWriter.addCategory(new FacetLabel("a", Integer.toString(i))); ++numA; } else { taxoWriter.addCategory(new FacetLabel("b", Integer.toString(i))); ++numB; } } // add category with no children taxoWriter.addCategory(new FacetLabel("c")); taxoWriter.close(); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir); // non existing category ChildrenIterator it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("invalid"))); assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next()); // a category with no children it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("c"))); assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next()); // arbitrary negative ordinal it = taxoReader.getChildren(-2); assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next()); // root's children Set<String> roots = new HashSet<>(Arrays.asList("a", "b", "c")); it = taxoReader.getChildren(TaxonomyReader.ROOT_ORDINAL); while (!roots.isEmpty()) { FacetLabel root = taxoReader.getPath(it.next()); assertEquals(1, root.length); assertTrue(roots.remove(root.components[0])); } assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next()); for (int i = 0; i < 2; i++) { FacetLabel cp = i == 0 ? new FacetLabel("a") : new FacetLabel("b"); int ordinal = taxoReader.getOrdinal(cp); it = taxoReader.getChildren(ordinal); int numChildren = 0; int child; while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) { FacetLabel path = taxoReader.getPath(child); assertEquals(2, path.length); assertEquals(path.components[0], i == 0 ? "a" : "b"); ++numChildren; } int expected = i == 0 ? numA : numB; assertEquals("invalid num children", expected, numChildren); } taxoReader.close(); dir.close(); }