X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestAddTaxonomies.java diff --git a/lucene-java-3.5.0/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestAddTaxonomies.java b/lucene-java-3.5.0/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestAddTaxonomies.java new file mode 100644 index 0000000..a25cd4c --- /dev/null +++ b/lucene-java-3.5.0/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestAddTaxonomies.java @@ -0,0 +1,254 @@ +package org.apache.lucene.facet.taxonomy.directory; + +import java.io.File; + +import org.apache.lucene.store.Directory; +import org.junit.Test; + +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; +import org.apache.lucene.facet.taxonomy.CategoryPath; +import org.apache.lucene.facet.taxonomy.TaxonomyReader; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public class TestAddTaxonomies extends LuceneTestCase { + + @Test + public void test1() throws Exception { + Directory dir1 = newDirectory(); + DirectoryTaxonomyWriter tw1 = new DirectoryTaxonomyWriter(dir1); + tw1.addCategory(new CategoryPath("Author", "Mark Twain")); + tw1.addCategory(new CategoryPath("Animals", "Dog")); + Directory dir2 = newDirectory(); + DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(dir2); + tw2.addCategory(new CategoryPath("Author", "Rob Pike")); + tw2.addCategory(new CategoryPath("Aardvarks", "Bob")); + tw2.close(); + Directory dir3 = newDirectory(); + DirectoryTaxonomyWriter tw3 = new DirectoryTaxonomyWriter(dir3); + tw3.addCategory(new CategoryPath("Author", "Zebra Smith")); + tw3.addCategory(new CategoryPath("Aardvarks", "Bob")); + tw3.addCategory(new CategoryPath("Aardvarks", "Aaron")); + tw3.close(); + + MemoryOrdinalMap[] maps = new MemoryOrdinalMap[2]; + maps[0] = new MemoryOrdinalMap(); + maps[1] = new MemoryOrdinalMap(); + + tw1.addTaxonomies(new Directory[] { dir2, dir3 }, maps); + tw1.close(); + + TaxonomyReader tr = new DirectoryTaxonomyReader(dir1); + + // Test that the merged taxonomy now contains what we expect: + // First all the categories of the original taxonomy, in their original order: + assertEquals(tr.getPath(0).toString(), ""); + assertEquals(tr.getPath(1).toString(), "Author"); + assertEquals(tr.getPath(2).toString(), "Author/Mark Twain"); + assertEquals(tr.getPath(3).toString(), "Animals"); + assertEquals(tr.getPath(4).toString(), "Animals/Dog"); + // Then the categories new in the new taxonomy, in alphabetical order: + assertEquals(tr.getPath(5).toString(), "Aardvarks"); + assertEquals(tr.getPath(6).toString(), "Aardvarks/Aaron"); + assertEquals(tr.getPath(7).toString(), "Aardvarks/Bob"); + assertEquals(tr.getPath(8).toString(), "Author/Rob Pike"); + assertEquals(tr.getPath(9).toString(), "Author/Zebra Smith"); + assertEquals(tr.getSize(), 10); + + // Test that the maps contain what we expect + int[] map0 = maps[0].getMap(); + assertEquals(5, map0.length); + assertEquals(0, map0[0]); + assertEquals(1, map0[1]); + assertEquals(8, map0[2]); + assertEquals(5, map0[3]); + assertEquals(7, map0[4]); + + int[] map1 = maps[1].getMap(); + assertEquals(6, map1.length); + assertEquals(0, map1[0]); + assertEquals(1, map1[1]); + assertEquals(9, map1[2]); + assertEquals(5, map1[3]); + assertEquals(7, map1[4]); + assertEquals(6, map1[5]); + + tr.close(); + dir1.close(); + dir2.close(); + dir3.close(); + } + + // a reasonable random test + public void testmedium() throws Exception { + int numTests = atLeast(3); + for (int i = 0; i < numTests; i++) { + dotest(_TestUtil.nextInt(random, 1, 10), + _TestUtil.nextInt(random, 1, 100), + _TestUtil.nextInt(random, 100, 1000), + random.nextBoolean()); + } + } + + // A more comprehensive and big random test. + @Test @Nightly + public void testbig() throws Exception { + dotest(2, 1000, 5000, false); + dotest(10, 10000, 100, false); + dotest(50, 20, 100, false); + dotest(10, 1000, 10000, false); + dotest(50, 20, 10000, false); + dotest(1, 20, 10000, false); + dotest(10, 1, 10000, false); + dotest(10, 1000, 20000, true); + } + + private void dotest(int ntaxonomies, int ncats, int range, boolean disk) throws Exception { + Directory dirs[] = new Directory[ntaxonomies]; + Directory copydirs[] = new Directory[ntaxonomies]; + + for (int i=0; i1) { + for (int i=0; i= copytr.getSize()); + } else { + assertEquals(copytr.getSize(), tr.getSize()); + } + for (int j=0; j copytr.getSize()) { + String prev = tr.getPath(copytr.getSize()).toString(); + for (int j=copytr.getSize()+1; j