pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / params / FacetRequestTest.java
1 package org.apache.lucene.facet.search.params;
2
3 import org.apache.lucene.index.IndexWriter;
4 import org.apache.lucene.index.IndexWriterConfig;
5 import org.apache.lucene.store.Directory;
6 import org.junit.Test;
7
8 import org.apache.lucene.util.LuceneTestCase;
9 import org.apache.lucene.facet.search.FacetResultsHandler;
10 import org.apache.lucene.facet.taxonomy.CategoryPath;
11 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
12 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
13
14 /**
15  * Licensed to the Apache Software Foundation (ASF) under one or more
16  * contributor license agreements.  See the NOTICE file distributed with
17  * this work for additional information regarding copyright ownership.
18  * The ASF licenses this file to You under the Apache License, Version 2.0
19  * (the "License"); you may not use this file except in compliance with
20  * the License.  You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30
31 public class FacetRequestTest extends LuceneTestCase {
32
33   @Test(expected=IllegalArgumentException.class)
34   public void testIllegalNumResults() throws Exception {
35     new CountFacetRequest(new CategoryPath("a", "b"), 0);
36   }
37   
38   @Test(expected=IllegalArgumentException.class)
39   public void testIllegalCategoryPath() throws Exception {
40     new CountFacetRequest(null, 1);
41   }
42
43   @Test
44   public void testHashAndEquals() {
45     CountFacetRequest fr1 = new CountFacetRequest(new CategoryPath("a"), 8);
46     CountFacetRequest fr2 = new CountFacetRequest(new CategoryPath("a"), 8);
47     assertEquals("hashCode() should agree on both objects", fr1.hashCode(), fr2.hashCode());
48     assertTrue("equals() should return true", fr1.equals(fr2));
49     fr1.setDepth(10);
50     assertFalse("equals() should return false as fr1.depth != fr2.depth", fr1.equals(fr2));
51   }
52   
53   @Test
54   public void testGetFacetResultHandlerDifferentTaxonomy() throws Exception {
55     FacetRequest fr = new CountFacetRequest(new CategoryPath("a"), 10);
56     Directory dir1 = newDirectory();
57     Directory dir2 = newDirectory();
58     // create empty indexes, so that LTR ctor won't complain about a missing index.
59     new IndexWriter(dir1, new IndexWriterConfig(TEST_VERSION_CURRENT, null)).close();
60     new IndexWriter(dir2, new IndexWriterConfig(TEST_VERSION_CURRENT, null)).close();
61     TaxonomyReader tr1 = new DirectoryTaxonomyReader(dir1);
62     TaxonomyReader tr2 = new DirectoryTaxonomyReader(dir2);
63     FacetResultsHandler frh1 = fr.createFacetResultsHandler(tr1);
64     FacetResultsHandler frh2 = fr.createFacetResultsHandler(tr2);
65     assertTrue("should not return the same FacetResultHandler instance for different TaxonomyReader instances", frh1 != frh2);
66     tr1.close();
67     tr2.close();
68     dir1.close();
69     dir2.close();
70   }
71   
72   @Test
73   public void testImmutability() throws Exception {
74     // Tests that after a FRH is created by FR, changes to FR are not reflected
75     // in the FRH.
76     FacetRequest fr = new CountFacetRequest(new CategoryPath("a"), 10);
77     Directory dir = newDirectory();
78     // create empty indexes, so that LTR ctor won't complain about a missing index.
79     new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null)).close();
80     TaxonomyReader tr = new DirectoryTaxonomyReader(dir);
81     FacetResultsHandler frh = fr.createFacetResultsHandler(tr);
82     fr.setDepth(10);
83     assertEquals(FacetRequest.DEFAULT_DEPTH, frh.getFacetRequest().getDepth());
84     tr.close();
85     dir.close();
86   }
87   
88   @Test
89   public void testClone() throws Exception {
90     FacetRequest fr = new CountFacetRequest(new CategoryPath("a"), 10);
91     FacetRequest clone = (FacetRequest) fr.clone();
92     fr.setDepth(10);
93     assertEquals("depth should not have been affected in the clone", FacetRequest.DEFAULT_DEPTH, clone.getDepth());
94   }
95   
96 }