/** * testWriterTwice3 is yet another test which tests creating a taxonomy in two separate writing * sessions. This test used to fail because of a bug involving commit(), explained below, and now * should succeed. */ @Test public void testWriterTwice3() throws Exception { Directory indexDir = newDirectory(); // First, create and fill the taxonomy TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); tw.close(); // Now, open the same taxonomy and add the same categories again. // After a few categories, the LuceneTaxonomyWriter implementation // will stop looking for each category on disk, and rather read them // all into memory and close it's reader. The bug was that it closed // the reader, but forgot that it did (because it didn't set the reader // reference to null). tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); // Add one new category, just to make commit() do something: tw.addCategory(new FacetLabel("hi")); // Do a commit(). Here was a bug - if tw had a reader open, it should // be reopened after the commit. However, in our case the reader should // not be open (as explained above) but because it was not set to null, // we forgot that, tried to reopen it, and got an AlreadyClosedException. tw.commit(); assertEquals(expectedCategories.length + 1, tw.getSize()); tw.close(); indexDir.close(); }
/** * Basic tests for TaxonomyWriter. Basically, we test that IndexWriter.addCategory works, i.e. * returns the expected ordinals (this is tested by calling the fillTaxonomy() method above). We * do not test here that after writing the index can be read - this will be done in more tests * below. */ @Test public void testWriter() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); // Also check TaxonomyWriter.getSize() - see that the taxonomy's size // is what we expect it to be. assertEquals(expectedCategories.length, tw.getSize()); tw.close(); indexDir.close(); }
/** * testWriterTwice is exactly like testWriter, except that after adding all the categories, we add * them again, and see that we get the same old ids again - not new categories. */ @Test public void testWriterTwice() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); // run fillTaxonomy again - this will try to add the same categories // again, and check that we see the same ordinal paths again, not // different ones. fillTaxonomy(tw); // Let's check the number of categories again, to see that no // extraneous categories were created: assertEquals(expectedCategories.length, tw.getSize()); tw.close(); indexDir.close(); }
/** * Test writing an empty index, and seeing that a reader finds in it the root category, and only * it. We check all the methods on that root category return the expected results. */ @Test public void testRootOnly() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); // right after opening the index, it should already contain the // root, so have size 1: assertEquals(1, tw.getSize()); tw.close(); TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir); assertEquals(1, tr.getSize()); assertEquals(0, tr.getPath(0).length); assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getParallelTaxonomyArrays().parents()[0]); assertEquals(0, tr.getOrdinal(new FacetLabel())); tr.close(); indexDir.close(); }
/** * testWriterTwice2 is similar to testWriterTwice, except that the index is closed and reopened * before attempting to write to it the same categories again. While testWriterTwice can get along * with writing and reading correctly just to the cache, testWriterTwice2 checks also the actual * disk read part of the writer: */ @Test public void testWriterTwice2() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); fillTaxonomy(tw); tw.close(); tw = new DirectoryTaxonomyWriter(indexDir); // run fillTaxonomy again - this will try to add the same categories // again, and check that we see the same ordinals again, not different // ones, and that the number of categories hasn't grown by the new // additions fillTaxonomy(tw); assertEquals(expectedCategories.length, tw.getSize()); tw.close(); indexDir.close(); }
/** * Another set of tests for the writer, which don't use an array and try to distill the different * cases, and therefore may be more helpful for debugging a problem than testWriter() which is * hard to know why or where it failed. */ @Test public void testWriterSimpler() throws Exception { Directory indexDir = newDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir); assertEquals(1, tw.getSize()); // the root only // Test that adding a new top-level category works assertEquals(1, tw.addCategory(new FacetLabel("a"))); assertEquals(2, tw.getSize()); // Test that adding the same category again is noticed, and the // same ordinal (and not a new one) is returned. assertEquals(1, tw.addCategory(new FacetLabel("a"))); assertEquals(2, tw.getSize()); // Test that adding another top-level category returns a new ordinal, // not the same one assertEquals(2, tw.addCategory(new FacetLabel("b"))); assertEquals(3, tw.getSize()); // Test that adding a category inside one of the above adds just one // new ordinal: assertEquals(3, tw.addCategory(new FacetLabel("a", "c"))); assertEquals(4, tw.getSize()); // Test that adding the same second-level category doesn't do anything: assertEquals(3, tw.addCategory(new FacetLabel("a", "c"))); assertEquals(4, tw.getSize()); // Test that adding a second-level category with two new components // indeed adds two categories assertEquals(5, tw.addCategory(new FacetLabel("d", "e"))); assertEquals(6, tw.getSize()); // Verify that the parents were added above in the order we expected assertEquals(4, tw.addCategory(new FacetLabel("d"))); // Similar, but inside a category that already exists: assertEquals(7, tw.addCategory(new FacetLabel("b", "d", "e"))); assertEquals(8, tw.getSize()); // And now inside two levels of categories that already exist: assertEquals(8, tw.addCategory(new FacetLabel("b", "d", "f"))); assertEquals(9, tw.getSize()); tw.close(); indexDir.close(); }