1
package org.apache.lucene.facet.search.params;
3
import org.apache.lucene.index.IndexWriter;
4
import org.apache.lucene.index.IndexWriterConfig;
5
import org.apache.lucene.store.Directory;
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;
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
22
* http://www.apache.org/licenses/LICENSE-2.0
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.
31
public class FacetRequestTest extends LuceneTestCase {
33
@Test(expected=IllegalArgumentException.class)
34
public void testIllegalNumResults() throws Exception {
35
new CountFacetRequest(new CategoryPath("a", "b"), 0);
38
@Test(expected=IllegalArgumentException.class)
39
public void testIllegalCategoryPath() throws Exception {
40
new CountFacetRequest(null, 1);
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));
50
assertFalse("equals() should return false as fr1.depth != fr2.depth", fr1.equals(fr2));
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);
73
public void testImmutability() throws Exception {
74
// Tests that after a FRH is created by FR, changes to FR are not reflected
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);
83
assertEquals(FacetRequest.DEFAULT_DEPTH, frh.getFacetRequest().getDepth());
89
public void testClone() throws Exception {
90
FacetRequest fr = new CountFacetRequest(new CategoryPath("a"), 10);
91
FacetRequest clone = (FacetRequest) fr.clone();
93
assertEquals("depth should not have been affected in the clone", FacetRequest.DEFAULT_DEPTH, clone.getDepth());