pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / params / FacetSearchParamsTest.java
1 package org.apache.lucene.facet.search.params;
2
3 import org.apache.lucene.store.Directory;
4 import org.junit.Test;
5
6 import org.apache.lucene.util.LuceneTestCase;
7 import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;
8 import org.apache.lucene.facet.taxonomy.CategoryPath;
9 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
10 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
11 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
12 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
13 import org.apache.lucene.facet.util.PartitionsUtils;
14
15 /**
16  * Licensed to the Apache Software Foundation (ASF) under one or more
17  * contributor license agreements.  See the NOTICE file distributed with
18  * this work for additional information regarding copyright ownership.
19  * The ASF licenses this file to You under the Apache License, Version 2.0
20  * (the "License"); you may not use this file except in compliance with
21  * the License.  You may obtain a copy of the License at
22  *
23  *     http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31
32 public class FacetSearchParamsTest extends LuceneTestCase {
33
34   @Test
35   public void testDefaultSettings() throws Exception {
36     FacetSearchParams fsp = new FacetSearchParams();
37     assertEquals("unexpected default facet indexing params class", DefaultFacetIndexingParams.class.getName(), fsp.getFacetIndexingParams().getClass().getName());
38     assertEquals("no facet requests should be added by default", 0, fsp.getFacetRequests().size());
39     Directory dir = newDirectory();
40     new DirectoryTaxonomyWriter(dir).close();
41     TaxonomyReader tr = new DirectoryTaxonomyReader(dir);
42     assertEquals("unexpected partition offset for 0 categories", 1, PartitionsUtils.partitionOffset(fsp, 1, tr));
43     assertEquals("unexpected partition size for 0 categories", 1, PartitionsUtils.partitionSize(fsp,tr));
44     tr.close();
45     dir.close();
46   }
47   
48   @Test
49   public void testAddFacetRequest() throws Exception {
50     FacetSearchParams fsp = new FacetSearchParams();
51     fsp.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "b"), 1));
52     assertEquals("expected 1 facet request", 1, fsp.getFacetRequests().size());
53   }
54   
55   @Test
56   public void testPartitionSizeWithCategories() throws Exception {
57     FacetSearchParams fsp = new FacetSearchParams();
58     Directory dir = newDirectory();
59     TaxonomyWriter tw = new DirectoryTaxonomyWriter(dir);
60     tw.addCategory(new CategoryPath("a"));
61     tw.commit();
62     tw.close();
63     TaxonomyReader tr = new DirectoryTaxonomyReader(dir);
64     assertEquals("unexpected partition offset for 1 categories", 2, PartitionsUtils.partitionOffset(fsp, 1, tr));
65     assertEquals("unexpected partition size for 1 categories", 2, PartitionsUtils.partitionSize(fsp,tr));
66     tr.close();
67     dir.close();
68   }
69   
70   @Test
71   public void testSearchParamsWithNullRequest() throws Exception {
72     FacetSearchParams fsp = new FacetSearchParams();
73     try {
74       fsp.addFacetRequest(null);
75       fail("FacetSearchParams should throw IllegalArgumentException when trying to add a null FacetRequest");
76     } catch (IllegalArgumentException e) {
77     }
78   }
79 }