private void checkWriterParent(TaxonomyReader tr, TaxonomyWriter tw) throws Exception { // check that the parent of the root ordinal is the invalid ordinal: assertEquals(TaxonomyReader.INVALID_ORDINAL, tw.getParent(0)); // check parent of non-root ordinals: for (int ordinal = 1; ordinal < tr.getSize(); ordinal++) { FacetLabel me = tr.getPath(ordinal); int parentOrdinal = tw.getParent(ordinal); FacetLabel parent = tr.getPath(parentOrdinal); if (parent == null) { fail( "Parent of " + ordinal + " is " + parentOrdinal + ", but this is not a valid category."); } // verify that the parent is indeed my parent, according to the // strings if (!me.subpath(me.length - 1).equals(parent)) { fail( "Got parent " + parentOrdinal + " for ordinal " + ordinal + " but categories are " + showcat(parent) + " and " + showcat(me) + " respectively."); } } // check parent of of invalid ordinals: try { tw.getParent(-1); fail("getParent for -1 should throw exception"); } catch (ArrayIndexOutOfBoundsException e) { // ok } try { tw.getParent(TaxonomyReader.INVALID_ORDINAL); fail("getParent for INVALID_ORDINAL should throw exception"); } catch (ArrayIndexOutOfBoundsException e) { // ok } try { int parent = tw.getParent(tr.getSize()); fail("getParent for getSize() should throw exception, but returned " + parent); } catch (ArrayIndexOutOfBoundsException e) { // ok } }
/** * Tests for TaxonomyReader's getParent() method. We check it by comparing its results to those we * could have gotten by looking at the category string paths (where the parentage is obvious). * Note that after testReaderBasic(), we already know we can trust the ordinal <=> category * conversions. * * <p>Note: At the moment, the parent methods in the reader are deprecated, but this does not mean * they should not be tested! Until they are removed (*if* they are removed), these tests should * remain to see that they still work correctly. */ @Test public void testReaderParent() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); tw.close(); TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir); // check that the parent of the root ordinal is the invalid ordinal: int[] parents = tr.getParallelTaxonomyArrays().parents(); assertEquals(TaxonomyReader.INVALID_ORDINAL, parents[0]); // check parent of non-root ordinals: for (int ordinal = 1; ordinal < tr.getSize(); ordinal++) { FacetLabel me = tr.getPath(ordinal); int parentOrdinal = parents[ordinal]; FacetLabel parent = tr.getPath(parentOrdinal); if (parent == null) { fail( "Parent of " + ordinal + " is " + parentOrdinal + ", but this is not a valid category."); } // verify that the parent is indeed my parent, according to the strings if (!me.subpath(me.length - 1).equals(parent)) { fail( "Got parent " + parentOrdinal + " for ordinal " + ordinal + " but categories are " + showcat(parent) + " and " + showcat(me) + " respectively."); } } tr.close(); indexDir.close(); }